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
# File 'lib/myob/api/client.rb', line 11

def initialize(options)
  Myob::Api::Model::Base.subclasses.each {|c| model(c.name.split("::").last)}

  @redirect_uri         = options[:redirect_uri]
  @consumer             = options[:consumer]
  @access_token         = options[:access_token]
  @refresh_token        = options[:refresh_token]

  @client               = OAuth2::Client.new(@consumer[:key], @consumer[:secret], {
    :site          => 'https://secure.myob.com',
    :authorize_url => '/oauth2/account/authorize',
    :token_url     => '/oauth2/v1/authorize',
  })

  # on client init, if we have a company file already, get the appropriate base URL for this company file from MYOB
  provided_company_file = options[:selected_company_file] || options[:company_file]
  select_company_file(provided_company_file) if provided_company_file
  @current_company_file ||= {}
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

#current_company_file_urlObject (readonly)

Returns the value of attribute current_company_file_url.



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

def current_company_file_url
  @current_company_file_url
end

Instance Method Details

#connectionObject



98
99
100
101
102
103
104
105
106
# File 'lib/myob/api/client.rb', line 98

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



31
32
33
# File 'lib/myob/api/client.rb', line 31

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



35
36
37
38
39
40
41
# File 'lib/myob/api/client.rb', line 35

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/myob/api/client.rb', line 43

def headers
  token = (@current_company_file || {})[:token]
  if token.nil? || token == ''
    # if token is blank assume we are using federated login - http://myobapi.tumblr.com/post/109848079164/2015-1-release-notes
    {
      'x-myobapi-key'     => @consumer[:key],
      'x-myobapi-version' => 'v2',
      'Content-Type'      => 'application/json'
    }
  else
    {
      'x-myobapi-key'     => @consumer[:key],
      'x-myobapi-version' => 'v2',
      'x-myobapi-cftoken' => token,
      'Content-Type'      => 'application/json'
    }
  end
end

#select_company_file(company_file) ⇒ Object

given some company file credentials, connect to MYOB and get the appropriate company file object. store its ID and token for auth’ing requests, and its URL to ensure we talk to the right MYOB server.

‘company_file` should be hash. accepted forms:

String, username: String, password: String String, token: String



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/myob/api/client.rb', line 69

def select_company_file(company_file)
  # store the provided company file as an ivar so we can use it for subsequent requests
  # we need the token from it to make the initial request to get company files
  @current_company_file ||= company_file if company_file[:token]

  selected_company_file = company_files.find {|file|
    if company_file[:name]
      file['Name'] == company_file[:name]
    elsif company_file[:id]
      file['Id'] == company_file[:id]
    end
  }

  if selected_company_file
    token = company_file[:token]
    if (token.nil? || token == '') && !company_file[:username].nil? && company_file[:username] != '' && !company_file[:password].nil?
      # if we have been given login details, encode them into a token
      token = Base64.encode64("#{company_file[:username]}:#{company_file[:password]}")
    end
    @current_company_file = {
      :id    => selected_company_file['Id'],
      :token => token
    }
    @current_company_file_url = selected_company_file['Uri']
  else
    @current_company_file = {}
  end
end