/lib/active_shipping/shipping/location.rb

https://bitbucket.org/pcmyers/active_shipping · Ruby · 137 lines · 123 code · 14 blank · 0 comment · 16 complexity · 2e86e9e460e1f61d378f9397012adbbd MD5 · raw file

  1. module ActiveMerchant #:nodoc:
  2. module Shipping #:nodoc:
  3. class Location
  4. ADDRESS_TYPES = %w{residential commercial po_box}
  5. attr_reader :options,
  6. :country,
  7. :postal_code,
  8. :province,
  9. :city,
  10. :name,
  11. :address1,
  12. :address2,
  13. :address3,
  14. :phone,
  15. :fax,
  16. :address_type,
  17. :company_name
  18. alias_method :zip, :postal_code
  19. alias_method :postal, :postal_code
  20. alias_method :state, :province
  21. alias_method :territory, :province
  22. alias_method :region, :province
  23. alias_method :company, :company_name
  24. def initialize(options = {})
  25. @country = (options[:country].nil? or options[:country].is_a?(ActiveMerchant::Country)) ?
  26. options[:country] :
  27. ActiveMerchant::Country.find(options[:country])
  28. @postal_code = options[:postal_code] || options[:postal] || options[:zip]
  29. @province = options[:province] || options[:state] || options[:territory] || options[:region]
  30. @city = options[:city]
  31. @name = options[:name]
  32. @address1 = options[:address1]
  33. @address2 = options[:address2]
  34. @address3 = options[:address3]
  35. @phone = options[:phone]
  36. @fax = options[:fax]
  37. @company_name = options[:company_name] || options[:company]
  38. self.address_type = options[:address_type]
  39. end
  40. def self.from(object, options={})
  41. return object if object.is_a? ActiveMerchant::Shipping::Location
  42. attr_mappings = {
  43. :name => [:name],
  44. :country => [:country_code, :country],
  45. :postal_code => [:postal_code, :zip, :postal],
  46. :province => [:province_code, :state_code, :territory_code, :region_code, :province, :state, :territory, :region],
  47. :city => [:city, :town],
  48. :address1 => [:address1, :address, :street],
  49. :address2 => [:address2],
  50. :address3 => [:address3],
  51. :phone => [:phone, :phone_number],
  52. :fax => [:fax, :fax_number],
  53. :address_type => [:address_type],
  54. :company_name => [:company, :company_name]
  55. }
  56. attributes = {}
  57. hash_access = begin
  58. object[:some_symbol]
  59. true
  60. rescue
  61. false
  62. end
  63. attr_mappings.each do |pair|
  64. pair[1].each do |sym|
  65. if value = (object[sym] if hash_access) || (object.send(sym) if object.respond_to?(sym) && (!hash_access || !Hash.public_instance_methods.include?(sym.to_s)))
  66. attributes[pair[0]] = value
  67. break
  68. end
  69. end
  70. end
  71. attributes.delete(:address_type) unless ADDRESS_TYPES.include?(attributes[:address_type].to_s)
  72. self.new(attributes.update(options))
  73. end
  74. def country_code(format = :alpha2)
  75. @country.nil? ? nil : @country.code(format).value
  76. end
  77. def residential?; @address_type == 'residential' end
  78. def commercial?; @address_type == 'commercial' end
  79. def po_box?; @address_type == 'po_box' end
  80. def address_type=(value)
  81. return unless value.present?
  82. raise ArgumentError.new("address_type must be one of #{ADDRESS_TYPES.join(', ')}") unless ADDRESS_TYPES.include?(value.to_s)
  83. @address_type = value.to_s
  84. end
  85. def to_hash
  86. {
  87. :country => country_code,
  88. :postal_code => postal_code,
  89. :province => province,
  90. :city => city,
  91. :name => name,
  92. :address1 => address1,
  93. :address2 => address2,
  94. :address3 => address3,
  95. :phone => phone,
  96. :fax => fax,
  97. :address_type => address_type,
  98. :company_name => company_name
  99. }
  100. end
  101. def to_xml(options={})
  102. options[:root] ||= "location"
  103. to_hash.to_xml(options)
  104. end
  105. def to_s
  106. prettyprint.gsub(/\n/, ' ')
  107. end
  108. def prettyprint
  109. chunks = []
  110. chunks << [@name, @address1,@address2,@address3].reject {|e| e.blank?}.join("\n")
  111. chunks << [@city,@province,@postal_code].reject {|e| e.blank?}.join(', ')
  112. chunks << @country
  113. chunks.reject {|e| e.blank?}.join("\n")
  114. end
  115. def inspect
  116. string = prettyprint
  117. string << "\nPhone: #{@phone}" unless @phone.blank?
  118. string << "\nFax: #{@fax}" unless @fax.blank?
  119. string
  120. end
  121. end
  122. end
  123. end