module OEHClient::Helper::Response

Public Class Methods

handle(rest_response) click to toggle source
# File lib/oehclient/helper.rb, line 61
def self.handle(rest_response)

        valid_response_codes = [100, 200, 204, 302]
        # raise a generic HTTPRequestException if the the status code is not 100 (Continue) or 200 (OK)
        raise OEHClient::Exception::HTTPRequestException.new("HTTP Request Exception with code #{rest_response.code} -- details: #{rest_response.body}", rest_response.code) unless (valid_response_codes.include?(rest_response.code))

        api_response = Hash.new

        api_response[:body]         = (rest_response.code == 204 ? {} : (OEHClient::Helper::Response.valid_json?(rest_response.body) ? ActiveSupport::JSON.decode(rest_response.body) : rest_response.body))
        api_response[:cookies]      = rest_response.cookies

        api_response

end
valid_json?(json) click to toggle source

determine if the string that was passed is valid JSON syntax and can be parsed by the the ActiveSupport::JSON.decode method

# File lib/oehclient/helper.rb, line 51
def self.valid_json?(json)
        valid_indicator = true
        begin
        ActiveSupport::JSON.decode(json)
        rescue ActiveSupport::JSON.parse_error
        valid_indicator = false
        end
end