Class: Restafari::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/restafari/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resp) ⇒ Response

Returns a new instance of Response.



6
7
8
9
# File 'lib/restafari/response.rb', line 6

def initialize(resp)
  @resp = resp
  @data = @resp.is_a?(Hash) ? @resp[:body] : JSON.parse(@resp.body)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



20
21
22
23
24
25
# File 'lib/restafari/response.rb', line 20

def method_missing(method_id, *args)
  return super unless @data.key?(method_id.to_s)
  self[method_id]
rescue NoMethodError=>ex
  raise NoMethodError.new("no #{method_id} method, response: #@resp", "no method error")
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/restafari/response.rb', line 4

def data
  @data
end

#respObject (readonly)

Returns the value of attribute resp.



4
5
6
# File 'lib/restafari/response.rb', line 4

def resp
  @resp
end

Instance Method Details

#[](i) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/restafari/response.rb', line 11

def [](i)
  case i
    when Symbol
      @data[i.to_s]
    else
      @data[i]
  end
end