Module: OEHClient::Helper::Response

Defined in:
lib/oehclient/helper.rb

Overview

module Request

Class Method Summary collapse

Class Method Details

.handle(rest_response) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)

51
52
53
54
55
56
57
58
# 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