myob-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.
data/.gitignore ADDED
@@ -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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in myob-api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 David Lumley
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.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # MYOB Api
2
+
3
+ [MYOB Api](https://github.com/davidlumley/myob-api) is an interface for accessing [MYOB](http://developer.myob.com/api/accountright/v2/)'s AccountRight Live API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'myob-api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install myob-api
18
+
19
+ ## Usage
20
+
21
+ ### API Client Setup
22
+
23
+ Create an api_client:
24
+
25
+ api_client = Myob::Api::Client.new({
26
+ :consumer => {
27
+ :key => YOUR_CONSUMER_KEY,
28
+ :secret => YOUR_CONSUMER_SECRET,
29
+ },
30
+ :access_token => YOUR_OAUTH_ACCESS_TOKEN,
31
+ })
32
+
33
+ Or if you know which Company File you want to access too:
34
+
35
+ api_client = Myob::Api::Client.new({
36
+ :consumer => {
37
+ :key => YOUR_CONSUMER_KEY,
38
+ :secret => YOUR_CONSUMER_SECRET,
39
+ },
40
+ :access_token => YOUR_OAUTH_ACCESS_TOKEN,
41
+ :company_file => {
42
+ :id => COMPANY_FILE_ID,
43
+ :username => COMPANY_FILE_USERNAME,
44
+ :password => COMPANY_FILE_PASSWORD,
45
+ },
46
+ })
47
+
48
+ ### API Methods
49
+
50
+ Before using the majority of API methods you will need to have selected a Company File. If you've already selected one when creating the client, feel free to ignore this.
51
+
52
+ #### Company Files
53
+
54
+ Return a list of company files
55
+
56
+ api_client.company_file.all
57
+
58
+ Select a company file to work with
59
+
60
+ api_client.select_company_file({
61
+ :id => COMPANY_FILE_ID,
62
+ :username => COMPANY_FILE_USERNAME,
63
+ :password => COMPANY_FILE_PASSWORD,
64
+ })
65
+
66
+ #### Contacts
67
+
68
+ Return a list of all contacts
69
+
70
+ api_client.contact.all
71
+
72
+ #### Customers
73
+
74
+ Return a list of all customers (a subset of contacts)
75
+
76
+ api_client.customer.all
77
+
78
+
79
+ ## Todo
80
+
81
+ * Expand API methods
82
+ * Refactor client factory architecture
83
+ * Tests
84
+
85
+
86
+ ## Contributing
87
+
88
+ 1. Fork it
89
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 4. Push to the branch (`git push origin my-new-feature`)
92
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/myob/api.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'myob/api/version'
2
+
3
+ require 'myob/api/helpers'
4
+
5
+ require 'myob/api/models/base'
6
+ require 'myob/api/models/company_file'
7
+ require 'myob/api/models/contact'
8
+ require 'myob/api/models/customer'
9
+
10
+ require 'myob/api/client'
@@ -0,0 +1,45 @@
1
+ require 'Base64'
2
+
3
+ module Myob
4
+ module Api
5
+ class Client
6
+ include Myob::Api::Helpers
7
+
8
+ attr_reader :current_company_file
9
+
10
+ def initialize(options)
11
+ model :CompanyFile
12
+ model :Contact
13
+ model :Customer
14
+
15
+ @consumer = options[:consumer]
16
+ @access_token = options[:access_token]
17
+ if options[:company_file]
18
+ @current_company_file = select_company_file(options[:company_file])
19
+ else
20
+ @current_company_file = {}
21
+ end
22
+ @client = OAuth2::Client.new(@consumer[:key], @consumer[:secret])
23
+ end
24
+
25
+ def headers
26
+ {
27
+ 'x-myobapi-key' => @consumer[:key],
28
+ 'x-myobapi-version' => 'v2',
29
+ 'x-myobapi-cftoken' => @current_company_file[:token] || '',
30
+ }
31
+ end
32
+
33
+ def select_company_file(company_file)
34
+ @current_company_file = {
35
+ :id => company_file[:id],
36
+ :token => Base64.encode64("#{company_file[:username]}:#{company_file[:password]}"),
37
+ }
38
+ end
39
+
40
+ def connection
41
+ @auth_connection ||= OAuth2::AccessToken.new(@client, @access_token)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,26 @@
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 Api
13
+ module Helpers
14
+ def model(model_name)
15
+ variable_name = "@#{model_name.to_s.underscore}_model".to_sym
16
+ unless instance_variable_defined?(variable_name)
17
+ instance_variable_set(variable_name, Myob::Api::Model.const_get("#{model_name}".to_sym).new(self, model_name.to_s))
18
+ self.define_singleton_method("#{model_name.to_s.underscore}".to_sym) do
19
+ instance_variable_get(variable_name)
20
+ end
21
+ end
22
+ instance_variable_get(variable_name)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ module Myob
2
+ module Api
3
+ module Model
4
+ class Base
5
+
6
+ API_URL = 'https://api.myob.com/accountright/'
7
+
8
+ def initialize(client, model_name)
9
+ @client = client
10
+ @model_name = model_name || 'Base'
11
+ end
12
+
13
+ def model_route
14
+ @model_name.to_s
15
+ end
16
+
17
+ def all
18
+ parse_response(@client.connection.get(self.url, {:headers => @client.headers}))
19
+ end
20
+
21
+ def url
22
+ if self.model_route == ''
23
+ "#{API_URL}"
24
+ else
25
+ "#{API_URL}#{@client.current_company_file[:id]}/#{self.model_route}"
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def parse_response(response)
32
+ JSON.parse(response.body)
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ module Myob
2
+ module Api
3
+ module Model
4
+ class CompanyFile < Myob::Api::Model::Base
5
+ def model_route
6
+ ''
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Myob
2
+ module Api
3
+ module Model
4
+ class Contact < Myob::Api::Model::Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ module Myob
2
+ module Api
3
+ module Model
4
+ class Customer < Myob::Api::Model::Base
5
+ def model_route
6
+ 'Contact/Customer'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Myob
2
+ module Api
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/myob-api.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'myob/api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "myob-api"
8
+ spec.version = Myob::Api::VERSION
9
+ spec.authors = ["David Lumley"]
10
+ spec.email = ["david@davidlumley.com.au"]
11
+ spec.description = %q{MYOB AccountRight Live API V2}
12
+ spec.summary = %q{MYOB AccountRight Live API V2}
13
+ spec.homepage = "https://github.com/davidlumley/myob-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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: myob-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Lumley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: MYOB AccountRight Live API V2
47
+ email:
48
+ - david@davidlumley.com.au
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/myob/api.rb
59
+ - lib/myob/api/client.rb
60
+ - lib/myob/api/helpers.rb
61
+ - lib/myob/api/models/base.rb
62
+ - lib/myob/api/models/company_file.rb
63
+ - lib/myob/api/models/contact.rb
64
+ - lib/myob/api/models/customer.rb
65
+ - lib/myob/api/version.rb
66
+ - myob-api.gemspec
67
+ homepage: https://github.com/davidlumley/myob-api
68
+ licenses:
69
+ - MIT
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ segments:
81
+ - 0
82
+ hash: 601466071620978750
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ segments:
90
+ - 0
91
+ hash: 601466071620978750
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.23
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: MYOB AccountRight Live API V2
98
+ test_files: []