Class: XeroGateway::Contact

Inherits:
Object
  • Object
show all
Includes:
Dates
Defined in:
lib/xero_gateway/contact.rb

Constant Summary collapse

GUID_REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
CONTACT_STATUS =
{
  'ACTIVE' =>     'Active',
  'DELETED' =>    'Deleted'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dates

included

Constructor Details

#initialize(params = {}) ⇒ Contact

Returns a new instance of Contact.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/xero_gateway/contact.rb', line 23

def initialize(params = {})
  @errors ||= []

  params = {}.merge(params)      
  params.each do |k,v|
    self.send("#{k}=", v)
  end

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

Instance Attribute Details

#accounts_payable_tax_typeObject

Returns the value of attribute accounts_payable_tax_type.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def accounts_payable_tax_type
  @accounts_payable_tax_type
end

#accounts_receivable_tax_typeObject

Returns the value of attribute accounts_receivable_tax_type.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def accounts_receivable_tax_type
  @accounts_receivable_tax_type
end

#addressesObject

Returns the value of attribute addresses.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def addresses
  @addresses
end

#bank_account_detailsObject

Returns the value of attribute bank_account_details.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def 
  @bank_account_details
end

#contact_groupsObject

Returns the value of attribute contact_groups.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def contact_groups
  @contact_groups
end

#contact_idObject

Returns the value of attribute contact_id.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def contact_id
  @contact_id
end

#contact_numberObject

Returns the value of attribute contact_number.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def contact_number
  @contact_number
end

#default_currencyObject

Returns the value of attribute default_currency.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def default_currency
  @default_currency
end

#emailObject

Returns the value of attribute email.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def email
  @email
end

#errorsObject (readonly)

Any errors that occurred when the #valid? method called.



16
17
18
# File 'lib/xero_gateway/contact.rb', line 16

def errors
  @errors
end

#first_nameObject

Returns the value of attribute first_name.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def first_name
  @first_name
end

#gatewayObject

Xero::Gateway associated with this contact.



13
14
15
# File 'lib/xero_gateway/contact.rb', line 13

def gateway
  @gateway
end

#is_customerObject

Returns the value of attribute is_customer.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def is_customer
  @is_customer
end

#is_supplierObject

Returns the value of attribute is_supplier.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def is_supplier
  @is_supplier
end

#last_nameObject

Returns the value of attribute last_name.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def last_name
  @last_name
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def name
  @name
end

#phonesObject

Returns the value of attribute phones.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def phones
  @phones
end

#statusObject

Returns the value of attribute status.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def status
  @status
end

#tax_numberObject

Returns the value of attribute tax_number.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def tax_number
  @tax_number
end

#updated_atObject

Returns the value of attribute updated_at.



18
19
20
# File 'lib/xero_gateway/contact.rb', line 18

def updated_at
  @updated_at
end

Class Method Details

.from_xml(contact_element, gateway = nil) ⇒ Object

Take a Contact element and convert it into an Contact object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/xero_gateway/contact.rb', line 165

def self.from_xml(contact_element, gateway = nil)
  contact = Contact.new(:gateway => gateway)
  contact_element.children.each do |element|
    case(element.name)
      when "ContactID" then contact.contact_id = element.text
      when "ContactNumber" then contact.contact_number = element.text
      when "ContactStatus" then contact.status = element.text
      when "Name" then contact.name = element.text
      when "FirstName" then contact.first_name = element.text
      when "LastName" then contact.last_name = element.text
      when "EmailAddress" then contact.email = element.text
      when "Addresses" then element.children.each {|address_element| contact.addresses << Address.from_xml(address_element)}
      when "Phones" then element.children.each {|phone_element| contact.phones << Phone.from_xml(phone_element)}
      when "FirstName" then contact.first_name = element.text
      when "LastName"  then contact.last_name  = element.text
      when "BankAccountDetails" then contact. = element.text
      when "TaxNumber" then contact.tax_number = element.text
      when "AccountsReceivableTaxType" then contact.accounts_receivable_tax_type = element.text
      when "AccountsPayableTaxType" then contact.accounts_payable_tax_type = element.text
      when "ContactGroups" then contact.contact_groups = element.text
      when "IsCustomer" then contact.is_customer = (element.text == "true")
      when "IsSupplier" then contact.is_supplier = (element.text == "true")
      when "DefaultCurrency" then contact.default_currency = element.text
    end
  end
  contact
end

Instance Method Details

#==(other) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/xero_gateway/contact.rb', line 193

def ==(other)
  [ :contact_id, :contact_number, :status, :name, :first_name, :last_name, :email, :addresses, :phones, :updated_at,
    :bank_account_details, :tax_number, :accounts_receivable_tax_type, :accounts_payable_tax_type, :is_customer, :is_supplier,
    :default_currency, :contact_groups ].each do |field|
    return false if send(field) != other.send(field)
  end
  return true
end

#add_address(address_params) ⇒ Object

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'
})


