class Klient::Response

Attributes

original_response[R]
parsed_body[R]
parsed_headers[R]
status[R]

Public Class Methods

new(original_response, data = nil) click to toggle source
# File lib/klient/response.rb, line 22
def initialize(original_response, data = nil)
  @status = original_response.code

  # If data arg is provided then it's a collection resource and the original
  # response is for the entire collection. We don't want that -- this is an
  # individual resource FOR the collection -- so the data arg is used in place
  # of the parsed body for the collection response.
  if data
    @original_response = nil
    @parsed_body = data
    @parsed_headers = nil
  else
    @original_response = original_response
    @body = @original_response.body
    @parsed_headers = @original_response.headers

    if @original_response.body.blank?
       @parsed_body = {}
    else
      @parsed_body = JSON.parse(@original_response.body)
    end
  end
end

Public Instance Methods

body() click to toggle source
# File lib/klient/response.rb, line 6
def body
  @original_response.body
end
headers() click to toggle source
# File lib/klient/response.rb, line 18
def headers
  @parsed_headers
end
method_missing(mth, *args, &block) click to toggle source

TODO: This is dangerously wrong. It's just a shortcut to get something working.

# File lib/klient/response.rb, line 47
def method_missing(mth, *args, &block)
  if mth.to_s =~ /http_(\d+)\?/
    status_code == $1.to_i
  else
    @parsed_body.send(mth)
  end
end
ok?() click to toggle source
# File lib/klient/response.rb, line 10
def ok?
  (200..299).include?(status_code)
end
respond_to_missing?(mth, *args) click to toggle source
Calls superclass method
# File lib/klient/response.rb, line 55
def respond_to_missing?(mth, *args)
  mth.to_s =~ /http_(\d+)\?/ || @parsed_body.respond_to?(mth) || super
end
status_code() click to toggle source
# File lib/klient/response.rb, line 14
def status_code
  @original_response.code
end