Class: Myob::Essentials::Api::Client
- Inherits:
-
Object
- Object
- Myob::Essentials::Api::Client
- Includes:
- Helpers
- Defined in:
- lib/myob/essentials/api/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#business_uid ⇒ Object
Returns the value of attribute business_uid.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
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.
- #refresh! ⇒ Object
Methods included from Helpers
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
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 39 |
# File 'lib/myob/essentials/api/client.rb', line 13 def initialize() model :Business model :TaxType model :AccountType model :AccountClassification model :Contact model :InventoryItem model :Account model :AccountBalance model :SaleInvoice model :SalePayment @redirect_uri = [:redirect_uri] @consumer = [:consumer] @access_token = [:access_token] @refresh_token = [:refresh_token] @auto_refresh = [:auto_refresh] || true @endpoint = [:endpoint] || 'au' @business_uid = [:business_uid] @client = OAuth2::Client.new(@consumer[:key], @consumer[:secret], { site: 'https://secure.myob.com', authorize_url: '/oauth2/account/authorize', token_url: '/oauth2/v1/authorize', connection_opts: {request: {timeout: [:timeout] || 300}} }) end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
10 11 12 |
# File 'lib/myob/essentials/api/client.rb', line 10 def access_token @access_token end |
#business_uid ⇒ Object
Returns the value of attribute business_uid.
11 12 13 |
# File 'lib/myob/essentials/api/client.rb', line 11 def business_uid @business_uid end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/myob/essentials/api/client.rb', line 10 def client @client end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
10 11 12 |
# File 'lib/myob/essentials/api/client.rb', line 10 def endpoint @endpoint end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
10 11 12 |
# File 'lib/myob/essentials/api/client.rb', line 10 def expires_at @expires_at end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
10 11 12 |
# File 'lib/myob/essentials/api/client.rb', line 10 def refresh_token @refresh_token end |
Instance Method Details
#connection ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/myob/essentials/api/client.rb', line 72 def connection @auth_connection ||= begin if @refresh_token OAuth2::AccessToken.new(@client, @access_token, {refresh_token: @refresh_token}) else OAuth2::AccessToken.new(@client, @access_token) end end refresh! if @auto_refresh && @expires_at && @expires_at < Time.now.to_i @auth_connection end |
#get_access_code_url(params = {}) ⇒ Object
41 42 43 |
# File 'lib/myob/essentials/api/client.rb', line 41 def get_access_code_url(params = {}) @client.auth_code.(params.merge(scope: 'la.global', redirect_uri: @redirect_uri)) end |
#get_access_token(access_code) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/myob/essentials/api/client.rb', line 45 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
53 54 55 56 57 58 59 60 |
# File 'lib/myob/essentials/api/client.rb', line 53 def headers { 'x-myobapi-key' => @consumer[:key], 'x-myobapi-version' => 'v0', 'Accept' => 'application/json', 'Content-Type' => 'application/json' } end |
#refresh! ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/myob/essentials/api/client.rb', line 62 def refresh! @auth_connection ||= OAuth2::AccessToken.new(@client, @access_token, {refresh_token: @refresh_token}) @token = @auth_connection.refresh! @access_token = @token.token @expires_at = @token.expires_at @refresh_token = @token.refresh_token @token end |