lib/enceladus/requester.rb in enceladus-0.0.3 vs lib/enceladus/requester.rb in enceladus-0.0.4

- old
+ new

@@ -11,12 +11,13 @@ # Performing this action might results in RestClient::Exception. # Check out https://github.com/rest-client/rest-client#exceptions-see-wwww3orgprotocolsrfc2616rfc2616-sec10html for more details. # def get(action, params={}) url = api.url_for(action, params) + Enceladus::Logger.log.info { "About to request: #{url}" } perform_request do - JSON.parse(RestClient.get(url, request_headers), object_class: OpenStruct) + parse_response(RestClient.get(url, request_headers)) end end # Makes a post request to TMDb API endpoints. # Example: @@ -28,12 +29,13 @@ # Performing this action might results in RestClient::Exception. # Check out https://github.com/rest-client/rest-client#exceptions-see-wwww3orgprotocolsrfc2616rfc2616-sec10html for more details. # def post(action, params={}, form_data={}) url = api.url_for(action, params) + Enceladus::Logger.log.info { "About to request: #{url}" } perform_request do - JSON.parse(RestClient.post(url, form_data.to_json, request_headers), object_class: OpenStruct) + parse_response(RestClient.post(url, form_data.to_json, request_headers)) end end private def api @@ -61,8 +63,17 @@ end end def request_headers { accept: 'application/json', content_type: 'application/json' } + end + + def parse_response(response_body) + begin + Enceladus::Logger.log.info { "Response: #{JSON.pretty_generate(JSON.parse(response_body))}" } + JSON.parse(response_body, object_class: OpenStruct) + rescue JSON::ParserError => e + raise Enceladus::Exception::JsonParseError.new("Response body could not be parsed: #{e.message}") + end end end end \ No newline at end of file