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,29 @@
1
+ class String
2
+ def underscore
3
+ self.gsub(/::/, '/').
4
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
+ tr("-", "_").
7
+ downcase
8
+ end
9
+ end
10
+
11
+ module Myob
12
+ module Essentials
13
+ module Api
14
+ module Helpers
15
+ def model(model_name)
16
+ method_name = model_name.to_s.underscore
17
+ variable_name = "@#{method_name}_model".to_sym
18
+ unless instance_variable_defined?(variable_name)
19
+ instance_variable_set(variable_name, Myob::Essentials::Api::Model.const_get("#{model_name}".to_sym).new(self, model_name.to_s))
20
+ self.define_singleton_method(method_name.to_sym) do
21
+ instance_variable_get(variable_name)
22
+ end
23
+ end
24
+ instance_variable_get(variable_name)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class Account < Base
6
+ def model_route
7
+ 'businesses/:business_uid/generalledger/accounts'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class AccountBalance < Base
6
+ def model_route
7
+ 'businesses/:business_uid/generalledger/accounts/balances'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class AccountClassification < Base
6
+ def model_route
7
+ 'account/classifications'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class AccountType < Base
6
+ def model_route
7
+ 'account/types'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,114 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class Base
6
+
7
+ def initialize(client, model_name=nil)
8
+ @client = client
9
+ @model_name = model_name || 'Base'
10
+
11
+ @links = nil
12
+ end
13
+
14
+ def model_route
15
+ @model_name.to_s.downcase
16
+ end
17
+
18
+ def get
19
+ perform_request(url)
20
+ end
21
+
22
+ def find(id)
23
+ object = { 'uid' => id }
24
+ perform_request(url(object))
25
+ end
26
+
27
+ def save(object)
28
+ new_record?(object) ? create(object) : update(object)
29
+ end
30
+
31
+ def destroy(object)
32
+ @client.connection.delete(url(object), :headers => @client.headers)
33
+ end
34
+
35
+ def all_items
36
+ results = get["items"]
37
+ while link('next')
38
+ results += next_page["items"] || []
39
+ end
40
+ results
41
+ end
42
+
43
+ def next_page
44
+ perform_request(link('next')) if link('next')
45
+ end
46
+
47
+ def previous_page
48
+ perform_request(link('previous')) if link('previous')
49
+ end
50
+
51
+ protected
52
+ def url(object = nil)
53
+ if model_route == ''
54
+ "https://api.myob.com/#{@client.endpoint}/essentials"
55
+ elsif object && object['uid']
56
+ "#{resource_url}/#{object['uid']}"
57
+ else
58
+ resource_url
59
+ end
60
+ end
61
+
62
+ def new_record?(object)
63
+ object["uid"].nil? || object["uid"] == ""
64
+ end
65
+
66
+ def create(object)
67
+ object = typecast(object)
68
+ @client.connection.post(url, {:headers => @client.headers, :body => object.to_json})
69
+ end
70
+
71
+ def update(object)
72
+ object = typecast(object)
73
+ @client.connection.put(url(object), {:headers => @client.headers, :body => object.to_json})
74
+ end
75
+
76
+ def typecast(object)
77
+ returned_object = object.dup # don't change the original object
78
+
79
+ returned_object.each do |key, value|
80
+ if value.respond_to?(:strftime)
81
+ returned_object[key] = value.strftime(date_formatter)
82
+ end
83
+ end
84
+
85
+ returned_object
86
+ end
87
+
88
+ def link(name)
89
+ return nil unless @links
90
+ link_hash = @links.select{|link_hash| link_hash['rel'] == name}.first
91
+ link_hash ? link_hash['href'] : nil
92
+ end
93
+
94
+ def date_formatter
95
+ "%Y-%m-%dT%H:%M:%S"
96
+ end
97
+
98
+ def resource_url
99
+ url = "https://api.myob.com/#{@client.endpoint}/essentials/#{model_route}"
100
+ url.gsub!(':business_uid', @client.business_uid) if @client.business_uid
101
+ url
102
+ end
103
+
104
+ def perform_request(url)
105
+ response = @client.connection.get(url, {:headers => @client.headers})
106
+ hash = JSON.parse(response.body)
107
+ @links = hash['_links']
108
+ hash
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class Business < Base
6
+ def model_route
7
+ 'businesses'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class Contact < Base
6
+ def model_route
7
+ 'businesses/:business_uid/contacts'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class InventoryItem < Base
6
+ def model_route
7
+ 'businesses/:business_uid/inventory/items'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class SaleInvoice < Base
6
+ def model_route
7
+ 'businesses/:business_uid/sale/invoices'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class SalePayment < Base
6
+ def model_route
7
+ 'businesses/:business_uid/sale/payments'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Myob
2
+ module Essentials
3
+ module Api
4
+ module Model
5
+ class TaxType < Base
6
+ def model_route
7
+ 'tax/types'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,102 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: myob-essentials-api 0.0.1 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "myob-essentials-api"
9
+ s.version = "0.0.1"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["BrunoChauvet"]
14
+ s.date = "2015-01-27"
15
+ s.description = "Integrate with MYOB Essentials Accounting"
16
+ s.email = "it@maestrano.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".rspec",
24
+ ".ruby-version",
25
+ ".travis.yml",
26
+ "Gemfile",
27
+ "Gemfile.lock",
28
+ "LICENSE",
29
+ "README.md",
30
+ "Rakefile",
31
+ "VERSION",
32
+ "lib/myob-essentials-api.rb",
33
+ "lib/myob/essentials/api/client.rb",
34
+ "lib/myob/essentials/api/helper.rb",
35
+ "lib/myob/essentials/api/model/account.rb",
36
+ "lib/myob/essentials/api/model/account_balance.rb",
37
+ "lib/myob/essentials/api/model/account_classification.rb",
38
+ "lib/myob/essentials/api/model/account_type.rb",
39
+ "lib/myob/essentials/api/model/base.rb",
40
+ "lib/myob/essentials/api/model/business.rb",
41
+ "lib/myob/essentials/api/model/contact.rb",
42
+ "lib/myob/essentials/api/model/inventory_item.rb",
43
+ "lib/myob/essentials/api/model/sale_invoice.rb",
44
+ "lib/myob/essentials/api/model/sale_payment.rb",
45
+ "lib/myob/essentials/api/model/tax_type.rb",
46
+ "myob-essentials-api.gemspec",
47
+ "spec/fixtures/account/classifications.json",
48
+ "spec/fixtures/account/classifications/1.json",
49
+ "spec/fixtures/account/types.json",
50
+ "spec/fixtures/account/types/1.json",
51
+ "spec/fixtures/base1.json",
52
+ "spec/fixtures/base2.json",
53
+ "spec/fixtures/base3.json",
54
+ "spec/fixtures/businesses.json",
55
+ "spec/fixtures/businesses/168254.json",
56
+ "spec/fixtures/businesses/168254/contacts.json",
57
+ "spec/fixtures/businesses/168254/contacts/3604150.json",
58
+ "spec/fixtures/businesses/168254/generalledger/accounts.json",
59
+ "spec/fixtures/businesses/168254/generalledger/accounts/10108109.json",
60
+ "spec/fixtures/businesses/168254/inventory/items.json",
61
+ "spec/fixtures/businesses/168254/inventory/items/987816.json",
62
+ "spec/fixtures/businesses/168254/sale/invoices.json",
63
+ "spec/fixtures/businesses/168254/sale/invoices/35608735.json",
64
+ "spec/fixtures/tax/types.json",
65
+ "spec/fixtures/tax/types/5.json",
66
+ "spec/myob/essentials/api/client_spec.rb",
67
+ "spec/myob/essentials/api/model/account_classification_spec.rb",
68
+ "spec/myob/essentials/api/model/account_spec.rb",
69
+ "spec/myob/essentials/api/model/account_type_spec.rb",
70
+ "spec/myob/essentials/api/model/base_spec.rb",
71
+ "spec/myob/essentials/api/model/business_spec.rb",
72
+ "spec/myob/essentials/api/model/contact_spec.rb",
73
+ "spec/myob/essentials/api/model/inventory_item_spec.rb",
74
+ "spec/myob/essentials/api/model/sale_invoice_spec.rb",
75
+ "spec/myob/essentials/api/model/sale_payment_spec.rb",
76
+ "spec/myob/essentials/api/model/tax_type_spec.rb",
77
+ "spec/spec_helper.rb"
78
+ ]
79
+ s.homepage = "http://github.com/BrunoChauvet/myob-essentials-api"
80
+ s.licenses = ["MIT"]
81
+ s.rubygems_version = "2.4.5"
82
+ s.summary = "Integrate with MYOB Essentials Accounting"
83
+
84
+ if s.respond_to? :specification_version then
85
+ s.specification_version = 4
86
+
87
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
88
+ s.add_runtime_dependency(%q<oauth2>, [">= 0"])
89
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
90
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
91
+ else
92
+ s.add_dependency(%q<oauth2>, [">= 0"])
93
+ s.add_dependency(%q<jeweler>, [">= 0"])
94
+ s.add_dependency(%q<simplecov>, [">= 0"])
95
+ end
96
+ else
97
+ s.add_dependency(%q<oauth2>, [">= 0"])
98
+ s.add_dependency(%q<jeweler>, [">= 0"])
99
+ s.add_dependency(%q<simplecov>, [">= 0"])
100
+ end
101
+ end
102
+
@@ -0,0 +1,104 @@
1
+ {
2
+ "_links": [
3
+ {
4
+ "rel": "self",
5
+ "href": "https://api.myob.com/au/essentials/account/classifications"
6
+ }
7
+ ],
8
+ "items": [
9
+ {
10
+ "uid": "1",
11
+ "uri": "https://api.myob.com/au/essentials/account/classifications/1",
12
+ "name": "Asset",
13
+ "_links": [
14
+ {
15
+ "rel": "self",
16
+ "href": "https://api.myob.com/au/essentials/account/classifications/1"
17
+ }
18
+ ]
19
+ },
20
+ {
21
+ "uid": "2",
22
+ "uri": "https://api.myob.com/au/essentials/account/classifications/2",
23
+ "name": "Liability",
24
+ "_links": [
25
+ {
26
+ "rel": "self",
27
+ "href": "https://api.myob.com/au/essentials/account/classifications/2"
28
+ }
29
+ ]
30
+ },
31
+ {
32
+ "uid": "3",
33
+ "uri": "https://api.myob.com/au/essentials/account/classifications/3",
34
+ "name": "Equity",
35
+ "_links": [
36
+ {
37
+ "rel": "self",
38
+ "href": "https://api.myob.com/au/essentials/account/classifications/3"
39
+ }
40
+ ]
41
+ },
42
+ {
43
+ "uid": "4",
44
+ "uri": "https://api.myob.com/au/essentials/account/classifications/4",
45
+ "name": "Income",
46
+ "_links": [
47
+ {
48
+ "rel": "self",
49
+ "href": "https://api.myob.com/au/essentials/account/classifications/4"
50
+ }
51
+ ]
52
+ },
53
+ {
54
+ "uid": "5",
55
+ "uri": "https://api.myob.com/au/essentials/account/classifications/5",
56
+ "name": "Cost Of Goods Sold",
57
+ "_links": [
58
+ {
59
+ "rel": "self",
60
+ "href": "https://api.myob.com/au/essentials/account/classifications/5"
61
+ }
62
+ ]
63
+ },
64
+ {
65
+ "uid": "6",
66
+ "uri": "https://api.myob.com/au/essentials/account/classifications/6",
67
+ "name": "Expense",
68
+ "_links": [
69
+ {
70
+ "rel": "self",
71
+ "href": "https://api.myob.com/au/essentials/account/classifications/6"
72
+ }
73
+ ]
74
+ },
75
+ {
76
+ "uid": "8",
77
+ "uri": "https://api.myob.com/au/essentials/account/classifications/8",
78
+ "name": "Other Income",
79
+ "_links": [
80
+ {
81
+ "rel": "self",
82
+ "href": "https://api.myob.com/au/essentials/account/classifications/8"
83
+ }
84
+ ]
85
+ },
86
+ {
87
+ "uid": "9",
88
+ "uri": "https://api.myob.com/au/essentials/account/classifications/9",
89
+ "name": "Other Expenses",
90
+ "_links": [
91
+ {
92
+ "rel": "self",
93
+ "href": "https://api.myob.com/au/essentials/account/classifications/9"
94
+ }
95
+ ]
96
+ }
97
+ ],
98
+ "page": {
99
+ "size": 8,
100
+ "totalElements": 8,
101
+ "totalPages": 1,
102
+ "number": 1
103
+ }
104
+ }