Module: Weese::Requests::Requester

Overview

Actually making the HTTP request

Instance Method Summary collapse

Instance Method Details

#fetch(wmata_request) ⇒ Hash

Makes a request to WMATA API & deserializes the response into a Hash

Parameters:

  • wmata_request (Request)

    Request to send to WMATA API

Returns:

  • (Hash)

    Hash of response JSON

Raises:

  • (WeeseError)

    If the requests fails in any way



63
64
65
# File 'lib/weese/requests.rb', line 63

def fetch(wmata_request)
  request(wmata_request).deserialize
end

#request(wmata_request) ⇒ String

Make a request to WMATA API

Parameters:

  • wmata_request (Request)

    Request to send to WMATA API

Returns:

  • (String)

    Body of response

Raises:

  • (WeeseError)

    If the requests fails in any way



43
44
45
46
47
48
49
50
51
52
# File 'lib/weese/requests.rb', line 43

def request(wmata_request)
  response = Faraday.get(wmata_request.path) do |request|
    request.params = wmata_request.query
    request.headers['api_key'] = wmata_request.api_key
  end

  return response.body if response.success?

  raise WeeseError, "#{response.status}, #{response.body}"
end