Class: Weary::Response
- Inherits:
-
Object
- Object
- Weary::Response
- Includes:
- Rack::Response::Helpers
- Defined in:
- lib/weary/response.rb
Instance Method Summary collapse
- #body ⇒ Object
- #call(env) ⇒ Object
- #each(&iterator) ⇒ Object
- #finish ⇒ Object
- #header ⇒ Object (also: #headers)
-
#initialize(body, status, headers) ⇒ Response
constructor
A new instance of Response.
- #length ⇒ Object
- #parse ⇒ Object
- #redirected? ⇒ Boolean
- #status ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(body, status, headers) ⇒ Response
Returns a new instance of Response.
9 10 11 12 |
# File 'lib/weary/response.rb', line 9 def initialize(body, status, headers) @response = Rack::Response.new body, status, headers @status = self.status end |
Instance Method Details
#body ⇒ Object
23 24 25 26 27 |
# File 'lib/weary/response.rb', line 23 def body buffer = "" @response.body.each {|chunk| buffer << chunk } buffer end |
#call(env) ⇒ Object
49 50 51 |
# File 'lib/weary/response.rb', line 49 def call(env) self.finish end |
#each(&iterator) ⇒ Object
29 30 31 |
# File 'lib/weary/response.rb', line 29 def each(&iterator) @response.body.each(&iterator) end |
#finish ⇒ Object
33 34 35 |
# File 'lib/weary/response.rb', line 33 def finish [status, header, self] end |
#header ⇒ Object Also known as: headers
18 19 20 |
# File 'lib/weary/response.rb', line 18 def header @response.header end |
#length ⇒ Object
45 46 47 |
# File 'lib/weary/response.rb', line 45 def length @response.length end |
#parse ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/weary/response.rb', line 53 def parse raise "The response does not contain a body" if body.nil? || body.empty? if block_given? yield body, content_type else raise "Unable to parse Content-Type: #{content_type}" unless content_type =~ /json($|;.*)/ MultiJson.decode body end end |
#redirected? ⇒ Boolean
41 42 43 |
# File 'lib/weary/response.rb', line 41 def redirected? @response.redirection? end |
#status ⇒ Object
14 15 16 |
# File 'lib/weary/response.rb', line 14 def status @response.status.to_i end |
#success? ⇒ Boolean
37 38 39 |
# File 'lib/weary/response.rb', line 37 def success? @response.successful? end |