Class: Nestful::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/nestful/response/headers.rb,
lib/nestful/response.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Headers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, parser = nil) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
11
# File 'lib/nestful/response.rb', line 5

def initialize(response, parser = nil)
  @response = response
  @body     = response.body
  @headers  = Headers.new(response.to_hash)
  @format   = Formats.for(headers.content_type)
  @parser ||= @format && @format.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



37
38
39
40
41
42
43
44
45
# File 'lib/nestful/response.rb', line 37

def method_missing(name, *args, &block)
  if decoded.respond_to?(name)
    decoded.send(name, *args, &block)
  elsif response.respond_to?(name)
    response.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/nestful/response.rb', line 3

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



3
4
5
# File 'lib/nestful/response.rb', line 3

def headers
  @headers
end

#parserObject (readonly)

Returns the value of attribute parser.



3
4
5
# File 'lib/nestful/response.rb', line 3

def parser
  @parser
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/nestful/response.rb', line 3

def response
  @response
end

Instance Method Details

#as_jsonObject



13
14
15
# File 'lib/nestful/response.rb', line 13

def as_json
  decoded
end

#codeObject Also known as: status



21
22
23
# File 'lib/nestful/response.rb', line 21

def code
  response.code.to_i
end

#decodedObject



27
28
29
# File 'lib/nestful/response.rb', line 27

def decoded
  @decoded ||= parser ? parser.decode(body) : body
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/nestful/response.rb', line 31

def respond_to?(name)
  super || decoded.respond_to?(name) || response.respond_to?(name)
end

#to_jsonObject



17
18
19
# File 'lib/nestful/response.rb', line 17

def to_json(*)
  as_json.to_json
end