Class: Kentaa::Api::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/kentaa/api/http.rb

Constant Summary collapse

LIVE_URL =
"https://api.kentaa.nl/v1/".freeze
TEST_URL =
"https://api.kentaa.staatklaar.nu/v1/".freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}) ⇒ Http

Returns a new instance of Http.



11
12
13
14
# File 'lib/kentaa/api/http.rb', line 11

def initialize(api_key, options = {})
  @api_key = api_key
  @options = options
end

Instance Method Details

#get(path, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kentaa/api/http.rb', line 16

def get(path, params = {})
  uri = URI.parse(File.join(api_url, path))
  uri.query = URI.encode_www_form(params) unless params.empty?

  request = Net::HTTP::Get.new(uri)
  request["X-Api-Key"] = @api_key

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
    http.request(request)
  end

  begin
    JSON.parse(response.body, symbolize_names: true)
  rescue JSON::ParserError => ex
    {
      message: "Unable to parse JSON: #{ex.message}"
    }
  end
end