myob-advanced-api 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: adead9767a1fc9b00e63a9f79d33249b9ea993652881b612bd8d35ea863a4663
4
+ data.tar.gz: 30c9d78d02bec44dbe5ed4d8054651aa0d780b9c35fe074df88c50cfd9a7a81f
5
+ SHA512:
6
+ metadata.gz: 29e8c436c6cd7287d0fcca4748d49911d6af232d76f3d3c611fe95e9c7f411a4ef7b755fb983938d906d26cfe15c9b2762470727b689a18af722ca11cd69e0d2
7
+ data.tar.gz: fce5283cad4e403486a6dc7e8bc6211b13ffa3e0579f7bef0c438497d3cf58271cf4b81a83700e77041a66f4c983597069c4ccbdb2c021508adf58d39046a16b
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1 @@
1
+ myob-advanced-api
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in myob-advanced-api.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2020 Van Nguyen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ # MYOB Advanced API
2
+
3
+ [MYOB Advanced Api](https://github.com/vannguyenvietnam/myob-advanced-api) inherited the structure of [MYOB Api](https://github.com/davidlumley/myob-api) is an interface for accessing [MYOB](https://developer.myob.com/api/advanced/)'s Advanced instance.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'myob-advanced-api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install myob-advanced-api
18
+
19
+ ## Contributing
20
+
21
+ 1. Fork it
22
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
23
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
24
+ 4. Push to the branch (`git push origin my-new-feature`)
25
+ 5. Create new Pull Request
26
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ require 'myob_advanced/api/version'
2
+ require 'myob_advanced/api/helpers'
3
+
4
+ require 'myob_advanced/api/models/base'
5
+ require 'myob_advanced/api/models/custom'
6
+
7
+ require 'myob_advanced/api/models/account'
8
+ require 'myob_advanced/api/models/payment_method'
9
+
10
+ require 'myob_advanced/api/models/customer'
11
+ require 'myob_advanced/api/models/customer_class'
12
+ require 'myob_advanced/api/models/contact'
13
+ require 'myob_advanced/api/models/customer_payment_method'
14
+
15
+ require 'myob_advanced/api/models/invoice'
16
+ require 'myob_advanced/api/models/sales_invoice'
17
+ require 'myob_advanced/api/models/payment'
18
+
19
+ require 'myob_advanced/api/models/vendor'
20
+ require 'myob_advanced/api/models/vendor_class'
21
+ require 'myob_advanced/api/models/bill'
22
+ require 'myob_advanced/api/models/check'
23
+
24
+ require 'myob_advanced/api/client'
@@ -0,0 +1,81 @@
1
+ require 'base64'
2
+ require 'oauth2'
3
+
4
+ module MyobAdvanced
5
+ module Api
6
+ class Client
7
+ include MyobAdvanced::Api::Helpers
8
+
9
+ attr_reader :client
10
+
11
+ def initialize(options)
12
+ @redirect_uri = options[:redirect_uri]
13
+ @consumer = options[:consumer]
14
+ @access_token = options[:access_token]
15
+ @refresh_token = options[:refresh_token]
16
+ @site_url = options[:site_url]
17
+ @default_version = options[:default_version] # Default web servive enpoint version
18
+
19
+ # Init model methods
20
+ MyobAdvanced::Api::Model::Base.subclasses.each {|c| model(c.name.split("::").last)}
21
+
22
+ @client = OAuth2::Client.new(@consumer[:key], @consumer[:secret], {
23
+ :site => @site_url,
24
+ :authorize_url => '/identity/connect/authorize',
25
+ :token_url => '/identity/connect/token',
26
+ })
27
+ end
28
+
29
+ def default_api_url
30
+ "#{@site_url}/entity/Default/#{@default_version}"
31
+ end
32
+
33
+ def site_url
34
+ @site_url
35
+ end
36
+
37
+ def get_access_code_url(params = {})
38
+ scope = params[:scope] || 'api offline_access'
39
+ @client.auth_code.authorize_url(params.merge(scope: scope, redirect_uri: @redirect_uri))
40
+ end
41
+
42
+ def get_access_token(access_code)
43
+ @token = @client.auth_code.get_token(access_code, redirect_uri: @redirect_uri)
44
+ @access_token = @token.token
45
+ @expires_at = @token.expires_at
46
+ @refresh_token = @token.refresh_token
47
+ @token
48
+ end
49
+
50
+ def refresh_access_token!
51
+ @token = OAuth2::AccessToken.new(@client, @access_token, {
52
+ :refresh_token => @refresh_token
53
+ }).refresh!
54
+
55
+ @access_token = @token.token
56
+ @expires_at = @token.expires_at
57
+ @refresh_token = @token.refresh_token
58
+ @token
59
+ end
60
+
61
+ def headers
62
+ {
63
+ 'Accept' => 'application/json',
64
+ 'Content-Type' => 'application/json'
65
+ }
66
+ end
67
+
68
+ def connection
69
+ @auth_connection ||= OAuth2::AccessToken.new(@client, @access_token)
70
+ end
71
+
72
+ def enpoints
73
+ url = "#{@site_url}/entity"
74
+ response = connection.get(url, { headers: headers })
75
+ JSON.parse(response.body)
76
+ end
77
+
78
+ private
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,36 @@
1
+ module MyobAdvanced
2
+ module Api
3
+
4
+ module RefinedString
5
+ refine String do
6
+ def underscore
7
+ self.gsub(/::/, '/').
8
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
9
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
10
+ tr("-", "_").
11
+ downcase
12
+ end
13
+ end
14
+ end
15
+
16
+ module Helpers
17
+ using RefinedString
18
+
19
+ def model(model_name)
20
+ method_name = model_name.to_s.underscore
21
+ variable_name = "@#{method_name}_model".to_sym
22
+
23
+ unless instance_variable_defined?(variable_name)
24
+ instance_variable_set(variable_name, MyobAdvanced::Api::Model.const_get("#{model_name}".to_sym).new(self, model_name.to_s))
25
+
26
+ self.define_singleton_method(method_name.to_sym) do
27
+ instance_variable_get(variable_name)
28
+ end
29
+ end
30
+
31
+ instance_variable_get(variable_name)
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class Account < Base
5
+ def model_route
6
+ 'Account'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,160 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class Base
5
+ QUERY_OPTIONS = [:top, :skip, :filter, :expand, :select, :custom]
6
+
7
+ def initialize(client, model_name)
8
+ @client = client
9
+ @api_url = client.default_api_url
10
+ @model_name = model_name || 'Base'
11
+ end
12
+
13
+ def model_route
14
+ @model_name.to_s
15
+ end
16
+
17
+ def all(options = {})
18
+ perform_request(self.url(nil, options[:params]), options)
19
+ end
20
+ alias_method :get, :all
21
+
22
+ def find(id, options = {})
23
+ object = { 'ID' => id }
24
+ perform_request(self.url(object, options[:params]), options)
25
+ end
26
+
27
+ def find_by_path(sub_path, options = {})
28
+ object = { 'sub_path' => sub_path }
29
+ perform_request(self.url(object, options[:params]), options)
30
+ end
31
+
32
+ def first(options = {})
33
+ all(options).first
34
+ end
35
+
36
+ def destroy(id)
37
+ object = { 'ID' => id }
38
+ @client.connection.delete(self.url(object), :headers => @client.headers)
39
+ end
40
+
41
+ def destroy_by_path(sub_path)
42
+ object = { 'sub_path' => sub_path }
43
+ @client.connection.delete(self.url(object), :headers => @client.headers)
44
+ end
45
+
46
+ def exec_action(action, options = {})
47
+ options[:method] = 'post'
48
+ object = { 'sub_path' => action }
49
+ perform_request(self.url(object, options[:params]), options)
50
+ end
51
+
52
+ def put_attach(options = {})
53
+ options[:method] = 'put'
54
+ object = nil
55
+ object = { 'sub_path' => options[:sub_path] } if options[:sub_path]
56
+ perform_request(self.url(object, options[:params]), options)
57
+ end
58
+
59
+ def get_attach(options = {})
60
+ object = nil
61
+ object = { 'sub_path' => options[:sub_path] } if options[:sub_path]
62
+ perform_request(self.url(object, options[:params]), options)
63
+ end
64
+
65
+ def create(object, options = {})
66
+ object = typecast(object)
67
+ request_options = { body: object, method: 'put' }
68
+ perform_request(self.url(nil, options[:params]), request_options)
69
+ end
70
+
71
+ def update(object, options = {})
72
+ object = typecast(object)
73
+ request_options = { body: object, method: 'put' }
74
+ perform_request(self.url(nil, options[:params]), request_options)
75
+ end
76
+
77
+ def url(object = nil, params = nil)
78
+ @model_route ||= model_route
79
+ url = @api_url
80
+
81
+ if @model_route.present?
82
+ sub_path = nil
83
+ sub_path = "/#{object['ID']}" if object && object['ID']
84
+ sub_path = "/#{object['sub_path']}" if object && object['sub_path']
85
+ # Init url
86
+ url = "#{@api_url}/#{@model_route}#{sub_path}"
87
+ end
88
+
89
+ if params.is_a?(Hash)
90
+ query = query_string(params)
91
+ url += "?#{query}" if !query.nil? && query.length > 0
92
+ end
93
+
94
+ url
95
+ end
96
+
97
+ private
98
+
99
+ def typecast(object)
100
+ returned_object = object.dup # don't change the original object
101
+
102
+ returned_object.each do |key, value|
103
+ if value.respond_to?(:strftime)
104
+ returned_object[key] = value.strftime(date_formatter)
105
+ end
106
+ end
107
+
108
+ returned_object
109
+ end
110
+
111
+ def date_formatter
112
+ "%Y-%m-%dT%H:%M:%S"
113
+ end
114
+
115
+ def resource_url
116
+ "#{@api_url}/#{self.model_route}"
117
+ end
118
+
119
+ def perform_request(url, options = {})
120
+ request_options = { headers: @client.headers }
121
+ request_options[:body] = options[:body].to_json if options[:body]
122
+ method = options[:method] || 'get'
123
+ parse_response(@client.connection.send(method, url, request_options))
124
+ end
125
+
126
+ def query_string(params)
127
+ params.map do |key, value|
128
+ if QUERY_OPTIONS.include?(key)
129
+ value = build_filter(value) if key == :filter
130
+ key = "$#{key}"
131
+ end
132
+
133
+ "#{key}=#{CGI.escape(value.to_s)}"
134
+ end.join('&')
135
+ end
136
+
137
+ def build_filter(value)
138
+ return value unless value.is_a?(Hash)
139
+
140
+ temp = "\\\\'"
141
+ value.map { |key, value| "#{key} eq '#{value.to_s.gsub("'", temp)}'" }.join(' and ')
142
+ end
143
+
144
+ def parse_response(response)
145
+ return response.body unless response.body.present?
146
+
147
+ JSON.parse(response.body) rescue {}
148
+ end
149
+
150
+ def process_query(data, query)
151
+ query.each do |property, value|
152
+ data.select! {|x| x[property] == value}
153
+ end
154
+
155
+ data
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class Bill < Base
5
+ def model_route
6
+ 'Bill'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class Check < Base
5
+ def model_route
6
+ 'Check'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class Contact < Base
5
+ def model_route
6
+ 'Contact'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ # This model will be used to get data from custom enpoint
5
+ class Custom < Base
6
+ def model_route
7
+ ''
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class Customer < Base
5
+ def model_route
6
+ 'Customer'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class CustomerClass < Base
5
+ def model_route
6
+ 'CustomerClass'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class CustomerPaymentMethod < Base
5
+ def model_route
6
+ 'CustomerPaymentMethod'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class Invoice < Base
5
+ def model_route
6
+ 'Invoice'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class Payment < Base
5
+ def model_route
6
+ 'Payment'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class PaymentMethod < Base
5
+ def model_route
6
+ 'PaymentMethod'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class SalesInvoice < Base
5
+ def model_route
6
+ 'SalesInvoice'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class Vendor < Base
5
+ def model_route
6
+ 'Vendor'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ module Model
4
+ class VendorClass < Base
5
+ def model_route
6
+ 'VendorClass'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module MyobAdvanced
2
+ module Api
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'myob_advanced/api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "myob-advanced-api"
8
+ spec.version = MyobAdvanced::Api::VERSION
9
+ spec.authors = ["Van Nguyen"]
10
+ spec.email = ["bichvannguyenvnn@gmail.com"]
11
+ spec.description = %q{MYOB Advanced API}
12
+ spec.summary = %q{MYOB Advanced API}
13
+ spec.homepage = "https://github.com/vannguyenvietnam/myob-advanced-api"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "oauth2", "~> 1.4"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: myob-advanced-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Van Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: MYOB Advanced API
56
+ email:
57
+ - bichvannguyenvnn@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rbenv-gemsets"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/myob-advanced-api.rb
69
+ - lib/myob_advanced/api/client.rb
70
+ - lib/myob_advanced/api/helpers.rb
71
+ - lib/myob_advanced/api/models/account.rb
72
+ - lib/myob_advanced/api/models/base.rb
73
+ - lib/myob_advanced/api/models/bill.rb
74
+ - lib/myob_advanced/api/models/check.rb
75
+ - lib/myob_advanced/api/models/contact.rb
76
+ - lib/myob_advanced/api/models/custom.rb
77
+ - lib/myob_advanced/api/models/customer.rb
78
+ - lib/myob_advanced/api/models/customer_class.rb
79
+ - lib/myob_advanced/api/models/customer_payment_method.rb
80
+ - lib/myob_advanced/api/models/invoice.rb
81
+ - lib/myob_advanced/api/models/payment.rb
82
+ - lib/myob_advanced/api/models/payment_method.rb
83
+ - lib/myob_advanced/api/models/sales_invoice.rb
84
+ - lib/myob_advanced/api/models/vendor.rb
85
+ - lib/myob_advanced/api/models/vendor_class.rb
86
+ - lib/myob_advanced/api/version.rb
87
+ - myob-advanced-api.gemspec
88
+ homepage: https://github.com/vannguyenvietnam/myob-advanced-api
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: MYOB Advanced API
111
+ test_files: []