Class: ActiveMerchant::Shipping::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/active_shipping/shipping/location.rb

Constant Summary collapse

ADDRESS_TYPES =
%w(residential commercial po_box)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Location


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_shipping/shipping/location.rb', line 27

def initialize(options = {})
  @country = (options[:country].nil? or options[:country].is_a?(ActiveMerchant::Country)) ?
                options[:country] :
                ActiveMerchant::Country.find(options[:country])
  @postal_code = options[:postal_code] || options[:postal] || options[:zip]
  @province = options[:province] || options[:state] || options[:territory] || options[:region]
  @city = options[:city]
  @name = options[:name]
  @address1 = options[:address1]
  @address2 = options[:address2]
  @address3 = options[:address3]
  @phone = options[:phone]
  @fax = options[:fax]
  @company_name = options[:company_name] || options[:company]

  self.address_type = options[:address_type]
end

Instance Attribute Details

#address1Object (readonly)

Returns the value of attribute address1


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def address1
  @address1
end

#address2Object (readonly)

Returns the value of attribute address2


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def address2
  @address2
end

#address3Object (readonly)

Returns the value of attribute address3


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def address3
  @address3
end

#address_typeObject

Returns the value of attribute address_type


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def address_type
  @address_type
end

#cityObject (readonly)

Returns the value of attribute city


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def city
  @city
end

#company_nameObject (readonly) Also known as: company

Returns the value of attribute company_name


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def company_name
  @company_name
end

#countryObject (readonly)

Returns the value of attribute country


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def country
  @country
end

#faxObject (readonly)

Returns the value of attribute fax


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def fax
  @fax
end

#nameObject (readonly)

Returns the value of attribute name


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def options
  @options
end

#phoneObject (readonly)

Returns the value of attribute phone


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def phone
  @phone
end

#postal_codeObject (readonly) Also known as: zip, postal

Returns the value of attribute postal_code


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def postal_code
  @postal_code
end

#provinceObject (readonly) Also known as: state, territory, region

Returns the value of attribute province


6
7
8
# File 'lib/active_shipping/shipping/location.rb', line 6

def province
  @province
end

Class Method Details

.from(object, options = {}) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_shipping/shipping/location.rb', line 45

def self.from(object, options = {})
  return object if object.is_a? ActiveMerchant::Shipping::Location
  attr_mappings = {
    :name => [:name],
    :country => [:country_code, :country],
    :postal_code => [:postal_code, :zip, :postal],
    :province => [:province_code, :state_code, :territory_code, :region_code, :province, :state, :territory, :region],
    :city => [:city, :town],
    :address1 => [:address1, :address, :street],
    :address2 => [:address2],
    :address3 => [:address3],
    :phone => [:phone, :phone_number],
    :fax => [:fax, :fax_number],
    :address_type => [:address_type],
    :company_name => [:company, :company_name]
  }
  attributes = {}
  hash_access = begin
    object[:some_symbol]
    true
  rescue
    false
  end
  attr_mappings.each do |pair|
    pair[1].each do |sym|
      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)))
        attributes[pair[0]] = value
        break
      end
    end
  end
  attributes.delete(:address_type) unless ADDRESS_TYPES.include?(attributes[:address_type].to_s)
  new(attributes.update(options))
end

Instance Method Details

#address2_and_3Object


147
148
149
# File 'lib/active_shipping/shipping/location.rb', line 147

def address2_and_3
  [address2, address3].reject(&:blank?).join(", ")
end

#commercial?Boolean


85
# File 'lib/active_shipping/shipping/location.rb', line 85

def commercial?; @address_type == 'commercial' end

#country_code(format = :alpha2) ⇒ Object


80
81
82
# File 'lib/active_shipping/shipping/location.rb', line 80

def country_code(format = :alpha2)
  @country.nil? ? nil : @country.code(format).value
end

#inspectObject


129
130
131
132
133
134
# File 'lib/active_shipping/shipping/location.rb', line 129

def inspect
  string = prettyprint
  string << "\nPhone: #{@phone}" unless @phone.blank?
  string << "\nFax: #{@fax}" unless @fax.blank?
  string
end

#po_box?Boolean


86
# File 'lib/active_shipping/shipping/location.rb', line 86

def po_box?; @address_type == 'po_box' end

#prettyprintObject


121
122
123
124
125
126
127
# File 'lib/active_shipping/shipping/location.rb', line 121

def prettyprint
  chunks = []
  chunks << [@name, @address1, @address2, @address3].reject(&:blank?).join("\n")
  chunks << [@city, @province, @postal_code].reject(&:blank?).join(', ')
  chunks << @country
  chunks.reject(&:blank?).join("\n")
end

#residential?Boolean


84
# File 'lib/active_shipping/shipping/location.rb', line 84

def residential?; @address_type == 'residential' end

#to_hashObject


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_shipping/shipping/location.rb', line 95

def to_hash
  {
    :country => country_code,
    :postal_code => postal_code,
    :province => province,
    :city => city,
    :name => name,
    :address1 => address1,
    :address2 => address2,
    :address3 => address3,
    :phone => phone,
    :fax => fax,
    :address_type => address_type,
    :company_name => company_name
  }
end

#to_sObject


117
118
119
# File 'lib/active_shipping/shipping/location.rb', line 117

def to_s
  prettyprint.gsub(/\n/, ' ')
end

#to_xml(options = {}) ⇒ Object


112
113
114
115
# File 'lib/active_shipping/shipping/location.rb', line 112

def to_xml(options = {})
  options[:root] ||= "location"
  to_hash.to_xml(options)
end

#unknown?Boolean


87
# File 'lib/active_shipping/shipping/location.rb', line 87

def unknown?; country_code == 'ZZ' end

#zip_plus_4Object

Returns the postal code as a properly formatted Zip+4 code, e.g. “77095-2233”


137
138
139
140
141
142
143
144
145
# File 'lib/active_shipping/shipping/location.rb', line 137

def zip_plus_4
  if /(\d{5})(\d{4})/ =~ @postal_code
    return "#{$1}-#{$2}"
  elsif /\d{5}-\d{4}/ =~ @postal_code
    return @postal_code
  else
    nil
  end
end