myob-essentials-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.rspec +2 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +12 -0
  6. data/Gemfile +14 -0
  7. data/Gemfile.lock +86 -0
  8. data/LICENSE +22 -0
  9. data/README.md +188 -0
  10. data/Rakefile +51 -0
  11. data/VERSION +1 -0
  12. data/lib/myob-essentials-api.rb +13 -0
  13. data/lib/myob/essentials/api/client.rb +86 -0
  14. data/lib/myob/essentials/api/helper.rb +29 -0
  15. data/lib/myob/essentials/api/model/account.rb +13 -0
  16. data/lib/myob/essentials/api/model/account_balance.rb +13 -0
  17. data/lib/myob/essentials/api/model/account_classification.rb +13 -0
  18. data/lib/myob/essentials/api/model/account_type.rb +13 -0
  19. data/lib/myob/essentials/api/model/base.rb +114 -0
  20. data/lib/myob/essentials/api/model/business.rb +13 -0
  21. data/lib/myob/essentials/api/model/contact.rb +13 -0
  22. data/lib/myob/essentials/api/model/inventory_item.rb +13 -0
  23. data/lib/myob/essentials/api/model/sale_invoice.rb +13 -0
  24. data/lib/myob/essentials/api/model/sale_payment.rb +13 -0
  25. data/lib/myob/essentials/api/model/tax_type.rb +13 -0
  26. data/myob-essentials-api.gemspec +102 -0
  27. data/spec/fixtures/account/classifications.json +104 -0
  28. data/spec/fixtures/account/classifications/1.json +11 -0
  29. data/spec/fixtures/account/types.json +170 -0
  30. data/spec/fixtures/account/types/1.json +11 -0
  31. data/spec/fixtures/base1.json +38 -0
  32. data/spec/fixtures/base2.json +42 -0
  33. data/spec/fixtures/base3.json +29 -0
  34. data/spec/fixtures/businesses.json +84 -0
  35. data/spec/fixtures/businesses/168254.json +34 -0
  36. data/spec/fixtures/businesses/168254/contacts.json +386 -0
  37. data/spec/fixtures/businesses/168254/contacts/3604150.json +37 -0
  38. data/spec/fixtures/businesses/168254/generalledger/accounts.json +1978 -0
  39. data/spec/fixtures/businesses/168254/generalledger/accounts/10108109.json +29 -0
  40. data/spec/fixtures/businesses/168254/inventory/items.json +331 -0
  41. data/spec/fixtures/businesses/168254/inventory/items/987816.json +45 -0
  42. data/spec/fixtures/businesses/168254/sale/invoices.json +131 -0
  43. data/spec/fixtures/businesses/168254/sale/invoices/35608735.json +55 -0
  44. data/spec/fixtures/tax/types.json +79 -0
  45. data/spec/fixtures/tax/types/5.json +7 -0
  46. data/spec/myob/essentials/api/client_spec.rb +78 -0
  47. data/spec/myob/essentials/api/model/account_classification_spec.rb +31 -0
  48. data/spec/myob/essentials/api/model/account_spec.rb +31 -0
  49. data/spec/myob/essentials/api/model/account_type_spec.rb +31 -0
  50. data/spec/myob/essentials/api/model/base_spec.rb +100 -0
  51. data/spec/myob/essentials/api/model/business_spec.rb +31 -0
  52. data/spec/myob/essentials/api/model/contact_spec.rb +32 -0
  53. data/spec/myob/essentials/api/model/inventory_item_spec.rb +32 -0
  54. data/spec/myob/essentials/api/model/sale_invoice_spec.rb +32 -0
  55. data/spec/myob/essentials/api/model/sale_payment_spec.rb +34 -0
  56. data/spec/myob/essentials/api/model/tax_type_spec.rb +31 -0
  57. data/spec/spec_helper.rb +94 -0
  58. metadata +143 -0
