class BusinessCentral::Client

Constants

DEFAULT_LOGIN_URL
DEFAULT_URL

Attributes

access_token[R]
application_id[R]
debug[R]
default_company_id[R]
oauth2_access_token[R]
oauth2_login_url[R]
password[R]
secret_key[R]
url[R]
username[R]
web_service_url[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/business_central/client.rb, line 75
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

Public Instance Methods

authorize(params = {}, oauth_authorize_callback: '') click to toggle source
# File lib/business_central/client.rb, line 88
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) click to toggle source
# File lib/business_central/client.rb, line 103
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_token() click to toggle source
# File lib/business_central/client.rb, line 113
def refresh_token
  @oauth2_access_token.refresh!
end
request_token(code = '', oauth_token_callback: '') click to toggle source
# File lib/business_central/client.rb, line 97
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_service() click to toggle source
# File lib/business_central/client.rb, line 117
def web_service
  @web_service ||= BusinessCentral::WebService.new(client: self, url: web_service_url)
end

Private Instance Methods

handle_error(error) click to toggle source
# File lib/business_central/client.rb, line 133
def handle_error(error)
  raise ApiException, error.message if error.code.nil?

  case error.code
  when 'invalid_client'
    raise InvalidClientException
  end
end
oauth2_client() click to toggle source
# File lib/business_central/client.rb, line 123
def oauth2_client
  OAuth2::Client.new(
    @application_id,
    @secret_key,
    site: @oauth2_login_url,
    authorize_url: 'oauth2/authorize?resource=https://api.businesscentral.dynamics.com',
    token_url: 'oauth2/token?resource=https://api.businesscentral.dynamics.com'
  )
end