Class: Whatconverts::HttpService

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

Constant Summary collapse

API_URL =
'https://app.whatconverts.com/api/v1/'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token, api_secret) ⇒ HttpService

Returns a new instance of HttpService.



7
8
9
10
# File 'lib/whatconverts/http_service.rb', line 7

def initialize(api_token, api_secret)
  @api_token = api_token
  @api_secret = api_secret
end

Instance Attribute Details

#api_secretObject

Returns the value of attribute api_secret.



5
6
7
# File 'lib/whatconverts/http_service.rb', line 5

def api_secret
  @api_secret
end

#api_tokenObject

Returns the value of attribute api_token.



5
6
7
# File 'lib/whatconverts/http_service.rb', line 5

def api_token
  @api_token
end

Instance Method Details

#connectionObject



20
21
22
23
24
# File 'lib/whatconverts/http_service.rb', line 20

def connection
  conn = Faraday.new(url: API_URL)
  conn.basic_auth(api_token, api_secret)
  conn
end

#make_request(endpoint, params = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/whatconverts/http_service.rb', line 12

def make_request(endpoint, params = {})
  method = params.delete(:method) { :get }
  response =  connection.send(method, endpoint, params)
  error = ErrorChecker.new(response).error_if_appropriate
  raise error if error
  JSON.parse(response.body)
end