class Papapi::Response

Constants

REMOVE_VARS

Attributes

request[R]
responses[R]

Public Class Methods

new(http_response, request) click to toggle source
# File lib/papapi/response.rb, line 8
def initialize (http_response, request)
  @http_response = http_response
  @request = request
  @responses = []
  check_for_errors
end

Public Instance Methods

parsed() click to toggle source
# File lib/papapi/response.rb, line 15
def parsed
  return @parsed if @parsed
  if @http_response.is_a? Net::HTTPResponse
    parsed_response = JSON.parse(@http_response.body)
    @parsed = parsed_response.shift

    #handle multi responses
    @request.requests.each do |req|
      resp = parsed_response.shift
      @responses << req.response(resp) if resp
    end
  else
    @parsed = @http_response
  end
  @parsed
end
to_h() click to toggle source
# File lib/papapi/response.rb, line 32
def to_h
  h = {}
  parsed.each_with_index do |p, index|
    next if index == 0
    h[p[0].to_sym] = p[1]
  end
  h
end

Private Instance Methods

check_for_errors() click to toggle source
# File lib/papapi/response.rb, line 43
def check_for_errors
  return if ! parsed.kind_of?(Hash)
  raise parsed['message'] if parsed['success'] != 'Y' && parsed['message']
  raise parsed['e']       if parsed['e']
end