Class: BusinessCentral::Client

Inherits:
Object
  • Object
show all
Includes:
Object::ObjectHelper
Defined in:
lib/business_central/client.rb

Constant Summary collapse

DEFAULT_LOGIN_URL =
'https://login.microsoftonline.com/common'
DEFAULT_URL =
'https://api.businesscentral.dynamics.com/v2.0/production/api/v1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Object::ObjectHelper

#method_missing, #respond_to_missing?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/business_central/client.rb', line 26

def initialize(options = {})
  opts = options.dup
  @username = opts.delete(:username)
  @password = opts.delete(:password)
  @url = opts.delete(:url) || DEFAULT_URL
  @web_service_url = opts.delete(:web_service_url)
  @application_id = opts.delete(:application_id)
  @secret_key = opts.delete(:secret_key)
  @oauth2_login_url = opts.delete(:oauth2_login_url) || DEFAULT_LOGIN_URL
  @default_company_id = opts.delete(:default_company_id)
  @debug = opts.delete(:debug) || false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BusinessCentral::Object::ObjectHelper

Instance Attribute Details

#application_idObject (readonly)

Returns the value of attribute application_id.



13
14
15
# File 'lib/business_central/client.rb', line 13

def application_id
  @application_id
end

#debugObject (readonly)

Returns the value of attribute debug.



13
14
15
# File 'lib/business_central/client.rb', line 13

def debug
  @debug
end

#default_company_idObject (readonly)

Returns the value of attribute default_company_id.



13
14
15
# File 'lib/business_central/client.rb', line 13

def default_company_id
  @default_company_id
end

#oauth2_access_tokenObject (readonly) Also known as: access_token

Returns the value of attribute oauth2_access_token.



13
14
15
# File 'lib/business_central/client.rb', line 13

def oauth2_access_token
  @oauth2_access_token
end

#oauth2_login_urlObject (readonly)

Returns the value of attribute oauth2_login_url.



13
14
15
# File 'lib/business_central/client.rb', line 13

def 
  @oauth2_login_url
end

#passwordObject (readonly)

Returns the value of attribute password.



13
14
15
# File 'lib/business_central/client.rb', line 13

def password
  @password
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



13
14
15
# File 'lib/business_central/client.rb', line 13

def secret_key
  @secret_key
end

#urlObject (readonly)

Returns the value of attribute url.



13
14
15
# File 'lib/business_central/client.rb', line 13

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



13
14
15
# File 'lib/business_central/client.rb', line 13

def username
  @username
end

#web_service_urlObject (readonly)

Returns the value of attribute web_service_url.



13
14
15
# File 'lib/business_central/client.rb', line 13

def web_service_url
  @web_service_url
end

Instance Method Details

#authorize(params = {}, oauth_authorize_callback: '') ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/business_central/client.rb', line 39

def authorize(params = {}, oauth_authorize_callback: '')
  params[:redirect_uri] = oauth_authorize_callback
  begin
    oauth2_client.auth_code.authorize_url(params)
  rescue OAuth2::Error => e
    handle_error(e)
  end
end

#authorize_from_token(token: '', refresh_token: '', expires_at: nil, expires_in: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/business_central/client.rb', line 54

def authorize_from_token(token: '', refresh_token: '', expires_at: nil, expires_in: nil)
  @oauth2_access_token = OAuth2::AccessToken.new(
    oauth2_client,
    token,
    refresh_token: refresh_token,
    expires_at: expires_at,
    expires_in: expires_in
  )
end

#refresh_tokenObject



64
65
66
67
68
# File 'lib/business_central/client.rb', line 64

def refresh_token
  @oauth2_access_token.refresh!
rescue OAuth2::Error => e
  handle_error(e)
end

#request_token(code = '', oauth_token_callback: '') ⇒ Object



48
49
50
51
52
# File 'lib/business_central/client.rb', line 48

def request_token(code = '', oauth_token_callback: '')
  oauth2_client.auth_code.get_token(code, redirect_uri: oauth_token_callback)
rescue OAuth2::Error => e
  handle_error(e)
end

#web_serviceObject



70
71
72
# File 'lib/business_central/client.rb', line 70

def web_service
  @web_service ||= BusinessCentral::WebService.new(client: self, url: web_service_url)
end