Class: GH::Response

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



10
11
12
# File 'lib/gh/response.rb', line 10

def body
  @body
end

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/gh/response.rb', line 10

def data
  @data
end

#headersObject

Returns the value of attribute headers.



10
11
12
# File 'lib/gh/response.rb', line 10

def headers
  @headers
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/gh/response.rb', line 10

def url
  @url
end

Instance Method Details

#==(other) ⇒ Object

Public: …



76
77
78
# File 'lib/gh/response.rb', line 76

def ==(other)
  super or @data == other
end

#dupObject

Public: Duplicates the instance. Will also duplicate some instance variables to behave as expected.

Returns new Response instance.



43
44
45
# File 'lib/gh/response.rb', line 43

def dup
  super.dup_ivars
end

#respond_to?(method) ⇒ Boolean

Public: Returns true or false indicating whether it supports method.

Returns:

  • (Boolean)


53
54
55
56
# 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_aryObject

Public: Implements to_ary conventions, please check respond_to?(:to_hash).



65
66
67
68
# File 'lib/gh/response.rb', line 65

def to_ary
  return method_missing(__method__) unless respond_to? __method__
  @data.dup.to_ary
end

#to_ghObject

Public: …



71
72
73
# File 'lib/gh/response.rb', line 71

def to_gh
  self
end

#to_hashObject

Public: Implements to_hash conventions, please check respond_to?(:to_hash).



59
60
61
62
# File 'lib/gh/response.rb', line 59

def to_hash
  return method_missing(__method__) unless respond_to? __method__
  @data.dup.to_hash
end

#to_sObject

Public: Returns the response body as a String.



48
49
50
# File 'lib/gh/response.rb', line 48

def to_s
  @body.dup
end