Class: Myob::Api::Client
- Inherits:
-
Object
- Object
- Myob::Api::Client
- Includes:
- Helpers
- Defined in:
- lib/myob/api/client.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#current_company_file ⇒ Object
readonly
Returns the value of attribute current_company_file.
Instance Method Summary collapse
- #connection ⇒ Object
- #get_access_code_url(params = {}) ⇒ Object
- #get_access_token(access_code) ⇒ Object
- #headers ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #select_company_file(company_file) ⇒ Object
Methods included from Helpers
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/myob/api/client.rb', line 11 def initialize() model :CompanyFile model :Contact model :Customer model :Employee model :EmployeePayrollDetail model :PayrollCategory model :Wage model :Timesheet @redirect_uri = [:redirect_uri] @consumer = [:consumer] @access_token = [:access_token] @refresh_token = [:refresh_token] @current_company_file = [:selected_company_file] || {} @client = OAuth2::Client.new(@consumer[:key], @consumer[:secret], { :site => 'https://secure.myob.com', :authorize_url => '/oauth2/account/authorize', :token_url => '/oauth2/v1/authorize', }) if [:company_file] @current_company_file = select_company_file([:company_file]) end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/myob/api/client.rb', line 9 def client @client end |
#current_company_file ⇒ Object (readonly)
Returns the value of attribute current_company_file.
9 10 11 |
# File 'lib/myob/api/client.rb', line 9 def current_company_file @current_company_file end |
Instance Method Details
#connection ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/myob/api/client.rb', line 69 def connection if @refresh_token @auth_connection ||= OAuth2::AccessToken.new(@client, @access_token, { :refresh_token => @refresh_token }).refresh! else @auth_connection ||= OAuth2::AccessToken.new(@client, @access_token) end end |
#get_access_code_url(params = {}) ⇒ Object
40 41 42 |
# File 'lib/myob/api/client.rb', line 40 def get_access_code_url(params = {}) @client.auth_code.(params.merge(scope: 'CompanyFile', redirect_uri: @redirect_uri)) end |
#get_access_token(access_code) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/myob/api/client.rb', line 44 def get_access_token(access_code) @token = @client.auth_code.get_token(access_code, redirect_uri: @redirect_uri) @access_token = @token.token @expires_at = @token.expires_at @refresh_token = @token.refresh_token @token end |
#headers ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/myob/api/client.rb', line 52 def headers { 'x-myobapi-key' => @consumer[:key], 'x-myobapi-version' => 'v2', 'x-myobapi-cftoken' => @current_company_file[:token] || '', 'Content-Type' => 'application/json' } end |
#select_company_file(company_file) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/myob/api/client.rb', line 61 def select_company_file(company_file) company_file_id = self.company_file.first('Name' => company_file[:name])['Id'] @current_company_file = { :id => company_file_id, :token => Base64.encode64("#{company_file[:username]}:#{company_file[:password]}"), } end |