Class: Hithorizons::BaseApi

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

Constant Summary collapse

API_URL =
''

Class Method Summary collapse

Class Method Details

.api_keyObject



29
30
31
# File 'lib/hithorizons/base_api.rb', line 29

def self.api_key
  Hithorizons.config.api_key
end

.get(path) ⇒ Object



42
43
44
# File 'lib/hithorizons/base_api.rb', line 42

def self.get(path)
  request(path, 'GET')
end

.initialize_clientObject



33
34
35
36
37
38
39
40
# File 'lib/hithorizons/base_api.rb', line 33

def self.initialize_client
  ::Faraday.new(url: Hithorizons.config.api_url) do |faraday|
    faraday.request :json
    faraday.response :json

    faraday.adapter Faraday.default_adapter
  end
end

.process_request(uri, method = 'GET', payload = nil) ⇒ Object

Raises:



7
8
9
10
11
12
13
# File 'lib/hithorizons/base_api.rb', line 7

def self.process_request(uri, method = 'GET', payload = nil)
  response = request(uri, method, payload)

  raise(Hithorizons::Error, response.body) if response.status != 200

  Response.new(response.body)
end

.request(uri, method = 'GET', payload = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hithorizons/base_api.rb', line 15

def self.request(uri, method = 'GET', payload = nil)
  client = initialize_client

  client.send(method.downcase.to_sym) do |request|
    request.headers['Ocp-Apim-Subscription-Key'] = api_key
    request.url("#{self::API_URL}#{uri}")
    if method == 'GET'
      request.params = payload
    else
      request.body = payload
    end
  end
end