Class: BusinessInsightApiClient::Helpers::RESTClient

Inherits:
Object
  • Object
show all
Includes:
HTTPClient::Util
Defined in:
lib/business_insight_api_client/helpers/restclient.rb

Overview

RESTClient helper, used as helper to interact with the API endpoints.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  api_url: 'https://api.nedap-bi.com',
  default_content_type: 'application/json',
}
API_VERSION_PATH =
'/v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authorization, options = {}) ⇒ RESTClient

Creates a new RESTClient helper

Parameters:

  • auhtorization (Authorization)

    the authorization helper used to create requests.

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

    the options to create the RESTClient Helper with.

Options Hash (options):

  • :api_url (String) — default: 'https://api.nedap-bi.com'

    the api url.

  • :content_type (String) — default: 'application/json'

    the content type of requests.

Raises:

  • (URI::InvalidURIError)

    when the api_url given can not be parsed into an uri.



34
35
36
37
38
# File 'lib/business_insight_api_client/helpers/restclient.rb', line 34

def initialize(authorization, options = {})
  @authorization = authorization
  @base_uri = URI(options[:api_url] || DEFAULT_OPTIONS[:api_url])
  @content_type = options[:content_type] || DEFAULT_OPTIONS[:default_content_type]
end

Instance Attribute Details

#authorizationObject (readonly)

Returns the value of attribute authorization.



19
20
21
# File 'lib/business_insight_api_client/helpers/restclient.rb', line 19

def authorization
  @authorization
end

#base_uriURI (readonly)

Returns the uri of the api.

Returns:

  • (URI)

    the uri of the api.



22
23
24
# File 'lib/business_insight_api_client/helpers/restclient.rb', line 22

def base_uri
  @base_uri
end

#content_typeString (readonly)

Returns the content type set.

Returns:

  • (String)

    the content type set



25
26
27
# File 'lib/business_insight_api_client/helpers/restclient.rb', line 25

def content_type
  @content_type
end

Instance Method Details

#delete(path) ⇒ Object



67
68
69
70
71
72
# File 'lib/business_insight_api_client/helpers/restclient.rb', line 67

def delete(path)
  build_path(path)
  response = client.delete(@base_uri.to_s, header: access_token_header)
  raise_errors(response)
  response.body
end

#get(path, *args, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/business_insight_api_client/helpers/restclient.rb', line 40

def get(path, *args, &block)
  build_path(path)
  begin
    arguments = argument_to_hash(args,:query, :body, :header, :follow_redirect) || {}
    arguments[:header] = access_token_header.merge(arguments[:header] || {})
    response = client.request(:get, @base_uri.to_s, arguments, &block)
  rescue HTTPClient::TimeoutError
    raise ::BusinessInsightApiClient::Errors::RequestTimedOutError
  end
  raise_errors(response)
  response.body
end

#post(path, body = '') ⇒ Object



60
61
62
63
64
65
# File 'lib/business_insight_api_client/helpers/restclient.rb', line 60

def post(path, body = '')
  build_path(path)
  response = client.post(@base_uri.to_s, body: body, header: { 'Content-Type' => content_type, 'Accept' => content_type}.merge(access_token_header))
  raise_errors(response)
  response.body
end

#put(path, body = '') ⇒ Object



53
54
55
56
57
58
# File 'lib/business_insight_api_client/helpers/restclient.rb', line 53

def put(path, body = '')
  build_path(path)
  response = client.put(@base_uri.to_s, body: body, header: { 'Content-Type' => content_type, 'Accept' => content_type}.merge(access_token_header))
  raise_errors(response)
  response.body
end