Module: Fakery::Api

Defined in:
lib/fakery/api.rb

Class Method Summary collapse

Class Method Details

.get(url, params: {}) ⇒ Object

Get the url and parse the response body into a Fakery::Fake object. If this is unsuccessful or an invalid JSON text is returned, this method will throw a Fakery::ApiError exception.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fakery/api.rb', line 12

def get(url, params: {})
  response = get_response(url, params: params)
  if response.success?
    JSON.parse response.body, object_class: Fakery::Fake
  else
    raise Fakery::ApiError.new(
      "api responded with http_status=#{response.code} "\
      "message=#{response.return_message.inspect}",
      response: response
    )
  end
rescue JSON::ParserError => e
  raise Fakery::ApiError.new(
    "#{e.class}: #{e.message}",
    response: response
  )
end

.get_response(url, params: {}) ⇒ Object

Get a http response from url with parameters params.



5
6
7
# File 'lib/fakery/api.rb', line 5

def get_response(url, params: {})
  Typhoeus.get(url, params: params, followlocation: true)
end