Class: HttpReactor::Response

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

Overview

A class that represents an HTTP response which wraps the Java HTTP NIO response object and provides methods for accessing the data using ruby idioms.

Instance Method Summary collapse

Constructor Details

#initialize(response_impl) ⇒ Response

Returns a new instance of Response.



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

def initialize(response_impl)
  @response_impl = response_impl
end

Instance Method Details

#[](name) ⇒ Object

Access the headers



35
36
37
# File 'lib/http_reactor/response.rb', line 35

def [](name)
  headers[name]
end

#bodyObject

Get the body text



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/http_reactor/response.rb', line 54

def body
  @body ||= begin
    begin
      io = Java.java_to_ruby(
        org.jruby.RubyIO.new(JRuby.runtime, entity.content).java_object
      )
      io.read
    rescue Exception => e
      puts "Error in Response#body: #{e.message}"
    end
  end
end

#codeObject



20
21
22
# File 'lib/http_reactor/response.rb', line 20

def code
  status_line.status_code
end

#content_lengthObject

Get the response content length



30
31
32
# File 'lib/http_reactor/response.rb', line 30

def content_length
  @content_length ||= @response_impl.entity.content_length
end

#content_typeObject

Get the response content type



25
26
27
# File 'lib/http_reactor/response.rb', line 25

def content_type
  @content_type ||= @response_impl.entity.content_type.value
end

#entityObject

Delegates to the HTTP NIO response



16
17
18
# File 'lib/http_reactor/response.rb', line 16

def entity
  @response_impl.entity
end

#headersObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/http_reactor/response.rb', line 39

def headers
  @headers ||= begin
    h = Hash.new
    @response_impl.all_headers.each do |header|
      if h[header.name]
        h[header.name] = [h[header.name], header.value]
      else
        h[header.name] = header.value
      end
    end
    h
  end
end

#status_lineObject

Delegates to the HTTP NIO response



11
12
13
# File 'lib/http_reactor/response.rb', line 11

def status_line
  @response_impl.status_line
end