Class: Singleplatform::Request

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

Class Method Summary collapse

Class Method Details

.get(url) ⇒ Singleplatform::Response

Make an HTTP get request to given URL

Parameters:

  • url (String)

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/singleplatform/request.rb', line 10

def self.get(url)
  tries ||= 3
  response = HTTParty.get(url)
rescue
  sleep 3
  retry if (tries -= 1) > 0
  raise Error::RequestError
else
  raise(
    Error::ApiError,
    "#{response.code}: #{response['errorMessage']}"
  ) if response.code != 200
  Response.new(
    code:   response.code,
    body:   self.parse_response_body(response.body),
    # Pass the calling method to the Response object so Response#next 
    # knows which method to call when API results are iterable.
    origin: caller_locations(1,1)[0].label
  )
end

.parse_response_body(body) ⇒ Hashie::Mash

Transform API JSON response to Hashie::Mash pseudo object

Returns:

  • (Hashie::Mash)


34
35
36
37
# File 'lib/singleplatform/request.rb', line 34

def self.parse_response_body(body)
  return body unless JSON.parse(body)
  Hashie::Mash.new(JSON.parse(body)).data
end