54
55
56
# File 'lib/xero_gateway/contact.rb', line 54

def add_address(address_params)
  self.addresses << Address.new(address_params)
end

#add_phone(phone_params = {}) ⇒ Object

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

Usage:

contact.add_phone({
  :phone_type =>   'MOBILE',
  :number =>       '0400123123'
})


77
78
79
# File 'lib/xero_gateway/contact.rb', line 77

def add_phone(phone_params = {})
  self.phones << Phone.new(phone_params)
end

#addressObject



39
40
41
# File 'lib/xero_gateway/contact.rb', line 39

def address
  self.addresses[0] ||= Address.new
end

#address=(address) ⇒ Object



35
36
37
# File 'lib/xero_gateway/contact.rb', line 35

def address=(address)
  self.addresses = [address]
end

#createObject

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

Raises:



127
128
129
130
# File 'lib/xero_gateway/contact.rb', line 127

def create
  raise NoGatewayError unless gateway
  gateway.create_contact(self)
end

#phoneObject



62
63
64
65
66
67
68
# File 'lib/xero_gateway/contact.rb', line 62

def phone
  if @phones.size > 1
    @phones.detect {|p| p.phone_type == 'DEFAULT'} || phones[0]
  else
    @phones[0] ||= Phone.new
  end
end

#phone=(phone) ⇒ Object



58
59
60
# File 'lib/xero_gateway/contact.rb', line 58

def phone=(phone)
  self.phones = [phone]
end

#saveObject

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



117
118
119
120
121
122
123
# File 'lib/xero_gateway/contact.rb', line 117

def save
  if contact_id.nil? && contact_number.nil?
    create
  else
    update
  end
end

#to_xml(b = Builder::XmlMarkup.new) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/xero_gateway/contact.rb', line 139

def to_xml(b = Builder::XmlMarkup.new)
  b.Contact {
    b.ContactID self.contact_id if self.contact_id
    b.ContactNumber self.contact_number if self.contact_number
    b.Name self.name if self.name
    b.EmailAddress self.email if self.email
    b.FirstName self.first_name if self.first_name
    b.LastName self.last_name if self.last_name
    b.BankAccountDetails self. if self.
    b.TaxNumber self.tax_number if self.tax_number
    b.AccountsReceivableTaxType self.accounts_receivable_tax_type if self.accounts_receivable_tax_type
    b.AccountsPayableTaxType self.accounts_payable_tax_type if self.accounts_payable_tax_type
    b.ContactGroups if self.contact_groups
    b.IsCustomer true if self.is_customer
    b.IsSupplier true if self.is_supplier
    b.DefaultCurrency if self.default_currency
    b.Addresses {
      addresses.each { |address| address.to_xml(b) }
    } if self.addresses.any?
    b.Phones {
      phones.each { |phone| phone.to_xml(b) }
    } if self.phones.any?
  }
end

#updateObject

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

Raises:



134
135
136
137
# File 'lib/xero_gateway/contact.rb', line 134

def update
  raise NoGatewayError unless gateway
  gateway.update_contact(self)
end

#valid?Boolean

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.

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/xero_gateway/contact.rb', line 87

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