Class: Weary::Response

Inherits:
Object
  • Object
show all
Includes:
Rack::Response::Helpers
Defined in:
lib/weary/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(body, status, headers) ⇒ 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

#bodyObject



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

#finishObject



33
34
35
# File 'lib/weary/response.rb', line 33

def finish
  [status, header, self]
end

#headerObject Also known as: headers



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

def header
  @response.header
end

#lengthObject



45
46
47
# File 'lib/weary/response.rb', line 45

def length
  @response.length
end

#parseObject



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

#statusObject



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