class Enceladus::Requester

Public Class Methods

get(action, params={}) click to toggle source

Makes a get request to one of the TMDb API endpoints. Example:

Enceladus::Requester.get("account", { session_id: "43462867" })

Performing this action might results in RestClient::Exception. Check out github.com/rest-client/rest-client#exceptions-see-wwww3orgprotocolsrfc2616rfc2616-sec10html for more details.

# File lib/enceladus/requester.rb, line 14
def get(action, params={})
  url = api.url_for(action, params)
  Enceladus::Logger.log.info { "About to request: #{url}" }
  perform_request do
    parse_response(RestClient.get(url, request_headers))
  end
end
post(action, params={}, form_data={}) click to toggle source

Makes a post request to TMDb API endpoints. Example:

params = { session_id: "77678687" }
form_data = { media_type: "movie", media_id: 31231, watchlist: true }
Enceladus::Requester.post("account/777/watchlist", params, form_data)

Performing this action might results in RestClient::Exception. Check out github.com/rest-client/rest-client#exceptions-see-wwww3orgprotocolsrfc2616rfc2616-sec10html for more details.

# File lib/enceladus/requester.rb, line 32
def post(action, params={}, form_data={})
  url = api.url_for(action, params)
  Enceladus::Logger.log.info { "About to request: #{url}" }
  perform_request do
    parse_response(RestClient.post(url, form_data.to_json, request_headers))
  end
end

Private Class Methods

api() click to toggle source
# File lib/enceladus/requester.rb, line 41
def api
  Enceladus::Configuration::Api.instance
end
parse_response(response_body) click to toggle source
# File lib/enceladus/requester.rb, line 72
def parse_response(response_body)
  begin
    Enceladus::Logger.log.info { "Response: #{JSON.pretty_generate(JSON.parse(response_body))}" }
    JSON.parse(response_body).to_hashugar
  rescue JSON::ParserError => e
    raise Enceladus::Exception::JsonParseError.new("Response body could not be parsed: #{e.message}")
  end
end
perform_request(&block) click to toggle source

Handles request calls exceptions. Performing RestClient actions might results in RestClient::Exception. Check out github.com/rest-client/rest-client#exceptions-see-wwww3orgprotocolsrfc2616rfc2616-sec10html for more details.

So, this method is responsible for handling RestClient::Exception, parsing the error message to finally raise Enceladus::Exception::Api.

# File lib/enceladus/requester.rb, line 51
def perform_request(&block)
  begin
    block.call
  rescue RestClient::SSLCertificateNotVerified
    raise
  rescue RestClient::Exception => e
    message = ["The Movie DB API Exception:"]
    message << "@message=\"#{e.message}\""

    JSON.parse(e.response).each do |k,v|
      message << "@#{k}=\"#{v}\""
    end

    raise Enceladus::Exception::Api.new(message.join(" "))
  end
end
request_headers() click to toggle source
# File lib/enceladus/requester.rb, line 68
def request_headers
  { accept: 'application/json', content_type: 'application/json' }
end