Class: Myob::Api::Client

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/myob/api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#model

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(options)
  model :CompanyFile

  model :Contact
  model :Customer
  model :Employee
  model :EmployeePayrollDetail

  model :PayrollCategory
  model :Wage

  model :Timesheet

  @redirect_uri         = options[:redirect_uri]
  @consumer             = options[:consumer]
  @access_token         = options[:access_token]
  @refresh_token        = options[:refresh_token]
  @current_company_file = options[: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 options[:company_file]
    @current_company_file = select_company_file(options[:company_file])
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/myob/api/client.rb', line 9

def client
  @client
end

#current_company_fileObject (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

#connectionObject



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.authorize_url(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

#headersObject



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