Module: Teemill::AuthenticatedRequestHandler

Included in:
BaseResource, LegacyAuthenticatedRequestHandler
Defined in:
lib/resources/request/authenticated_request_handler.rb

Instance Method Summary collapse

Instance Method Details

#authenticated_request_headersObject


12
13
14
# File 'lib/resources/request/authenticated_request_handler.rb', line 12

def authenticated_request_headers
  { Authorization: Teemill.api_key }
end

#base_urlObject


8
9
10
# File 'lib/resources/request/authenticated_request_handler.rb', line 8

def base_url
  "#{Teemill.api_base}/v#{Teemill.api_version}"
end

#send_get_request(request_url, data) ⇒ Object


36
37
38
# File 'lib/resources/request/authenticated_request_handler.rb', line 36

def send_get_request(request_url, data)
  send_request(request_url, data, 'GET')
end

#send_post_request(request_url, data) ⇒ Object


32
33
34
# File 'lib/resources/request/authenticated_request_handler.rb', line 32

def send_post_request(request_url, data)
  send_request(request_url, data, 'POST')
end

#send_request(request_url, data, method = 'GET') ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/resources/request/authenticated_request_handler.rb', line 16

def send_request(request_url, data, method = 'GET')
  check_credentials
  Teemill::Debug.log("Dispatching: #{method} #{base_url}", 'yellow')
  url = URI("#{base_url}#{request_url}")

  http = create_http(url)

  request = create_request_object(url, data, method)

  response = http.request(request)
  Teemill::Debug.log("Received: #{response.code} #{response.msg}", 'yellow')
  raise Teemill::InvalidResponseError, "Error #{response.code}: #{response.msg}" unless response.code == "200"

  JSON.parse(response.read_body)
end