Class: Http::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



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

def initialize
  @headers = {}
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/http/response.rb', line 5

def body
  @body
end

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/http/response.rb', line 4

def headers
  @headers
end

#statusObject Also known as: code, status_code, status_code=

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#[](header) ⇒ Object

Get a header value



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

def [](header)
  @headers[header.to_s.downcase]
end

#[]=(header, value) ⇒ Object

Set a header value



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/http/response.rb', line 19

def []=(header, value)
  key = header.to_s.downcase

  # Check if the header has already been set and group
  old_value = @headers[key]
  if old_value
    @headers[key] = [old_value].flatten << key
  else
    @headers[key] = value
  end
end

#parse_bodyObject

Parse the response body according to its content type



37
38
39
40
41
42
43
44
# File 'lib/http/response.rb', line 37

def parse_body
  if @headers['content-type']
    mime_type = MimeType[@headers['content-type'].split(/;\s*/).first]
    return mime_type.parse(@body) if mime_type
  end

  @body
end