Class: Xero::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/xero/client.rb

Direct Known Subclasses

Xero::Clients::PrivateApplication

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
# File 'lib/xero/client.rb', line 5

def initialize(options = {})
  self.client = OAuth::Consumer.new(
    Xero.configuration.consumer_key,
    Xero.configuration.consumer_secret, options
  )
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/xero/client.rb', line 3

def client
  @client
end

Instance Method Details

#build_invoice(attributes = {}) ⇒ Object



66
67
68
# File 'lib/xero/client.rb', line 66

def build_invoice(attributes = {})
  build_model Xero::Models::Invoice, attributes
end

#build_item(attributes = {}) ⇒ Object



70
71
72
# File 'lib/xero/client.rb', line 70

def build_item(attributes = {})
  build_model Xero::Models::Item, attributes
end

#build_model(klass, item_attributes = {}) ⇒ Object



74
75
76
77
78
# File 'lib/xero/client.rb', line 74

def build_model(klass, item_attributes = {})
  klass.new(item_attributes).tap do |item|
    item.client = self
  end
end

#contacts(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/xero/client.rb', line 12

def contacts(options = {})
  response = self.connection.get(Xero::Models::Contact.path, options)
  contacts_attributes = hash_from_response(response, 'Contacts')
  unless contacts_attributes.is_a?(Array)
    contacts_attributes = [contacts_attributes]
  end
  contacts_attributes.map { |attrs| Xero::Models::Contact.new(attrs) }
end

#get_contact(contact_id) ⇒ Object



21
22
23
24
25
# File 'lib/xero/client.rb', line 21

def get_contact(contact_id)
  response = self.connection.
    get_by_id(Xero::Models::Contact.path, contact_id)
  Xero::Models::Contact.new hash_from_response(response, 'Contacts')
end

#get_invoice(invoice_id) ⇒ Object



38
39
40
41
42
# File 'lib/xero/client.rb', line 38

def get_invoice(invoice_id)
  response = self.connection.
    get_by_id(Xero::Models::Invoice.path, invoice_id)
  Xero::Models::Invoice.new hash_from_response(response, 'Invoices')
end

#get_item(item_id) ⇒ Object



27
28
29
30
# File 'lib/xero/client.rb', line 27

def get_item(item_id)
  response = self.connection.get_by_id(Xero::Models::Item.path, item_id)
  Xero::Models::Item.new(hash_from_response(response, 'Items'))
end

#invoices(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/xero/client.rb', line 44

def invoices(options = {})
  response = self.connection.get(Xero::Models::Invoice.path, options)
  invoices_attributes = hash_from_response(response, 'Invoices')
  unless invoices_attributes.is_a?(Array)
    invoices_attributes = [invoices_attributes]
  end
  invoices_attributes.map { |attrs| Xero::Models::Invoice.new(attrs) }
end

#items(options = {}) ⇒ Object



32
33
34
35
36
# File 'lib/xero/client.rb', line 32

def items(options = {})
  response = self.connection.get(Xero::Models::Item.path, options)
  items = hash_from_response(response, 'Items')
  items.map { |item| Xero::Models::Item.new(item) }
end

#save(model) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xero/client.rb', line 53

def save(model)
  model.tap do |item|
    response = self.connection.post(model.class.path, model.to_xero_xml)

    Hash.from_xml(response.body)['Response'].tap do |resp|
      klass = model.class.to_s.demodulize
      model.attributes = resp[klass.pluralize][klass]
    end

    model.new_record = false
  end
end