zoho_invoice 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -5,6 +5,8 @@
5
5
  .config
6
6
  .yardoc
7
7
  .rvmrc
8
+ .ruby-version
9
+ .ruby-gemset
8
10
  Gemfile.lock
9
11
  InstalledFiles
10
12
  _yardoc
data/README.md CHANGED
@@ -38,6 +38,24 @@ homebase $ irb
38
38
  => "blahblahblahnumbersnstuff"
39
39
  ```
40
40
 
41
+ ## Client Setup
42
+
43
+ One of the great things about zoho_invoice is the ability for clients to inherit from a global config and override it if need be.
44
+
45
+ ```
46
+ homebase $ irb
47
+ > require 'zoho_invoice'
48
+ => true
49
+ > ZohoInvoice.authtoken = 'first_authtoken'
50
+ => "first_authtoken"
51
+ > ZohoInvoice.apikey = 'first_apikey'
52
+ => "first_apikey"
53
+ > client = ZohoInvoice::Client.new
54
+ => #<ZohoInvoice::Client:0x007f9e6cf274b8 @authtoken="first_authtoken", @scope="invoiceapi", @apikey="first_apikey", @client_options={:url=>"https://invoice.zoho.com"}>
55
+ > another_client = ZohoInvoice::Client.new(:authtoken => "override_authtoken")
56
+ => #<ZohoInvoice::Client:0x007f9e6cf395a0 @authtoken="override_authtoken", @scope="invoiceapi", @apikey="first_apikey", @client_options={:url=>"https://invoice.zoho.com"}>
57
+ ```
58
+
41
59
  ## Usage
42
60
 
43
61
  ```ruby
@@ -138,6 +138,41 @@ module ZohoInvoice
138
138
 
139
139
  private
140
140
 
141
+ def self.retrieve(client, url)
142
+ klass = self.to_s.split('::').last
143
+ page = 1
144
+ query = {}
145
+ objects_to_hydrate = []
146
+
147
+ begin
148
+ result_hash = client.get(url, query).body
149
+ potential_objects = result_hash['Response'][klass + 's']
150
+
151
+ if potential_objects
152
+ potential_objects = potential_objects[klass]
153
+ if potential_objects.is_a? Hash
154
+ potential_objects = [potential_objects]
155
+ end
156
+ objects_to_hydrate += potential_objects
157
+ end
158
+
159
+ page_context = result_hash['Response']['PageContext']
160
+ if page_context
161
+ num_pages = page_context['Total_Pages'].to_i
162
+ page += 1
163
+ query = { :page => page }
164
+ else
165
+ num_pages = nil
166
+ end
167
+ end while num_pages && page <= num_pages
168
+
169
+ self.process_objects(client, objects_to_hydrate)
170
+ rescue Faraday::Error::ClientError => e
171
+ if e.response[:body]
172
+ raise ZohoInvoice::Error::ClientError.from_response(e.response)
173
+ end
174
+ end
175
+
141
176
  def self.process_objects(client, objects_to_hydrate)
142
177
  if objects_to_hydrate.nil?
143
178
  return []
@@ -17,9 +17,22 @@ module ZohoInvoice
17
17
  :shipping_state,
18
18
  :shipping_country,
19
19
  :shipping_fax,
20
- :customer_id
20
+ :customer_id,
21
+ :active
21
22
 
22
23
  has_many :contacts
23
24
 
25
+ def self.all(client, options = {})
26
+ self.all_active(client, options) + self.all_inactive(client, options)
27
+ end
28
+
29
+ def self.all_active(client, options = {})
30
+ retrieve(client, '/api/customers').each { |c| c.active = true }
31
+ end
32
+
33
+ def self.all_inactive(client, options = {})
34
+ retrieve(client, '/api/customers/inactive').each { |c| c.active = false }
35
+ end
36
+
24
37
  end
25
38
  end
@@ -22,14 +22,7 @@ module ZohoInvoice
22
22
  has_many :invoice_items
