class Deliveries::Address

Constants

COUNTRY_PHONE_PREFIXES
COUNTRY_TRUNK_PREFIXES

Attributes

address_id[RW]
city[RW]
country[RW]
email[RW]
name[RW]
phone[RW]
postcode[RW]
state[RW]
street[RW]

Public Class Methods

new(**attributes) click to toggle source
# File lib/deliveries/address.rb, line 24
def initialize(**attributes)
  self.name = attributes[:name]
  self.email = attributes[:email]
  self.phone = attributes[:phone]
  self.country = attributes[:country]
  self.state = attributes[:state]
  self.city = attributes[:city]
  self.street = attributes[:street]
  self.postcode = attributes[:postcode]
  self.address_id = attributes[:address_id]
end

Public Instance Methods

courierize(courier_id) click to toggle source
# File lib/deliveries/address.rb, line 36
def courierize(courier_id)
  courier_address = %(Deliveries::Couriers::#{courier_id.to_s.camelize}::Address).safe_constantize.new
  instance_variables.each do |iv|
    courier_address.instance_variable_set(iv, instance_variable_get(iv))
  end
  courier_address
end

Protected Instance Methods

format_email() click to toggle source
# File lib/deliveries/address.rb, line 65
def format_email
  return '' if @email.blank?

  matches = @email.split(/[+@]/)
  %(#{matches.first}@#{matches.last})
end
format_international_phone() click to toggle source
# File lib/deliveries/address.rb, line 46
def format_international_phone
  return '' if @phone.blank? || @country.blank?

  country = @country.downcase.to_sym
  prefix = COUNTRY_PHONE_PREFIXES[country]
  trunk_prefix = COUNTRY_TRUNK_PREFIXES[country]

  # Remove white spaces.
  tmp_phone = @phone.gsub(' ', '')

  # Remove current prefix if present.
  tmp_phone.gsub!(/\A(00|\+)#{prefix}/, '')

  # Remove trunk prefix to convert it to international.
  tmp_phone.gsub!(/\A#{trunk_prefix}/, '') if trunk_prefix

  "+#{prefix}#{tmp_phone}"
end