class KipalogAPI::APIClient

Constants

ACCEPT_CHARSET
BASE_URI
DEFAULT_CHARSET
X_KIPALOG_TOKEN

Attributes

config[R]

Public Class Methods

new(config = KipalogAPI.config) click to toggle source
# File lib/kipalog_api/api_client.rb, line 11
def initialize(config = KipalogAPI.config)
  @config = config
end

Public Instance Methods

get(path) click to toggle source
# File lib/kipalog_api/api_client.rb, line 15
def get(path)
  send_request(:get, path)
end
post(path, request_body) click to toggle source
# File lib/kipalog_api/api_client.rb, line 19
def post(path, request_body)
  send_request(:post, path, request_body)
end
send_request(method, path, body_hash = {}) click to toggle source
# File lib/kipalog_api/api_client.rb, line 23
def send_request(method, path, body_hash = {})
  _make_request(method, path, body_hash) do |result|
    response = KipalogAPI::Response.new(
      result.code,
      result.headers,
      result.parsed_response
    )

    if response.ok?
      response
    else
      raise RequestError, response.body
    end
  end
end

Private Instance Methods

_headers() click to toggle source
# File lib/kipalog_api/api_client.rb, line 57
def _headers
  {
    X_KIPALOG_TOKEN => config.token,
    ACCEPT_CHARSET => DEFAULT_CHARSET
  }
end
_make_request(method, path, body_hash) { |send(method, path, _request_options(body_hash))| ... } click to toggle source
# File lib/kipalog_api/api_client.rb, line 43
def _make_request(method, path, body_hash)
  yield(HTTParty.send(method, path, _request_options(body_hash)))
rescue HTTParty::Error => e
  raise RequestError, e.message
end
_request_options(body_hash) click to toggle source
# File lib/kipalog_api/api_client.rb, line 49
def _request_options(body_hash)
  {
    base_uri: BASE_URI,
    body: body_hash.to_json,
    headers: _headers
  }
end