class GH::Response
Public: Class wrapping low level Github responses.
Delegates safe methods to the parsed body (expected to be an Array or Hash).
Attributes
body[RW]
data[RW]
headers[RW]
url[RW]
Public Class Methods
new(body = "{}", headers = {}, url = nil)
click to toggle source
Internal: Initializes a new instance.
headers - HTTP headers as a Hash body - HTTP body as a String
# File lib/gh/response.rb, line 21 def initialize(body = "{}", headers = {}, url = nil) @url = url @headers = Hash[headers.map { |k,v| [k.downcase, v] }] case body when nil, '' then @data = {} when respond_to(:to_str) then @body = body.to_str when respond_to(:to_hash) then @data = body.to_hash when respond_to(:to_ary) then @data = body.to_ary else raise ArgumentError, "cannot parse #{body.inspect}" end @body.force_encoding("utf-8") if @body.respond_to? :force_encoding @body ||= MultiJson.encode(@data) @data ||= MultiJson.decode(@body) rescue EncodingError => error fail "Invalid encoding in #{url.to_s}, please contact github." end
Public Instance Methods
==(other)
click to toggle source
Public: …
Calls superclass method
# File lib/gh/response.rb, line 76 def ==(other) super or @data == other end
dup()
click to toggle source
Public: Duplicates the instance. Will also duplicate some instance variables to behave as expected.
Returns new Response
instance.
Calls superclass method
# File lib/gh/response.rb, line 43 def dup super.dup_ivars end
respond_to?(method, *)
click to toggle source
Public: Returns true or false indicating whether it supports method.
Calls superclass method
# File lib/gh/response.rb, line 53 def respond_to?(method, *) return super unless method.to_s == "to_hash" or method.to_s == "to_ary" data.respond_to? method end
to_ary()
click to toggle source
Public: Implements to_ary
conventions, please check respond_to?(:to_hash).
# File lib/gh/response.rb, line 65 def to_ary return method_missing(__method__) unless respond_to? __method__ @data.dup.to_ary end
to_gh()
click to toggle source
Public: …
# File lib/gh/response.rb, line 71 def to_gh self end
to_hash()
click to toggle source
Public: Implements to_hash
conventions, please check respond_to?(:to_hash).
# File lib/gh/response.rb, line 59 def to_hash return method_missing(__method__) unless respond_to? __method__ @data.dup.to_hash end
to_s()
click to toggle source
Public: Returns the response body as a String.
# File lib/gh/response.rb, line 48 def to_s @body.dup end
Protected Instance Methods
dup_ivars()
click to toggle source
# File lib/gh/response.rb, line 82 def dup_ivars @headers, @data, @body = @headers.dup, @data.dup, @body.dup self end
Private Instance Methods
content_type()
click to toggle source
# File lib/gh/response.rb, line 89 def content_type headers['content-type'] end