Class: ActiveMerchant::Shipping::Location

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Location

Returns a new instance of Location.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_shipping/shipping/location.rb', line 23

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]
  @address1 = options[:address1]
  @address2 = options[:address2]
  @address3 = options[:address3]
  @phone = options[:phone]
  @fax = options[:fax]
  raise ArgumentError.new('address_type must be either "residential" or "commercial"') if options[:address_type] and not (["residential", "commercial", ""]).include?(options[:address_type].to_s)
  @address_type = options[:address_type].nil? ? nil : options[:address_type].to_s
end

Instance Attribute Details

#address1Object (readonly)

Returns the value of attribute address1.



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

def address1
  @address1
end

#address2Object (readonly)

Returns the value of attribute address2.



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

def address2
  @address2
end

#address3Object (readonly)

Returns the value of attribute address3.



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

def address3
  @address3
end

#address_typeObject (readonly)

Returns the value of attribute address_type.



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

def address_type
  @address_type
end

#cityObject (readonly)

Returns the value of attribute city.



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

def city
  @city
end

#countryObject (readonly)

Returns the value of attribute country.



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

def country
  @country
end

#faxObject (readonly)

Returns the value of attribute fax.



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

def fax
  @fax
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#phoneObject (readonly)

Returns the value of attribute phone.



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

def phone
  @phone
end

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

Returns the value of attribute postal_code.



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

def postal_code
  @postal_code
end

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

Returns the value of attribute province.



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

def province
  @province
end

Class Method Details

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



39
40
41
42
43
44
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
# File 'lib/active_shipping/shipping/location.rb', line 39

def self.from(object, options={})
  return object if object.is_a? ActiveMerchant::Shipping::Location
  attr_mappings = {
    :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]
  }
  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 %w{residential commercial}.include?(attributes[:address_type].to_s)
  self.new(attributes.update(options))
end

Instance Method Details

#commercial?Boolean

Returns:

  • (Boolean)


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

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

#country_code(format = :alpha2) ⇒ Object



72
73
74
# File 'lib/active_shipping/shipping/location.rb', line 72

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

#inspectObject



91
92
93
94
95
96
# File 'lib/active_shipping/shipping/location.rb', line 91

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

#prettyprintObject



83
84
85
86
87
88
89
# File 'lib/active_shipping/shipping/location.rb', line 83

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

#residential?Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



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

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