class ReactiveShipping::Location

Constants

ADDRESS_TYPES
ATTRIBUTE_ALIASES

Attributes

address1[R]
address2[R]
address3[R]
address_type[R]
city[R]
company[R]
company_name[R]
country[R]
email[R]
fax[R]
name[R]
options[R]
phone[R]
postal[R]
postal_code[R]
province[R]
region[R]
state[R]
territory[R]
zip[R]

Public Class Methods

from(object, options = {}) click to toggle source
# File lib/reactive_shipping/location.rb, line 65
def self.from(object, options = {})
  return object if object.is_a?(ReactiveShipping::Location)

  attributes = {}

  hash_access = object.respond_to?(:[])

  ATTRIBUTE_ALIASES.each do |attribute, aliases|
    aliases.detect do |sym|
      value = object[sym] if hash_access
      if !value &&
        object.respond_to?(sym) &&
        (!hash_access || !Hash.public_instance_methods.include?(sym))
        value = object.send(sym)
      end

      attributes[attribute] = value if value
    end
  end

  attributes.delete(:address_type) unless ADDRESS_TYPES.include?(attributes[:address_type].to_s)

  new(attributes.update(options))
end
new(options = {}) click to toggle source
# File lib/reactive_shipping/location.rb, line 43
def initialize(options = {})
  @country = if options[:country].nil? || options[:country].is_a?(ActiveUtils::Country)
    options[:country]
  else
    ActiveUtils::Country.find(options[:country])
  end

  @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]
  @email = options[:email]
  @company_name = options[:company_name] || options[:company]

  self.address_type = options[:address_type]
end

Public Instance Methods

==(other) click to toggle source
# File lib/reactive_shipping/location.rb, line 162
def ==(other)
  to_hash == other.to_hash
end
address2_and_3() click to toggle source
# File lib/reactive_shipping/location.rb, line 158
def address2_and_3
  [address2, address3].reject(&:blank?).join(", ")
end
address_type=(value) click to toggle source
# File lib/reactive_shipping/location.rb, line 110
def address_type=(value)
  return unless value.present?
  raise ArgumentError.new("address_type must be one of #{ADDRESS_TYPES.join(', ')}") unless ADDRESS_TYPES.include?(value.to_s)
  @address_type = value.to_s
end
commercial?() click to toggle source
# File lib/reactive_shipping/location.rb, line 98
def commercial?
  @address_type == 'commercial'
end
country_code(format = :alpha2) click to toggle source
# File lib/reactive_shipping/location.rb, line 90
def country_code(format = :alpha2)
  @country.nil? ? nil : @country.code(format).value
end
inspect() click to toggle source
# File lib/reactive_shipping/location.rb, line 145
def inspect
  string = prettyprint
  string << "\nPhone: #{@phone}" unless @phone.blank?
  string << "\nFax: #{@fax}" unless @fax.blank?
  string << "\nEmail: #{@email}" unless @email.blank?
  string
end
po_box?() click to toggle source
# File lib/reactive_shipping/location.rb, line 102
def po_box?
  @address_type == 'po_box'
end
prettyprint() click to toggle source
# File lib/reactive_shipping/location.rb, line 138
def prettyprint
  chunks = [@name, @address1, @address2, @address3]
  chunks << [@city, @province, @postal_code].reject(&:blank?).join(', ')
  chunks << @country
  chunks.reject(&:blank?).join("\n")
end
residential?() click to toggle source
# File lib/reactive_shipping/location.rb, line 94
def residential?
  @address_type == 'residential'
end
to_hash() click to toggle source
# File lib/reactive_shipping/location.rb, line 116
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,
    email: email,
    address_type: address_type,
    company_name: company_name
  }
end
to_s() click to toggle source
# File lib/reactive_shipping/location.rb, line 134
def to_s
  prettyprint.gsub(/\n/, ' ')
end
unknown?() click to toggle source
# File lib/reactive_shipping/location.rb, line 106
def unknown?
  country_code == 'ZZ'
end
zip_plus_4() click to toggle source

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

# File lib/reactive_shipping/location.rb, line 154
def zip_plus_4
  "#{$1}-#{$2}" if /(\d{5})-?(\d{4})/ =~ @postal_code
end