23
23
 
24
24
  def self.find_by_customer_id(client, id, options = {})
25
- result_hash = client.get("/api/invoices/customer/#{id}").body
26
- potential_objects = result_hash['Response']["Invoices"]
27
- objects_to_hydrate = potential_objects["Invoice"] if potential_objects
28
- self.process_objects(client, objects_to_hydrate)
29
- rescue Faraday::Error::ClientError => e
30
- if e.response[:body]
31
- raise ZohoInvoice::Error::ClientError.from_response(e.response)
32
- end
25
+ retrieve(client, "/api/invoices/customer/#{id}")
33
26
  end
34
27
 
35
28
  def self.find_by_multiple_customer_ids(client, ids, options={})
@@ -41,14 +34,7 @@ module ZohoInvoice
41
34
  end
42
35
 
43
36
  def self.find_unpaid_by_customer_id(client, id, options = {})
44
- result_hash = client.get("/api/invoices/unpaid/customer/#{id}").body
45
- potential_objects = result_hash['Response']["Invoices"]
46
- objects_to_hydrate = potential_objects["Invoice"] if potential_objects
47
- self.process_objects(client, objects_to_hydrate)
48
- rescue Faraday::Error::ClientError => e
49
- if e.response[:body]
50
- raise ZohoInvoice::Error::ClientError.from_response(e.response)
51
- end
37
+ retrieve(client, "/api/invoices/unpaid/customer/#{id}")
52
38
  end
53
39
 
54
40
  def self.find_unpaid_by_multiple_customer_ids(client, ids, options={})
@@ -59,5 +45,9 @@ module ZohoInvoice
59
45
  return new_hash
60
46
  end
61
47
 
48
+ def self.all(client)
49
+ retrieve(client, '/api/invoices')
50
+ end
51
+
62
52
  end
63
53
  end
@@ -1,3 +1,3 @@
1
1
  module ZohoInvoice
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -0,0 +1,7 @@
1
+ <Response>
2
+ <Customers uri='/api/customers'>
3
+ <Customer>
4
+ <CustomerID>1</CustomerID>
5
+ </Customer>
6
+ </Customers>
7
+ </Response>
@@ -1,5 +1,5 @@
1
1
  <Response>
2
- <Invoices uri='/api/views/invoices/customer/1234'>
2
+ <Invoices uri='/api/invoices/customer/1234'>
3
3
  <Invoice>
4
4
  <InvoiceID>1</InvoiceID>
5
5
  <Status>Open</Status>
@@ -0,0 +1,19 @@
1
+ <Response>
2
+ <Invoices uri='/api/invoices/customer/1234'>
3
+ <Invoice>
4
+ <InvoiceID>1</InvoiceID>
5
+ <Status>Open</Status>
6
+ </Invoice>
7
+ <Invoice>
8
+ <InvoiceID>2</InvoiceID>
9
+ <Status>Open</Status>
10
+ </Invoice>
11
+ </Invoices>
12
+ <PageContext>
13
+ <Page>1</Page>
14
+ <Per_Page>2</Per_Page>
15
+ <Total>3</Total>
16
+ <Total_Pages>2</Total_Pages>
17
+ </PageContext>
18
+ </Response>
19
+
@@ -0,0 +1,15 @@
1
+ <Response>
2
+ <Invoices uri='/api/invoices/customer/1234'>
3
+ <Invoice>
4
+ <InvoiceID>3</InvoiceID>
5
+ <Status>Open</Status>
6
+ </Invoice>
7
+ </Invoices>
8
+ <PageContext>
9
+ <Page>2</Page>
10
+ <Per_Page>2</Per_Page>
11
+ <Total>3</Total>
12
+ <Total_Pages>2</Total_Pages>
13
+ </PageContext>
14
+ </Response>
15
+
@@ -1,5 +1,5 @@
1
1
  <Response>
2
- <Invoices uri='/api/views/invoices/customer/1234'>
2
+ <Invoices uri='/api/invoices/customer/1234'>
3
3
  <Invoice>
