Class: Motion::HTTP::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_request, status_code, headers, body) ⇒ Response



6
7
8
9
10
11
# File 'lib/common/http/response.rb', line 6

def initialize(original_request, status_code, headers, body)
  @original_request = original_request
  @status_code = status_code
  @headers = headers
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#original_requestObject (readonly)

Returns the value of attribute original_request.



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

def original_request
  @original_request
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



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

def status_code
  @status_code
end

Instance Method Details

#client_error?Boolean



21
22
23
# File 'lib/common/http/response.rb', line 21

def client_error?
  status_code && (400..499) === status_code
end

#inspectObject



38
39
40
# File 'lib/common/http/response.rb', line 38

def inspect
  "<Motion::HTTP::Response status_code:#{status_code} headers:#{headers.inspect} body:#{body.inspect}>"
end

#objectObject



29
30
31
32
33
34
35
36
# File 'lib/common/http/response.rb', line 29

def object
  @object ||= case headers['Content-Type']
    when /^application\/json/, /^application\/vnd.api\+json/
      JSON.parse(body)
    else
      body
    end
end

#redirect?Boolean



17
18
19
# File 'lib/common/http/response.rb', line 17

def redirect?
  status_code && (300..399) === status_code
end

#server_error?Boolean



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

def server_error?
  status_code && (500..599) === status_code
end

#success?Boolean



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

def success?
  status_code && (200..299) === status_code
end