@@ -0,0 +1,55 @@
1
+ {
2
+ "uid": "35608735",
3
+ "uri": "https://api.myob.com/au/essentials/businesses/168254/sale/invoices/35608735",
4
+ "invoiceNumber": "IV00000000001",
5
+ "contact": {
6
+ "uid": "3604148",
7
+ "uri": "https://api.myob.com/au/essentials/businesses/168254/contacts/3604148",
8
+ "name": "Malvern Glass"
9
+ },
10
+ "issueDate": "2014-09-29",
11
+ "dueDate": "2014-10-29",
12
+ "gstInclusive": true,
13
+ "lines": [
14
+ {
15
+ "account": {
16
+ "uid": "10108111",
17
+ "uri": "https://api.myob.com/au/essentials/businesses/168254/generalledger/accounts/10108111",
18
+ "name": "Sales 1",
19
+ "displayId": "4-2400"
20
+ },
21
+ "item": {
22
+ "uid": "987815",
23
+ "uri": "https://api.myob.com/au/essentials/businesses/168254/inventory/items/987815",
24
+ "number": "AC304",
25
+ "name": "Pens"
26
+ },
27
+ "taxType": {
28
+ "uid": "10",
29
+ "uri": "https://api.myob.com/au/essentials/tax/types/10",
30
+ "code": "GST",
31
+ "name": "Goods & Services Tax"
32
+ },
33
+ "unitOfMeasure": "Qty",
34
+ "description": "ball point and gel tips",
35
+ "quantity": 150,
36
+ "unitPrice": 6.05,
37
+ "total": 907.5,
38
+ "links": []
39
+ }
40
+ ],
41
+ "notes": null,
42
+ "purchaseOrderNumber": null,
43
+ "total": 907.5,
44
+ "gst": 82.5,
45
+ "amountPaid": 0,
46
+ "amountDue": 907.5,
47
+ "status": "Open",
48
+ "displayStatus": "Overdue",
49
+ "_links": [
50
+ {
51
+ "rel": "self",
52
+ "href": "https://api.myob.com/au/essentials/businesses/168254/sale/invoices/35608735"
53
+ }
54
+ ]
55
+ }
@@ -0,0 +1,79 @@
1
+ {
2
+ "_links": [
3
+ {
4
+ "rel": "self",
5
+ "href": "https://api.myob.com/au/essentials/tax/types"
6
+ }
7
+ ],
8
+ "items": [
9
+ {
10
+ "uid": "5",
11
+ "uri": null,
12
+ "code": "ABN",
13
+ "name": "ABN Not Disclosed - Withholdings",
14
+ "_links": []
15
+ },
16
+ {
17
+ "uid": "6",
18
+ "uri": null,
19
+ "code": "CAP",
20
+ "name": "Capital Acquisitions",
21
+ "_links": []
22
+ },
23
+ {
24
+ "uid": "7",
25
+ "uri": null,
26
+ "code": "EXP",
27
+ "name": "Export Sales",
28
+ "_links": []
29
+ },
30
+ {
31
+ "uid": "8",
32
+ "uri": null,
33
+ "code": "FRE",
34
+ "name": "GST Free",
35
+ "_links": []
36
+ },
37
+ {
38
+ "uid": "9",
39
+ "uri": null,
40
+ "code": "GNR",
41
+ "name": "Not Registered",
42
+ "_links": []
43
+ },
44
+ {
45
+ "uid": "10",
46
+ "uri": null,
47
+ "code": "GST",
48
+ "name": "Goods & Services Tax",
49
+ "_links": []
50
+ },
51
+ {
52
+ "uid": "11",
53
+ "uri": null,
54
+ "code": "INP",
55
+ "name": "Input Taxed Purchases",
56
+ "_links": []
57
+ },
58
+ {
59
+ "uid": "12",
60
+ "uri": null,
61
+ "code": "ITS",
62
+ "name": "Input Taxed Sales",
63
+ "_links": []
64
+ },
65
+ {
66
+ "uid": "13",
67
+ "uri": null,
68
+ "code": "NTR",
69
+ "name": "Not Reportable",
70
+ "_links": []
71
+ }
72
+ ],
73
+ "page": {
74
+ "size": 9,
75
+ "totalElements": 9,
76
+ "totalPages": 1,
77
+ "number": 1
78
+ }
79
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "uid": "5",
3
+ "uri": null,
4
+ "code": "ABN",
5
+ "name": "ABN Not Disclosed - Withholdings",
6
+ "_links": []
7
+ }
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Myob::Essentials::Api::Client do
4
+
5
+ let(:consumer) { {key: 'key', secret: 'secret'} }
6
+ let(:params) { {redirect_uri: 'redirect_uri', consumer: consumer, access_token: 'access_token', refresh_token: 'refresh_token'} }
7
+
8
+ subject { Myob::Essentials::Api::Client.new(params) }
9
+
10
+ describe ".get_access_code_url" do
11
+ it "generates the access code url" do
12
+ expect(subject.get_access_code_url).to eql('https://secure.myob.com/oauth2/account/authorize?client_id=key&redirect_uri=redirect_uri&response_type=code&scope=la.global')
13
+ end
14
+ end
15
+
16
+ describe ".get_access_token" do
17
+ let(:access_code) { '123' }
18
+
19
+ before { Timecop.freeze(Time.parse("2015-01-01T00:00:00")) }
20
+ after { Timecop.return }
21
+
22
+ it "requests the access_token" do
23
+ stub_request(:post, "https://secure.myob.com/oauth2/v1/authorize").
24
+ to_return(:status => 200, :body => { 'access_token'=>'access_code','token_type'=>'bearer','expires_in'=>1200,'refresh_token'=>'refresh_token',
25
+ 'scope'=>'la.global','user'=>{'uid'=>'uid','username'=>'username'}}.to_json,
26
+ :headers => {"content-type"=>"application/json; charset=utf-8"})
27
+
28
+ subject.get_access_token(access_code)
29
+ expect(subject.access_token).to eql('access_code')
30
+ expect(subject.refresh_token).to eql('refresh_token')
31
+ expect(subject.expires_at).to eql((Time.now+1200).to_i)
32
+ end
33
+ end
34
+
35
+ describe ".refresh!" do
36
+ before { Timecop.freeze(Time.parse("2015-01-01T00:00:00")) }
37
+ after { Timecop.return }
38
+
39
+ it "refreshes the access_token" do
40
+ stub_request(:post, "https://secure.myob.com/oauth2/v1/authorize")
41
+ .with(:body => {"client_id"=>"key", "client_secret"=>"secret", "grant_type"=>"refresh_token", "refresh_token"=>"refresh_token"},
42
+ :headers => {'Content-Type'=>'application/x-www-form-urlencoded'})
43
+ .to_return(:status => 200, :body => { 'access_token'=>'new_access_code','token_type'=>'bearer','expires_in'=>1200,'refresh_token'=>'new_refresh_token',
44
+ 'scope'=>'la.global','user'=>{'uid'=>'uid','username'=>'username'}}.to_json,
45
+ :headers => {"content-type"=>"application/json; charset=utf-8"})
46
+
47
+ subject.refresh!
48
+ expect(subject.access_token).to eql('new_access_code')
49
+ expect(subject.refresh_token).to eql('new_refresh_token')
50
+ expect(subject.expires_at).to eql((Time.now+1200).to_i)
51
+ end
52
+ end
53
+
54
+ describe ".connection" do
55
+ let (:expires_in) { 1200 }
56
+
57
+ before { stub_request(:post, "https://secure.myob.com/oauth2/v1/authorize").
58
+ to_return(:status => 200, :body => { 'access_token'=>'access_code','token_type'=>'bearer','expires_in'=>expires_in,'refresh_token'=>'refresh_token',
59
+ 'scope'=>'la.global','user'=>{'uid'=>'uid','username'=>'username'}}.to_json,
60
+ :headers => {"content-type"=>"application/json; charset=utf-8"}) }
61
+ before { subject.get_access_token('access_code') }
62
+
63
+ context "current token is valid" do
64
+ it "returns the current access token" do
65
+ expect(subject.connection).to be_a(OAuth2::AccessToken)
66
+ end
67
+ end
68
+
69
+ context "current token is expired" do
70
+ let (:expires_in) { -1200 }
71
+
72
+ it "returns the current access token" do
73
+ expect(subject).to receive(:refresh!)
74
+ expect(subject.connection).to be_a(OAuth2::AccessToken)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Myob::Essentials::Api::Model::AccountClassification do
4
+
5
+ let(:consumer) { {key: 'key', secret: 'secret'} }
6
+ let(:params) { {consumer: consumer, access_token: 'access_token', refresh_token: 'refresh_token'} }
7
+
8
+ subject { Myob::Essentials::Api::Client.new(params) }
9
+
10
+ describe ".account_classification.all_items" do
11
+ let(:tax_types_response) { File.read('spec/fixtures/account/classifications.json') }
12
+
13
+ before { stub_request(:get, "https://api.myob.com/au/essentials/account/classifications").to_return(:status => 200, :body => tax_types_response, :headers => {}) }
14
+
15
+ it "fetches the list of account classifications" do
16
+ expect(subject.account_classification.all_items).to eql(JSON.parse(tax_types_response)["items"])
17
+ end
18
+ end
19
+
20
+ describe ".account_classification.find" do
21
+ let(:tax_type_uid) { '1' }
22
+ let(:tax_type_response) { File.read("spec/fixtures/account/classifications/#{tax_type_uid}.json") }
23
+
24
+ before { stub_request(:get, "https://api.myob.com/au/essentials/account/classifications/#{tax_type_uid}").to_return(:status => 200, :body => tax_type_response, :headers => {}) }
25
+
26
+ it "fetches an account classification by uid" do
27
+ expect(subject.account_classification.find(tax_type_uid)).to eql(JSON.parse(tax_type_response))
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Myob::Essentials::Api::Model::Account do
4
+
5
+ let(:consumer) { {key: 'key', secret: 'secret'} }
6
+ let(:business_uid) { '168254' }
7
+ let(:params) { {consumer: consumer, access_token: 'access_token', refresh_token: 'refresh_token', business_uid: business_uid} }
8
+
9
+ subject { Myob::Essentials::Api::Client.new(params) }
10
+
11
+ describe ".account.all_items" do
12
+ let(:accounts_response) { File.read("spec/fixtures/businesses/#{business_uid}/generalledger/accounts.json") }
13
+
14
+ before { stub_request(:get, "https://api.myob.com/au/essentials/businesses/#{business_uid}/generalledger/accounts").to_return(:status => 200, :body => accounts_response, :headers => {}) }
15
+
16
+ it "fetches the list of accounts" do
17
+ expect(subject.account.all_items).to eql(JSON.parse(accounts_response)["items"])
18
+ end
19
+ end
20
+
21
+ describe ".account.find" do
22
+ let(:account_uid) { '10108109' }
23
+ let(:account_response) { File.read("spec/fixtures/businesses/#{business_uid}/generalledger/accounts/#{account_uid}.json") }
24
+
25
+ before { stub_request(:get, "https://api.myob.com/au/essentials/businesses/#{business_uid}/generalledger/accounts/#{account_uid}").to_return(:status => 200, :body => account_response, :headers => {}) }
26
+ it "fetches an account by uid" do
27
+ expect(subject.account.find(account_uid)).to eql(JSON.parse(account_response))
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Myob::Essentials::Api::Model::AccountType do
4
+
5
+ let(:consumer) { {key: 'key', secret: 'secret'} }
6
+ let(:params) { {consumer: consumer, access_token: 'access_token', refresh_token: 'refresh_token'} }
7
+
8
+ subject { Myob::Essentials::Api::Client.new(params) }
9
+
10
+ describe ".account_type.all_items" do
11
+ let(:tax_types_response) { File.read('spec/fixtures/account/types.json') }
12
+
13
+ before { stub_request(:get, "https://api.myob.com/au/essentials/account/types").to_return(:status => 200, :body => tax_types_response, :headers => {}) }
14
+
15
+ it "fetches the list of account types" do
16
+ expect(subject.account_type.all_items).to eql(JSON.parse(tax_types_response)["items"])
17
+ end
18
+ end
19
+
20
+ describe ".account_type.find" do
21
+ let(:tax_type_uid) { '1' }
22
+ let(:tax_type_response) { File.read("spec/fixtures/account/types/#{tax_type_uid}.json") }
23
+
24
+ before { stub_request(:get, "https://api.myob.com/au/essentials/account/types/#{tax_type_uid}").to_return(:status => 200, :body => tax_type_response, :headers => {}) }
25
+
26
+ it "fetches an account type by uid" do
27
+ expect(subject.account_type.find(tax_type_uid)).to eql(JSON.parse(tax_type_response))
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe Myob::Essentials::Api::Model::Base do
4
+
5
+ let(:consumer) { {key: 'key', secret: 'secret'} }
6
+ let(:endpoint) { 'au' }
7
+ let(:params) { {redirect_uri: 'redirect_uri', consumer: consumer, access_token: 'access_token', refresh_token: 'refresh_token', endpoint: endpoint} }
8
+ let(:client) { Myob::Essentials::Api::Client.new(params) }
9
+
10
+ subject { Myob::Essentials::Api::Model::Base.new(client, 'base') }
11
+
12
+ describe ".get" do
13
+ let(:response_hash) { {'key' => 'value'} }
14
+
15
+ context 'AU endpoint' do
16
+ let(:endpoint) { 'au' }
17
+ before { stub_request(:get, "https://api.myob.com/au/essentials/base").with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
18
+ .to_return(:status => 200, :body => response_hash.to_json, :headers => {}) }
19
+
20
+ it "gets all the objects from AU endpoint" do
21
+ expect(subject.get).to eql(response_hash)
22
+ end
23
+ end
24
+
25
+ context 'NZ endpoint' do
26
+ let(:endpoint) { 'nz' }
27
+ before { stub_request(:get, "https://api.myob.com/nz/essentials/base").with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
28
+ .to_return(:status => 200, :body => response_hash.to_json, :headers => {}) }
29
+
30
+ it "gets all the objects from NZ endpoint" do
31
+ expect(subject.get).to eql(response_hash)
32
+ end
33
+ end
34
+ end
35
+
36
+ describe ".find" do
37
+ let(:response_hash) { {'key' => 'value'} }
38
+
39
+ before { stub_request(:get, "https://api.myob.com/au/essentials/base/1").with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
40
+ .to_return(:status => 200, :body => response_hash.to_json, :headers => {}) }
41
+
42
+ it "gets the object" do
43
+ expect(subject.find('1')).to eql(response_hash)
44
+ end
45
+ end
46
+
47
+ describe ".save" do
48
+ let(:base_hash) { {'key' => 'value'} }
49
+
50
+ context 'create a new object' do
51
+ before { stub_request(:post, "https://api.myob.com/au/essentials/base")
52
+ .with(:body => base_hash.to_json, :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
53
+ .to_return(:status => 200, :body => {}.to_json, :headers => {}) }
54
+
55
+ it "posts the object" do
56
+ subject.save(base_hash)
57
+ end
58
+ end
59
+
60
+ context 'update an existing object' do
61
+ let(:uid) { '892341' }
62
+
63
+ before { base_hash['uid'] = uid }
64
+ before { stub_request(:put, "https://api.myob.com/au/essentials/base/#{uid}")
65
+ .with(:body => base_hash.to_json, :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
66
+ .to_return(:status => 200, :body => {}.to_json, :headers => {}) }
67
+
68
+ it "puts the object" do
69
+ subject.save(base_hash)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe ".all_items" do
75
+ let(:page1) { File.read('spec/fixtures/base1.json') }
76
+ let(:page2) { File.read('spec/fixtures/base2.json') }
77
+ let(:page3) { File.read('spec/fixtures/base3.json') }
78
+
79
+ before do
80
+ stub_request(:get, "https://api.myob.com/au/essentials/base").with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
81
+ .to_return(:status => 200, :body => page1, :headers => {})
82
+ stub_request(:get, "https://api.myob.com/au/essentials/base?page=2&size=2").with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
83
+ .to_return(:status => 200, :body => page2, :headers => {})
84
+ stub_request(:get, "https://api.myob.com/au/essentials/base?page=3&size=2").with(:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json'})
85
+ .to_return(:status => 200, :body => page3, :headers => {})
86
+ end
87
+
88
+
89
+ it "fetches all the paginated objects" do
90
+ expect(subject.all_items).to eql([
91
+ {"uid"=>"1", "_links"=>[{"rel"=>"self", "href"=>"https://api.myob.com/au/essentials/base/1"}]},
92
+ {"uid"=>"2", "_links"=>[{"rel"=>"self", "href"=>"https://api.myob.com/au/essentials/base/2"}]},
93
+ {"uid"=>"3", "_links"=>[{"rel"=>"self", "href"=>"https://api.myob.com/au/essentials/base/3"}]},
94
+ {"uid"=>"4", "_links"=>[{"rel"=>"self", "href"=>"https://api.myob.com/au/essentials/base/4"}]},
95
+ {"uid"=>"5", "_links"=>[{"rel"=>"self", "href"=>"https://api.myob.com/au/essentials/base/5"}]}
96
+ ])
97
+ end
98
+ end
99
+
100
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Myob::Essentials::Api::Model::Business do
4
+
5
+ let(:consumer) { {key: 'key', secret: 'secret'} }
6
+ let(:params) { {consumer: consumer, access_token: 'access_token', refresh_token: 'refresh_token'} }
7
+
8
+ subject { Myob::Essentials::Api::Client.new(params) }
9
+
10
+ describe ".business.all_items" do
11
+ let(:businesses_response) { File.read('spec/fixtures/businesses.json') }
12
+
13
+ before { stub_request(:get, "https://api.myob.com/au/essentials/businesses").to_return(:status => 200, :body => businesses_response, :headers => {}) }
14
+
15
+ it "fetches the list of businesses" do
16
+ expect(subject.business.all_items).to eql(JSON.parse(businesses_response)["items"])
17
+ end
18
+ end
19
+
20
+ describe ".business.find" do
21
+ let(:business_uid) { '168254' }
22
+ let(:business_response) { File.read("spec/fixtures/businesses/#{business_uid}.json") }
23
+
24
+ before { stub_request(:get, "https://api.myob.com/au/essentials/businesses/#{business_uid}").to_return(:status => 200, :body => business_response, :headers => {}) }
25
+
26
+ it "fetches a business by uid" do
27
+ expect(subject.business.find(business_uid)).to eql(JSON.parse(business_response))
28
+ end
29
+ end
30
+
31
+ end