Class: BusinessCentral::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Creates an instance of the BusinessCentral::Client.

Pass in a Hash of the various options:

api_version: The path of the API Version - eg "/v1.0"
api_path: Appended after the API Version - eg "/api/v1.0"
api_username: Used for Basic Auth
api_password: Used for Basic Auth
api_tenant: The tenant domain for your BusinessCentral installation
api_company_id: The company that will be queried

Parameters:

  • opts (Hash) (defaults to: {})

    A Hash with login configuration options



22
23
24
25
26
27
28
29
30
# File 'lib/business_central/client.rb', line 22

def initialize(opts = {})
  @api_version = opts[:api_version] ||= BusinessCentral::API_VERSION
  @api_path = opts[:api_path] ||= BusinessCentral::API_PATH
  @api_tenant = opts[:api_tenant] ||= ENV['BC_TENANT']
  @api_username = opts[:api_username] ||= ENV['BC_USERNAME']
  @api_password = opts[:api_password] ||= ENV['BC_PASSWORD']
  @api_company_id = opts[:api_company_id] ||= ENV['BC_COMPANY_ID']
  @api_host = opts[:api_host] ||= ENV['BC_HOST']
end

Instance Attribute Details

#api_company_idObject (readonly)

Returns the value of attribute api_company_id.



7
8
9
# File 'lib/business_central/client.rb', line 7

def api_company_id
  @api_company_id
end

#api_hostObject (readonly)

Returns the value of attribute api_host.



7
8
9
# File 'lib/business_central/client.rb', line 7

def api_host
  @api_host
end

#api_passwordObject (readonly)

Returns the value of attribute api_password.



7
8
9
# File 'lib/business_central/client.rb', line 7

def api_password
  @api_password
end

#api_pathObject (readonly)

Returns the value of attribute api_path.



7
8
9
# File 'lib/business_central/client.rb', line 7

def api_path
  @api_path
end

#api_tenantObject (readonly)

Returns the value of attribute api_tenant.



7
8
9
# File 'lib/business_central/client.rb', line 7

def api_tenant
  @api_tenant
end

#api_usernameObject (readonly)

Returns the value of attribute api_username.



7
8
9
# File 'lib/business_central/client.rb', line 7

def api_username
  @api_username
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



7
8
9
# File 'lib/business_central/client.rb', line 7

def api_version
  @api_version
end

Instance Method Details

#base_urlObject

Returns the URL used for interacting with the API



36
37
38
39
40
41
42
# File 'lib/business_central/client.rb', line 36

def base_url
  url = "#{@api_host}#{@api_version}/#{@api_tenant}#{@api_path}"
  unless @api_company_id.nil?
    url += "/companies(#{@api_company_id})"
  end
  url
end

#dataset(response) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/business_central/client.rb', line 88

def dataset(response)
  json = JSON.parse(response.body)
  if !json["value"].nil?
    json["value"]
  else
    json
  end
# rescue
#   {}
end

#delete(url, etag) ⇒ Object

Performs a DELETE operation

Parameters:

  • url (String)

    The URL to perform a request on

  • etag (String)

    The etag of the remote object on BusinessCentral



83
84
85
86
# File 'lib/business_central/client.rb', line 83

def delete(url, etag)
  request = build_request({ verb: "Delete", url: url, etag: etag })
  perform_request(request)
end

#get(url) ⇒ Object

Performs a GET operation

Parameters:

  • url (String)

    The URL to perform a request on



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

def get(url)
  request = build_request({ verb: "Get", url: url })
  perform_request(request)
end

#patch(url, etag, data) ⇒ Object

Performs a PATCH operation

Parameters:

  • url (String)

    The URL to perform a request on

  • etag (String)

    The etag of the remote object on BusinessCentral

  • data (Hash)

    The data to PATCH the remote object with



72
73
74
75
# File 'lib/business_central/client.rb', line 72

def patch(url, etag, data)
  request = build_request({ verb: "Patch", url: url, data: data, etag: etag })
  perform_request(request)
end

#post(url, data) ⇒ Object

Performs a POST operation

Parameters:

  • url (String)

    The URL to perform a request on

  • data (Hash)

    The data to POST to the URL



60
61
62
63
# File 'lib/business_central/client.rb', line 60

def post(url, data)
  request = build_request({ verb: "Post", url: url, data: data })
  perform_request(request)
end