4
4
  <InvoiceID>1</InvoiceID>
5
5
  <Status>Open</Status>
@@ -1,5 +1,5 @@
1
1
  <Response>
2
- <Invoices uri='/api/views/invoices/unpaid/customer/1234'>
2
+ <Invoices uri='/api/invoices/unpaid/customer/1234'>
3
3
  <Invoice>
4
4
  <InvoiceID>1</InvoiceID>
5
5
  <Status>Open</Status>
@@ -1,5 +1,5 @@
1
1
  <Response>
2
- <Invoices uri='/api/views/invoices/unpaid/customer/1234'>
2
+ <Invoices uri='/api/invoices/unpaid/customer/1234'>
3
3
  <Invoice>
4
4
  <InvoiceID>1</InvoiceID>
5
5
  <Status>Open</Status>
data/spec/spec_helper.rb CHANGED
@@ -53,6 +53,23 @@ def multiple_invoice_test(fixture_to_use, num_customers, method_to_test, method_
53
53
  end
54
54
  end
55
55
 
56
+ def customer_test(path, fixture_to_use, num_customers, active, method_to_test, *args)
57
+ stub_get(path).
58
+ with(:query => default_credentials).
59
+ to_return(:status => 200, :body => fixture(fixture_to_use), :headers => {:content_type => 'application/xml'})
60
+ result = ZohoInvoice::Customer.send(method_to_test, args[0])
61
+ expect(a_get(path).with(:query => default_credentials)).to have_been_made
62
+ expect(result.class).to eq(Array)
63
+ expect(result.length).to eq(num_customers)
64
+ if num_customers > 0
65
+ result.each_with_index do |r, i|
66
+ expect(r.class).to eq(ZohoInvoice::Customer)
67
+ expect(r.customer_id).to eq((i+1).to_s)
68
+ expect(r.active).to be(active)
69
+ end
70
+ end
71
+ end
72
+
56
73
  def error_test(path, fixture_to_use, method_to_test, message, code, response_status, http_status, *args)
57
74
  stub_get(path).
58
75
  with(:query => default_credentials).
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZohoInvoice::Customer do
4
+ describe "retrieving customers" do
5
+ before do
6
+ @client = ZohoInvoice::Client.new(default_credentials)
7
+ end
8
+
9
+ it "should return an array of active customers" do
10
+ customer_test('/api/customers', 'successful_customers_response', 1, true, :all_active, @client)
11
+ end
12
+
13
+ it "should return an array of inactive customers" do
14
+ customer_test('/api/customers/inactive', 'successful_customers_response', 1, false, :all_inactive, @client)
15
+ end
16
+
17
+ it "should return an array of all customers" do
18
+ stub_get('/api/customers').
19
+ with(:query => default_credentials).
20
+ to_return(:status => 200, :body => fixture('successful_customers_response'), :headers => {:content_type => 'application/xml'})
21
+
22
+ stub_get('/api/customers/inactive').
23
+ with(:query => default_credentials).
24
+ to_return(:status => 200, :body => fixture('successful_customers_response'), :headers => {:content_type => 'application/xml'})
25
+
26
+ result = ZohoInvoice::Customer.send('all', @client)
27
+ expect(a_get('/api/customers').with(:query => default_credentials)).to have_been_made
28
+ expect(a_get('/api/customers/inactive').with(:query => default_credentials)).to have_been_made
29
+ expect(result.class).to eq(Array)
30
+ expect(result.length).to eq(2)
31
+
32
+ result.each_with_index do |r, i|
33
+ expect(r.class).to eq(ZohoInvoice::Customer)
34
+ expect(r.active.to_s).to match(/true|false/)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -54,4 +54,31 @@ describe ZohoInvoice::Invoice do
54
54
  end
55
55
  end
56
56
 
57
+ describe "retrieving multiple pages" do
58
+ before do
59
+ @client = ZohoInvoice::Client.new(default_credentials)
60
+ end
61
+
62
+ it "should return all invoices from a paginated response" do
63
+ stub_get('/api/invoices/customer/1234').
64
+ with(:query => default_credentials).
65
+ to_return(:status => 200, :body => fixture('successful_multiple_page_response_page_1'), :headers => {:content_type => 'application/xml'})
66
+
67
+ stub_get('/api/invoices/customer/1234').
68
+ with(:query => default_credentials.merge(:page => 2)).
69
+ to_return(:status => 200, :body => fixture('successful_multiple_page_response_page_2'), :headers => {:content_type => 'application/xml'})
70
+
71
+ result = ZohoInvoice::Invoice.send(:find_by_customer_id, @client, '1234')
72
+ expect(a_get('/api/invoices/customer/1234').with(:query => default_credentials)).to have_been_made
73
+ expect(a_get('/api/invoices/customer/1234').with(:query => default_credentials.merge(:page => 2))).to have_been_made
74
+ expect(result.class).to eq(Array)
75
+ expect(result.length).to eq(3)
76
+
77
+ result.each_with_index do |r, i|
78
+ expect(r.class).to eq(ZohoInvoice::Invoice)
79
+ expect(r.invoice_id).to eq((i+1).to_s)
80
+ end
81
+ end
82
+ end
83
+
57
84
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zoho_invoice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-27 00:00:00.000000000 Z
12
+ date: 2013-04-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -130,9 +130,12 @@ files:
130
130
  - spec/fixtures/invalid_user
131
131
  - spec/fixtures/please_select_an_item
132
132
  - spec/fixtures/successful_authtoken
133
+ - spec/fixtures/successful_customers_response
133
134
  - spec/fixtures/successful_empty_customer_response
134
135
  - spec/fixtures/successful_empty_response
135
136
  - spec/fixtures/successful_multiple_customer_response
137
+ - spec/fixtures/successful_multiple_page_response_page_1
138
+ - spec/fixtures/successful_multiple_page_response_page_2
136
139
  - spec/fixtures/successful_multiple_record_response
137
140
  - spec/fixtures/successful_single_customer_response
138
141
  - spec/fixtures/successful_single_record_response
@@ -146,6 +149,7 @@ files:
146
149
  - spec/zoho_invoice/client_spec.rb
147
150
  - spec/zoho_invoice/collection_spec.rb
148
151
  - spec/zoho_invoice/contact_spec.rb
152
+ - spec/zoho_invoice/customer_spec.rb
149
153
  - spec/zoho_invoice/error/client_error_spec.rb
150
154
  - spec/zoho_invoice/invoice_spec.rb
151
155
  - spec/zoho_invoice/string_spec.rb
@@ -184,9 +188,12 @@ test_files:
184
188
  - spec/fixtures/invalid_user
185
189
  - spec/fixtures/please_select_an_item
186
190
  - spec/fixtures/successful_authtoken
191
+ - spec/fixtures/successful_customers_response
187
192
  - spec/fixtures/successful_empty_customer_response
188
193
  - spec/fixtures/successful_empty_response
189
194
  - spec/fixtures/successful_multiple_customer_response
195
+ - spec/fixtures/successful_multiple_page_response_page_1
196
+ - spec/fixtures/successful_multiple_page_response_page_2
190
197
  - spec/fixtures/successful_multiple_record_response
191
198
  - spec/fixtures/successful_single_customer_response
192
199
  - spec/fixtures/successful_single_record_response
@@ -200,6 +207,7 @@ test_files:
200
207
  - spec/zoho_invoice/client_spec.rb
201
208
  - spec/zoho_invoice/collection_spec.rb
202
209
  - spec/zoho_invoice/contact_spec.rb
210
+ - spec/zoho_invoice/customer_spec.rb
203
211
  - spec/zoho_invoice/error/client_error_spec.rb
204
212
  - spec/zoho_invoice/invoice_spec.rb
205
213
  - spec/zoho_invoice/string_spec.rb