Class: Magellan::Rails::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



9
10
11
# File 'lib/magellan/rails/response.rb', line 9

def body
  @body
end

#body_encodingObject

Returns the value of attribute body_encoding.



9
10
11
# File 'lib/magellan/rails/response.rb', line 9

def body_encoding
  @body_encoding
end

#headersObject

Returns the value of attribute headers.



9
10
11
# File 'lib/magellan/rails/response.rb', line 9

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



9
10
11
# File 'lib/magellan/rails/response.rb', line 9

def status
  @status
end

Instance Method Details

#parse_rack_response(response) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/magellan/rails/response.rb', line 11

def parse_rack_response(response)
  @status  = response[0]
  @headers = response[1]
  @body    = ''
  @body_encoding = :plain
  body_proxy = response[2]
  begin
    body_proxy.each{|b| @body << b}
  ensure
    body_proxy.close if body_proxy.respond_to?(:close)
  end
end

#to_messageObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/magellan/rails/response.rb', line 24

def to_message
  json_generate_errors = [JSON::GeneratorError, Encoding::UndefinedConversionError]
  begin
    {
      headers: @headers,
      status:  @status,
      body:    @body,
      body_encoding: @body_encoding.to_s,
    }.to_msgpack
  rescue *json_generate_errors
    @body = Base64.strict_encode64(@body)
    @body_encoding = :base64
    to_message
  end
end