class LunaPark::Http::Response
Http
response value object.
Constants
- STATUSES
List of descriptions http codes
Attributes
Http
code, reference: {tools.ietf.org/html/rfc7231 rfc7231}
@example success response
response.code # => 200
@return Integer
Headers of http response
@example json response
response.headers # => { 'Content-Type': 'application/json' }
@return Hash
The request that actually initializes the current response
@example
request = Request.new( title: 'Get users list', method: :get, url: 'http://example.com/users' ) response = request.call response.request === request # => true response.request # => "<LunaPark::Http::Request @title=\"Get users\" # @url=\"http://localhost:8080/get_200\" @method=\"get\" # @headers=\"{}\" @body=\"\" @sent_at=\"\">"
@return LunaPark::Http::Response
Public Class Methods
Create new response
@param [LunaPark::Http::Request] request @param [Integer] code @param [String] body @param [Hash] headers @param [Hash] cookies
# File lib/luna_park/http/response.rb, line 140 def initialize(request:, code:, body: '', headers: {}, cookies: {}) @request = request @code = Integer(code) @body = String(body) @headers = Hash(headers) @cookies = Hash(cookies) end
Public Instance Methods
Two response should be equal, if their attributes (request, code, body, headers, cookies) match.
# File lib/luna_park/http/response.rb, line 373 def ==(other) code == other.code && body == other.body && headers == other.headers && cookies == other.cookies end
Check this response code is 4xx
@example response status - `Unprocessable Entity`
response.code # => 422 response.client_error? # => true
@example response status - `Success`
response.code # => 200 response.client_error? # => false
@return [Boolean]
# File lib/luna_park/http/response.rb, line 227 def client_error? (400..499).cover?(code) end
Check this response code is 1xx
@example response status - `Continue`
response.code # => 100 response.informational_response? # => true
@example response status - `Success`
response.code # => 200 response.informational_response? # => false
@return [Boolean]
# File lib/luna_park/http/response.rb, line 181 def informational_response? (100..199).cover?(code) end
@example inspect get users index request
request = LunaPark::Http::Request.new( title: 'Get users', method: :get, url: 'http://localhost:8080/get_200' ) response = LunaPark::Http::Request.new( code: 200, body: "{\"users\":[{\"name\":\"john\"}]", headers: {'Content-Type': 'application/json'} ) request.inspect # => "<LunaPark::Http::Request @title=\"Get users\" # @url=\"http://localhost:8080/get_200\" @method=\"get\" # @headers=\"{}\" @body=\"\" @sent_at=\"\">"
@return [String]
# File lib/luna_park/http/response.rb, line 166 def inspect "<#{self.class.name} @code=#{code} @body=\"#{body}\" @headers=#{headers} @cookies=#{cookies}>" end
Try to parse this response body from JSON format. If body doesn`t consists expected JSON format, you get nil. Also you can get only payload data if define payload_key.
@param [String, Symbol] payload_key - key of payload data @param [Boolean] stringify_keys - output hash should
@example parse from json whole data
response.body # => "{\"version\": 1, \"data\":{\"message\":\"pong\"}}" response.json_parse! # => {version: 1, data: { message: 'pong' }}
@example get only payload data
response.body # => "{\"version\": 1, \"data\":{\"message\":\"pong\"}}" response.json_parse! payload_key: :data # => { message: 'pong' }
@example parse from json whole data with string keys
response.body # => "{\"version\": 1, \"data\":{\"message\":\"pong\"}}" response.json_parse! stringify_keys: true # => {'version' => 1, 'data' => { 'message' => 'pong' }}
@example get data from non-json body data
response.body # => "pong" response.json_parse! # => nil
@return [Hash, String, nil]
# File lib/luna_park/http/response.rb, line 340 def json_parse(payload_key: nil, stringify_keys: false) json_parse!(payload_key: payload_key, stringify_keys: stringify_keys) rescue Errors::JsonParse nil end
Try to parse this response body from JSON format. If body doesn`t consists expected JSON format, you catch {Errors::JsonParse}. Also you can get only payload data if define payload_key.
@param [String, Symbol] payload_key - key of payload data @param [Boolean] stringify_keys - output hash should
@example parse from json whole data
response.body # => "{\"version\": 1, \"data\":{\"message\":\"pong\"}}" response.json_parse! # => {version: 1, data: { message: 'pong' }}
@example get only payload data
response.body # => "{\"version\": 1, \"data\":{\"message\":\"pong\"}}" response.json_parse! payload_key: :data # => { message: 'pong' }
@example parse from json whole data with string keys
response.body # => "{\"version\": 1, \"data\":{\"message\":\"pong\"}}" response.json_parse! stringify_keys: true # => {'version' => 1, 'data' => { 'message' => 'pong' }}
@example get data from non-json body data
response.body # => "pong" response.json_parse! # => raise Errors::JsonParse
@return [Hash, String]
# File lib/luna_park/http/response.rb, line 306 def json_parse!(payload_key: nil, stringify_keys: false) data = JSON.parse(body, symbolize_names: !stringify_keys) return data unless payload_key payload_key = stringify_keys ? payload_key.to_s : payload_key.to_sym data.fetch(payload_key) rescue KeyError, JSON::ParserError => e raise Errors::JsonParse.substitute(e) end
Check this response code is 3xx
@example response status - `Permanent Redirect`
response.code # => 308 response.redirection? # => true
@example response status - `Success`
response.code # => 200 response.redirection? # => false
@return [Boolean]
# File lib/luna_park/http/response.rb, line 212 def redirection? (300..399).cover?(code) end
Check this response code is 5xx
@example response status - `Internal Server Error`
response.code # => 500 response.server_error? # => true
@example response status - `Success`
response.code # => 200 response.server_error? # => false
@return [Boolean]
# File lib/luna_park/http/response.rb, line 242 def server_error? (500..599).cover?(code) end
Describes what the code means in a human-readable format.
@example when the object is successfully created
response.code # => 201 response.code # => 'Created'
@example when the server is down
response.code # => 503 response.code # => 'Service Unavailable'
# File lib/luna_park/http/response.rb, line 278 def status STATUSES[code] || 'Unknown' end
@return [Boolean]
# File lib/luna_park/http/response.rb, line 197 def success? (200..299).cover?(code) end
@example
request.to_h # => { :code=>200, :body=>"John Doe, Marry Ann", :headers=>{}, :cookies=>{}, :request=>{ :title=>"Get users", :method=>:get, :url=>"http://localhost:8080/get_200", :body=>nil, :headers=>{}, :read_timeout=>10, :open_timeout=>10, :sent_at=>nil } }
@return [Hash]
# File lib/luna_park/http/response.rb, line 362 def to_h { code: code, body: body, headers: headers, cookies: cookies, request: request.to_h } end
Return response code type. It can be
-
:informational_response - for response with 1xx code
-
:success - for response with 2xx code
-
:redirection - for response with 3xx code
-
:client_error - for response with 4xx code
-
:server_error - for response with 5xx code
@example success response
response.code # => 200 response.type # => :success
@return [Symbol]
# File lib/luna_park/http/response.rb, line 258 def type case code when 100..199 then :informational_response when 200..299 then :success when 300..399 then :redirection when 400..499 then :client_error when 500..599 then :server_error else :unknown end end