Class: ZohoInvoiceResource::Formats::Customer

Inherits:
Base
  • Object
show all
Defined in:
lib/zoho_invoice_resource/formats/customer.rb

Instance Attribute Summary

Attributes inherited from Base

#element_name

Instance Method Summary collapse

Methods inherited from Base

#initialize, #mime_type

Constructor Details

This class inherits a constructor from ZohoInvoiceResource::Formats::Base

Instance Method Details

#decode(xml) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/zoho_invoice_resource/formats/customer.rb', line 55

def decode(xml)
  super.tap do |hash|
    # avoid weird XML structure
    %w(BillingAddress ShippingAddress).each do |attr_name|
      if hash[attr_name].is_a?(Array)
        hash[attr_name] = hash[attr_name].find{|value| value.is_a?(String)}
      end
    end
  end
end

#encode(record, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zoho_invoice_resource/formats/customer.rb', line 4

def encode(record, options={})
  options[:root] = options[:root].try(:camelize)
  # updatable attributes restricted
  data = {
    'CustomerID'      => record.customer_id,
    'Name'            => record.name,
    'PaymentsDue'     => record.payments_due,
    'BillingAddress'  => record.billing_address,
    'BillingCity'     => record.billing_city,
    'BillingState'    => record.billing_state,
    'BillingZip'      => record.billing_zip,
    'BillingCountry'  => record.billing_country,
    'BillingFax'      => record.billing_fax,
    'ShippingAddress' => record.shipping_address,
    'ShippingCity'    => record.shipping_city,
    'ShippingState'   => record.shipping_state,
    'ShippingZip'     => record.shipping_zip,
    'ShippingCountry' => record.shipping_country,
    'ShippingFax'     => record.shipping_fax,
    'Contacts'        => [],
    'Notes'           => record.notes,
    'CustomFields'    => {}
  }
  record.contacts.each do |contact|
    item = {
      'ContactID'  => contact.contact_id,
      'Salutation' => contact.salutation,
      'FirstName'  => contact.first_name,
      'LastName'   => contact.last_name,
      'EMail'      => contact.e_mail,
      'Phone'      => contact.phone,
      'Mobile'     => contact.mobile
    }
    data['Contacts'] << item
  end if record.contacts
  if record.custom_fields.custom_field_label1
    data['CustomFields']['CustomFieldLabel1'] = record.custom_fields.custom_field_label1
    data['CustomFields']['CustomFieldValue1'] = record.custom_fields.custom_field_value1
  end
  if record.custom_fields.custom_field_label2
    data['CustomFields']['CustomFieldLabel2'] = record.custom_fields.custom_field_label2
    data['CustomFields']['CustomFieldValue2'] = record.custom_fields.custom_field_value2
  end
  if record.custom_fields.custom_field_label3
    data['CustomFields']['CustomFieldLabel3'] = record.custom_fields.custom_field_label3
    data['CustomFields']['CustomFieldValue3'] = record.custom_fields.custom_field_value3
  end
  remove_empty!(data)
  data.to_xml(options)
end