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, location = nil) ⇒ Response

Returns a new instance of Response.



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

def initialize(response, location = nil)
  @response = response
  @body     = response.body
  @location = location
  @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)



42
43
44
45
46
47
48
49
50
# File 'lib/nestful/response.rb', line 42

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

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
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



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

def as_json
  decoded
end

#codeObject Also known as: status



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

def code
  response.code.to_i
end

#decodedObject



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

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

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/nestful/response.rb', line 36

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

#to_jsonObject



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

def to_json(*)
  as_json.to_json
end

#to_sObject



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

def to_s
  body
end