class XeroGateway::Contact

Constants

CONTACT_STATUS
GUID_REGEX

Attributes

errors[RW]
gateway[RW]

Public Class Methods

new(params = {}) click to toggle source
Calls superclass method XeroGateway::BaseRecord::new
# File lib/xero_gateway/contact.rb, line 45
def initialize(params = {})
  super

  @errors ||= []
  @phones ||= []
  @addresses ||= nil
end

Public Instance Methods

add_address(address_params) click to toggle source

Helper method to add a new address object to this contact.

Usage:

contact.add_address({
  :address_type =>   'STREET',
  :line_1 =>         '100 Queen Street',
  :city =>           'Brisbane',
  :region =>         'QLD',
  :post_code =>      '4000',
  :country =>        'Australia'
})
# File lib/xero_gateway/contact.rb, line 74
def add_address(address_params)
  self.addresses << Address.new(address_params)
end
add_contact_person(contact_person_params = {}) click to toggle source
# File lib/xero_gateway/contact.rb, line 101
def add_contact_person(contact_person_params = {})
  self.contact_persons ||= []
  self.contact_persons << ContactPerson.new(contact_person_params)
end
add_phone(phone_params = {}) click to toggle source

Helper method to add a new phone object to this contact.

Usage:

contact.add_phone({
  :phone_type =>   'MOBILE',
  :number =>       '0400123123'
})
# File lib/xero_gateway/contact.rb, line 97
def add_phone(phone_params = {})
  self.phones << Phone.new(phone_params)
end
address() click to toggle source
# File lib/xero_gateway/contact.rb, line 58
def address
  self.addresses    ||= []
  self.addresses[0] ||= Address.new
end
address=(address) click to toggle source
# File lib/xero_gateway/contact.rb, line 54
def address=(address)
  self.addresses = [address]
end
create() click to toggle source

Creates this contact record (using gateway.create_contact) with the associated gateway. If no gateway set, raise a NoGatewayError exception.

# File lib/xero_gateway/contact.rb, line 153
def create
  raise NoGatewayError unless gateway
  gateway.create_contact(self)
end
phone() click to toggle source
# File lib/xero_gateway/contact.rb, line 82
def phone
  if @phones.size > 1
    @phones.detect {|p| p.phone_type == 'DEFAULT'} || phones[0]
  else
    @phones[0] ||= Phone.new
  end
end
phone=(phone) click to toggle source
# File lib/xero_gateway/contact.rb, line 78
def phone=(phone)
  self.phones = [phone]
end
save() click to toggle source

General purpose create/save method. If contact_id and contact_number are nil then create, otherwise, attempt to save.

# File lib/xero_gateway/contact.rb, line 143
def save
  if contact_id.nil? && contact_number.nil?
    create
  else
    update
  end
end
update() click to toggle source

Creates this contact record (using gateway.update_contact) with the associated gateway. If no gateway set, raise a NoGatewayError exception.

# File lib/xero_gateway/contact.rb, line 160
def update
  raise NoGatewayError unless gateway
  gateway.update_contact(self)
end
valid?() click to toggle source

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

Usage:

contact.valid?     # Returns true/false

Additionally sets contact.errors array to an array of field/error.
# File lib/xero_gateway/contact.rb, line 113
def valid?
  @errors = []

  if !contact_id.nil? && contact_id !~ GUID_REGEX
    @errors << ['contact_id', 'must be blank or a valid Xero GUID']
  end

  if status && !CONTACT_STATUS[status]
    @errors << ['status', "must be one of #{CONTACT_STATUS.keys.join('/')}"]
  end

  unless name
    @errors << ['name', "can't be blank"]
  end

  # Make sure all addresses are correct.
  unless addresses.all? { | address | address.valid? }
    @errors << ['addresses', 'at least one address is invalid']
  end

  # Make sure all phone numbers are correct.
  unless phones.all? { | phone | phone.valid? }
    @errors << ['phones', 'at least one phone is invalid']
  end

  @errors.size == 0
end