Class: GH::Response
- Inherits:
-
Object
- Object
- GH::Response
- Extended by:
- Forwardable
- Includes:
- Enumerable, Case
- Defined in:
- lib/gh/response.rb
Overview
Public: Class wrapping low level Github responses.
Delegates safe methods to the parsed body (expected to be an Array or Hash).
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#data ⇒ Object
Returns the value of attribute data.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Public: …
-
#dup ⇒ Object
Public: Duplicates the instance.
-
#initialize(body = '{}', headers = {}, url = nil) ⇒ Response
constructor
Internal: Initializes a new instance.
-
#respond_to?(method) ⇒ Boolean
Public: Returns true or false indicating whether it supports method.
-
#to_ary ⇒ Object
Public: Implements to_ary conventions, please check respond_to?(:to_hash).
-
#to_gh ⇒ Object
Public: …
-
#to_hash ⇒ Object
Public: Implements to_hash conventions, please check respond_to?(:to_hash).
-
#to_s ⇒ Object
Public: Returns the response body as a String.
Constructor Details
#initialize(body = '{}', headers = {}, url = nil) ⇒ Response
Internal: Initializes a new instance.
headers - HTTP headers as a Hash body - HTTP body as a String
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gh/response.rb', line 24 def initialize(body = '{}', headers = {}, url = nil) @url = url @headers = headers.transform_keys { |k| k.downcase } 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 raise "Invalid encoding in #{url}, please contact github." end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
13 14 15 |
# File 'lib/gh/response.rb', line 13 def body @body end |
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/gh/response.rb', line 13 def data @data end |
#headers ⇒ Object
Returns the value of attribute headers.
13 14 15 |
# File 'lib/gh/response.rb', line 13 def headers @headers end |
#url ⇒ Object
Returns the value of attribute url.
13 14 15 |
# File 'lib/gh/response.rb', line 13 def url @url end |
Instance Method Details
#==(other) ⇒ Object
Public: …
82 83 84 |
# File 'lib/gh/response.rb', line 82 def ==(other) super or @data == other end |
#dup ⇒ Object
Public: Duplicates the instance. Will also duplicate some instance variables to behave as expected.
Returns new Response instance.
46 47 48 |
# File 'lib/gh/response.rb', line 46 def dup super.dup_ivars end |
#respond_to?(method) ⇒ Boolean
Public: Returns true or false indicating whether it supports method.
56 57 58 59 60 |
# File 'lib/gh/response.rb', line 56 def respond_to?(method, *) return super unless (method.to_s == 'to_hash') || (method.to_s == 'to_ary') data.respond_to? method end |
#to_ary ⇒ Object
Public: Implements to_ary conventions, please check respond_to?(:to_hash).
70 71 72 73 74 |
# File 'lib/gh/response.rb', line 70 def to_ary return method_missing(__method__) unless respond_to? __method__ @data.dup.to_ary end |
#to_gh ⇒ Object
Public: …
77 78 79 |
# File 'lib/gh/response.rb', line 77 def to_gh self end |
#to_hash ⇒ Object
Public: Implements to_hash conventions, please check respond_to?(:to_hash).
63 64 65 66 67 |
# File 'lib/gh/response.rb', line 63 def to_hash return method_missing(__method__) unless respond_to? __method__ @data.dup.to_hash end |
#to_s ⇒ Object
Public: Returns the response body as a String.
51 52 53 |
# File 'lib/gh/response.rb', line 51 def to_s @body.dup end |