myob-essentials-api 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 11e3b86876568aa61bff577a0843a04ab3ef3595
4
+ data.tar.gz: d394f5559b06038a4a2d65cc9f965e529469e040
5
+ SHA512:
6
+ metadata.gz: e7d96c886395485c5ae9abf13ab644a720834114f3eeb13219ba55942fcbe3ff74301f5e834f19d336363d51018686f2149a0a732450f5b2fff3b230ddfa03a6
7
+ data.tar.gz: 19e4075bc5bf100903a4d79d8a32f9814baf43311fa11e5448ed08fd9913e91d7b929f826a1434d9de6fc7bbf27c235abda4eb4863de87d56668411f2b63440d
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1 @@
1
+ ruby-2.2.0@myob-essentials-api
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+
3
+ language: ruby
4
+
5
+ cache: bundler
6
+
7
+ rvm:
8
+ - 2.2.0
9
+ - 2.1.3
10
+ - 2.0.0
11
+ - 1.9.3
12
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "oauth2"
4
+
5
+ group :development do
6
+ gem "jeweler"
7
+ gem "simplecov"
8
+ end
9
+
10
+ group :test do
11
+ gem "rspec"
12
+ gem "webmock"
13
+ gem "timecop"
14
+ end
@@ -0,0 +1,86 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.6)
5
+ builder (3.2.2)
6
+ crack (0.4.2)
7
+ safe_yaml (~> 1.0.0)
8
+ descendants_tracker (0.0.4)
9
+ thread_safe (~> 0.3, >= 0.3.1)
10
+ diff-lcs (1.2.5)
11
+ docile (1.1.5)
12
+ faraday (0.9.1)
13
+ multipart-post (>= 1.2, < 3)
14
+ git (1.2.9.1)
15
+ github_api (0.12.2)
16
+ addressable (~> 2.3)
17
+ descendants_tracker (~> 0.0.4)
18
+ faraday (~> 0.8, < 0.10)
19
+ hashie (>= 3.3)
20
+ multi_json (>= 1.7.5, < 2.0)
21
+ nokogiri (~> 1.6.3)
22
+ oauth2
23
+ hashie (3.3.2)
24
+ highline (1.6.21)
25
+ jeweler (2.0.1)
26
+ builder
27
+ bundler (>= 1.0)
28
+ git (>= 1.2.5)
29
+ github_api
30
+ highline (>= 1.6.15)
31
+ nokogiri (>= 1.5.10)
32
+ rake
33
+ rdoc
34
+ jwt (1.2.1)
35
+ mini_portile (0.6.2)
36
+ multi_json (1.10.1)
37
+ multi_xml (0.5.5)
38
+ multipart-post (2.0.0)
39
+ nokogiri (1.6.6.2)
40
+ mini_portile (~> 0.6.0)
41
+ nokogiri (1.6.6.2-java)
42
+ oauth2 (1.0.0)
43
+ faraday (>= 0.8, < 0.10)
44
+ jwt (~> 1.0)
45
+ multi_json (~> 1.3)
46
+ multi_xml (~> 0.5)
47
+ rack (~> 1.2)
48
+ rack (1.6.0)
49
+ rake (10.4.2)
50
+ rdoc (4.2.0)
51
+ rspec (3.1.0)
52
+ rspec-core (~> 3.1.0)
53
+ rspec-expectations (~> 3.1.0)
54
+ rspec-mocks (~> 3.1.0)
55
+ rspec-core (3.1.7)
56
+ rspec-support (~> 3.1.0)
57
+ rspec-expectations (3.1.2)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.1.0)
60
+ rspec-mocks (3.1.3)
61
+ rspec-support (~> 3.1.0)
62
+ rspec-support (3.1.2)
63
+ safe_yaml (1.0.4)
64
+ simplecov (0.9.1)
65
+ docile (~> 1.1.0)
66
+ multi_json (~> 1.0)
67
+ simplecov-html (~> 0.8.0)
68
+ simplecov-html (0.8.0)
69
+ thread_safe (0.3.4)
70
+ thread_safe (0.3.4-java)
71
+ timecop (0.7.1)
72
+ webmock (1.20.4)
73
+ addressable (>= 2.3.6)
74
+ crack (>= 0.3.2)
75
+
76
+ PLATFORMS
77
+ java
78
+ ruby
79
+
80
+ DEPENDENCIES
81
+ jeweler
82
+ oauth2
83
+ rspec
84
+ simplecov
85
+ timecop
86
+ webmock
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Bruno
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,188 @@
1
+ myob-essentials-api
2
+ ===============
3
+
4
+ [![Build Status](https://secure.travis-ci.org/BrunoChauvet/myob-essentials-api.png?branch=master)](http://travis-ci.org/BrunoChauvet/myob-essentials-api)
5
+
6
+ ## MYOB Essentials Accounting
7
+
8
+ Integrate with ![MYOB Essentials Accounting](http://developer.myob.com/api/essentials-accounting/)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ `gem 'myob-essentials-api'`
15
+
16
+ And then execute:
17
+
18
+ `bundle`
19
+
20
+ Or install it yourself as:
21
+
22
+ `gem install myob-essentials-api`
23
+
24
+ ## Usage
25
+
26
+ ### OAuth Authentication
27
+
28
+ If you've already got an OAuth access token, feel free to skip to API Client Setup.
29
+
30
+ The MYOB API uses 3 legged OAuth2. If you don't want to roll your own, or use the [OmniAuth strategy](https://github.com/davidlumley/omniauth-myob) you can authenticate using the `get_access_code_url` and `get_access_token` methods that [ghiculescu](https://github.com/ghiculescu) has provided like so:
31
+
32
+
33
+ ``` ruby
34
+ class MYOBSessionController
35
+ def new
36
+ redirect_to myob_client.get_access_code_url
37
+ end
38
+
39
+ def create
40
+ @token = myob_client.get_access_token(params[:code])
41
+ @businesses = myob_client.business.all_items
42
+ # then show the user a view of their available businesses
43
+ end
44
+
45
+ def myob_client
46
+ @api_client = Myob::Essentials::Api::Client.new({
47
+ consumer: {
48
+ key: YOUR_CONSUMER_KEY,
49
+ secret: YOUR_CONSUMER_SECRET,
50
+ },
51
+ redirect_uri: callback_create_url
52
+ })
53
+ end
54
+ end
55
+ ```
56
+
57
+ ### API Client Setup
58
+
59
+ #### Create an api_client
60
+
61
+ ``` ruby
62
+ api_client = Myob::Essentials::Api::Client.new({
63
+ consumer: {
64
+ key: YOUR_CONSUMER_KEY,
65
+ secret: YOUR_CONSUMER_SECRET,
66
+ },
67
+ access_token: YOUR_OAUTH_ACCESS_TOKEN
68
+ })
69
+ ```
70
+
71
+ If you have a refresh token (the Myob API returns one by default) you can use that too:
72
+
73
+ ``` ruby
74
+ api_client = Myob::Essentials::Api::Client.new({
75
+ consumer: {
76
+ key: YOUR_CONSUMER_KEY,
77
+ secret: YOUR_CONSUMER_SECRET,
78
+ },
79
+ access_token: YOUR_OAUTH_ACCESS_TOKEN,
80
+ refresh_token: YOUR_OAUTH_REFRESH_TOKEN
81
+ })
82
+ ```
83
+
84
+ Or if you know which Business UID you want to access too:
85
+
86
+ ``` ruby
87
+ api_client = Myob::Essentials::Api::Client.new({
88
+ consumer: {
89
+ key: YOUR_CONSUMER_KEY,
90
+ secret: YOUR_CONSUMER_SECRET,
91
+ },
92
+ access_token: YOUR_OAUTH_ACCESS_TOKEN,
93
+ refresh_token: YOUR_OAUTH_REFRESH_TOKEN,
94
+ business_uid: BUSINESS_UID
95
+ })
96
+ ```
97
+
98
+ The available options when creating the MYOB Essentials API client are
99
+
100
+ * redirect_uri: URI to redirect the user to after OAuth authentication
101
+ * consumer: Hash containing your OAuth key and secret
102
+ * access_token: Previously fetched access token
103
+ * refresh_token: Previously fetched refresh token
104
+ * business_uid: Previously selected business uid
105
+ * auto_refresh: Automatically refresh the access_token if expired (default `true`)
106
+ * endpoint: `au` or `nz` (default `au` for https://api.myob.com/au/essentials)
107
+
108
+ #### Refresh access token
109
+
110
+ The OAuth access token can be refreshed at any time by calling
111
+ `client.refresh!`
112
+
113
+ ### API Methods
114
+
115
+ #### get
116
+
117
+ Retrieves the first page of specified collection
118
+
119
+ ```ruby
120
+ contacts = api_client.contact.get
121
+ ```
122
+
123
+ #### next_page / previous_page
124
+
125
+ Retrieves the next/previous page of specified collection. A call to `get` must have been performed first
126
+
127
+ ```ruby
128
+ contacts = api_client.contact.get
129
+ next_contacts = api_client.contact.next_page
130
+ previous_contacts = api_client.contact.previous_page
131
+ ```
132
+
133
+ #### find
134
+
135
+ Retrieves a single element by uid
136
+
137
+ ```ruby
138
+ contact = api_client.contact.find('123')
139
+ ```
140
+
141
+ #### save
142
+
143
+ Saves a resource. The resource is `created` if no `uid` is specified, otherwise it is `updated`
144
+
145
+ ```ruby
146
+ contact = api_client.contact.save({'uid' => '123', 'key' => 'value'})
147
+ ```
148
+
149
+ #### all_items
150
+
151
+ Fetches the entire collection of elements
152
+
153
+ ```ruby
154
+ contact = api_client.contact.all_items
155
+ ```
156
+
157
+ ### API Resources
158
+
159
+ #### Businesses
160
+ Before using the majority of API methods you will need to have selected a Business UID. If you've already selected one when creating the client, feel free to ignore this.
161
+ ```ruby
162
+ businesses = api_client.business.all_items
163
+ api_client.business_uid = businesses[0]['uid']
164
+ ```
165
+
166
+ #### Account Classifications
167
+ `api_client.account_classification.get`
168
+
169
+ #### Account Types
170
+ `api_client.account_type.get`
171
+
172
+ #### Tax Types
173
+ `api_client.tax_type.get`
174
+
175
+ #### Contacts
176
+ `api_client.contact.get`
177
+
178
+ #### Accounts
179
+ `api_client.account.get`
180
+
181
+ #### Inventory Items
182
+ `api_client.inventory_item.get`
183
+
184
+ #### Sale Invoices
185
+ `api_client.sale_invoice.get`
186
+
187
+ #### Sale Payments
188
+ `api_client.sale_payment.get`
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "myob-essentials-api"
18
+ gem.homepage = "http://github.com/BrunoChauvet/myob-essentials-api"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Integrate with MYOB Essentials Accounting}
21
+ gem.description = %Q{Integrate with MYOB Essentials Accounting}
22
+ gem.email = "it@maestrano.com"
23
+ gem.authors = ["BrunoChauvet"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ desc "Code coverage detail"
36
+ task :simplecov do
37
+ ENV['COVERAGE'] = "true"
38
+ Rake::Task['test'].execute
39
+ end
40
+
41
+ task :default => :test
42
+
43
+ require 'rdoc/task'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "myob-essentials-api #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,13 @@
1
+ require 'myob/essentials/api/helper.rb'
2
+ require 'myob/essentials/api/client.rb'
3
+ require 'myob/essentials/api/model/base.rb'
4
+ require 'myob/essentials/api/model/business.rb'
5
+ require 'myob/essentials/api/model/tax_type.rb'
6
+ require 'myob/essentials/api/model/account_type.rb'
7
+ require 'myob/essentials/api/model/account_classification.rb'
8
+ require 'myob/essentials/api/model/contact.rb'
9
+ require 'myob/essentials/api/model/inventory_item.rb'
10
+ require 'myob/essentials/api/model/account.rb'
11
+ require 'myob/essentials/api/model/account_balance.rb'
12
+ require 'myob/essentials/api/model/sale_invoice.rb'
13
+ require 'myob/essentials/api/model/sale_payment.rb'
@@ -0,0 +1,86 @@
1
+ require 'base64'
2
+ require 'oauth2'
3
+
4
+ module Myob
5
+ module Essentials
6
+ module Api
7
+ class Client
8
+ include Myob::Essentials::Api::Helpers
9
+
10
+ attr_reader :client, :access_token, :refresh_token, :expires_at, :endpoint
11
+ attr_accessor :business_uid
12
+
13
+ def initialize(options)
14
+ model :Business
15
+ model :TaxType
16
+ model :AccountType
17
+ model :AccountClassification
18
+ model :Contact
19
+ model :InventoryItem
20
+ model :Account
21
+ model :AccountBalance
22
+ model :SaleInvoice
23
+ model :SalePayment
24
+
25
+ @redirect_uri = options[:redirect_uri]
26
+ @consumer = options[:consumer]
27
+ @access_token = options[:access_token]
28
+ @refresh_token = options[:refresh_token]
29
+ @auto_refresh = options[:auto_refresh] || true
30
+ @endpoint = options[:endpoint] || 'au'
31
+ @business_uid = options[:business_uid]
32
+
33
+ @client = OAuth2::Client.new(@consumer[:key], @consumer[:secret], {
34
+ :site => 'https://secure.myob.com',
35
+ :authorize_url => '/oauth2/account/authorize',
36
+ :token_url => '/oauth2/v1/authorize',
37
+ })
38
+ end
39
+
40
+ def get_access_code_url(params = {})
41
+ @client.auth_code.authorize_url(params.merge(scope: 'la.global', redirect_uri: @redirect_uri))
42
+ end
43
+
44
+ def get_access_token(access_code)
45
+ @token = @client.auth_code.get_token(access_code, redirect_uri: @redirect_uri)
46
+ @access_token = @token.token
47
+ @expires_at = @token.expires_at
48
+ @refresh_token = @token.refresh_token
49
+ @token
50
+ end
51
+
52
+ def headers
53
+ {
54
+ 'x-myobapi-key' => @consumer[:key],
55
+ 'x-myobapi-version' => 'v0',
56
+ 'Accept' => 'application/json',
57
+ 'Content-Type' => 'application/json'
58
+ }
59
+ end
60
+
61
+ def refresh!
62
+ @auth_connection ||= OAuth2::AccessToken.new(@client, @access_token, {refresh_token: @refresh_token})
63
+
64
+ @token = @auth_connection.refresh!
65
+ @access_token = @token.token
66
+ @expires_at = @token.expires_at
67
+ @refresh_token = @token.refresh_token
68
+ @token
69
+ end
70
+
71
+ def connection
72
+ @auth_connection ||= begin
73
+ if @refresh_token
74
+ OAuth2::AccessToken.new(@client, @access_token, {refresh_token: @refresh_token})
75
+ else
76
+ OAuth2::AccessToken.new(@client, @access_token)
77
+ end
78
+ end
79
+ refresh! if @auto_refresh && @expires_at && @expires_at < Time.now.to_i
80
+ @auth_connection
81
+ end
82
+
83
+ end
84
+ end
85
+ end
86
+ end