class XeroGateway::Phone

Constants

PHONE_TYPE

Attributes

area_code[RW]
country_code[RW]
errors[R]

Any errors that occurred when the valid? method called.

number[RW]
phone_type[RW]

Public Class Methods

from_xml(phone_element) click to toggle source
# File lib/xero_gateway/phone.rb, line 59
def self.from_xml(phone_element)
  phone = Phone.new
  phone_element.children.each do |element|
    case(element.name)
      when "PhoneType" then phone.phone_type = element.text
      when "PhoneNumber" then phone.number = element.text
      when "PhoneAreaCode" then phone.area_code = element.text
      when "PhoneCountryCode" then phone.country_code = element.text      
    end
  end
  phone
end
new(params = {}) click to toggle source
# File lib/xero_gateway/phone.rb, line 16
def initialize(params = {})
  @errors ||= []
  
  params = {
    :phone_type => "DEFAULT"
  }.merge(params)
  
  params.each do |k,v|
    self.send("#{k}=", v)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/xero_gateway/phone.rb, line 72
def ==(other)
  [:phone_type, :number, :area_code, :country_code].each do |field|
    return false if send(field) != other.send(field)
  end
  return true
end
to_xml(b = Builder::XmlMarkup.new) click to toggle source
# File lib/xero_gateway/phone.rb, line 50
def to_xml(b = Builder::XmlMarkup.new)
  b.Phone {
    b.PhoneType phone_type
    b.PhoneNumber number
    b.PhoneAreaCode area_code if area_code
    b.PhoneCountryCode country_code if country_code
  }
end
valid?() click to toggle source

Validate the Phone record according to what will be valid by the gateway.

Usage:

phone.valid?     # Returns true/false

Additionally sets phone.errors array to an array of field/error.
# File lib/xero_gateway/phone.rb, line 34
def valid?
  @errors = []
        
  unless number
    @errors << ['number', "can't be blank"]
  else
    @errors << ['number', "must 50 characters or less"] if number.length > 50
  end
  
  if phone_type && !PHONE_TYPE[phone_type]
    @errors << ['phone_type', "must be one of #{PHONE_TYPE.keys.join('/')}"]
  end
  
  @errors.size == 0
end