class DarujmeCz::Connection

Constants

URL

Public Class Methods

new(app_id:, api_key:) click to toggle source

@param [String] app_id @param [String] api_key

# File lib/darujme_cz/connection.rb, line 12
def initialize(app_id:, api_key:)
  @app_id = app_id
  @api_key = api_key
end

Public Instance Methods

get(path, **params) click to toggle source

@param [String] path @param [Hash] params

# File lib/darujme_cz/connection.rb, line 19
def get(path, **params)
  begin
    response = RestClient.get "#{URL}/#{path}", params: { apiId: @app_id, apiSecret: @api_key }.merge(params)
  rescue RestClient::Exception => ex
    handle_response_error(ex)
  end
  parse_response(response)
end
post(path, **data) click to toggle source
# File lib/darujme_cz/connection.rb, line 28
def post(path, **data)
  # @todo
  raise NotImplementedError
end

Private Instance Methods

handle_response_error(exception) click to toggle source

@param [RestClient::Exception] exception @raise [RestClient::Exception]

# File lib/darujme_cz/connection.rb, line 37
def handle_response_error(exception)
  data = parse_response(exception.response)
  exception.message += "\n#{data["message"]}"
  raise exception
end
parse_response(response) click to toggle source
# File lib/darujme_cz/connection.rb, line 43
def parse_response(response)
  JSON.parse(response.body)
rescue JSON::ParserError => _ex
  {}
end