Class: AllHours::Everhour

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, api_url: 'https://api.everhour.com/', api_version: '1.2') ⇒ Everhour

Returns a new instance of Everhour.



13
14
15
16
17
# File 'lib/everhour.rb', line 13

def initialize api_key:, api_url: 'https://api.everhour.com/', api_version: '1.2'
  @api_key = api_key
  @api_url = api_url
  @api_version = api_version
end

Instance Method Details

#api_call(path:, method: 'get', query: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/everhour.rb', line 19

def api_call path:, method: 'get', query: nil
  url = build_url path: path, query: query

  case method
  when 'get'
    req = Net::HTTP::Get.new(url.to_s)
    req['X-Api-Key'] = api_key
    req['X-Accept-Version'] = api_version
    req['Content-Type'] = 'application/json'
  else
    "Error: Unknown method “#{method}”."
  end

  res = Net::HTTP.start(url.host, url.port, use_ssl: true) { |http|
    http.request req
  }

  JSON.parse res.body
end