Class: Resteze::Response
- Inherits:
-
Object
- Object
- Resteze::Response
- Defined in:
- lib/resteze/response.rb
Constant Summary collapse
- OK =
200
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#http_body ⇒ Object
Returns the value of attribute http_body.
-
#http_headers ⇒ Object
Returns the value of attribute http_headers.
-
#http_status ⇒ Object
Returns the value of attribute http_status.
-
#request_id ⇒ Object
Returns the value of attribute request_id.
Class Method Summary collapse
- .batch_response?(headers) ⇒ Boolean
- .from_faraday_response(faraday_response) ⇒ Object
- .mime_type(headers = {}) ⇒ Object
- .parse_body(body, status: nil, headers: {}) ⇒ Object
- .unparsable_body?(status:, headers:) ⇒ Boolean
Instance Method Summary collapse
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/resteze/response.rb', line 4 def data @data end |
#http_body ⇒ Object
Returns the value of attribute http_body.
4 5 6 |
# File 'lib/resteze/response.rb', line 4 def http_body @http_body end |
#http_headers ⇒ Object
Returns the value of attribute http_headers.
4 5 6 |
# File 'lib/resteze/response.rb', line 4 def http_headers @http_headers end |
#http_status ⇒ Object
Returns the value of attribute http_status.
4 5 6 |
# File 'lib/resteze/response.rb', line 4 def http_status @http_status end |
#request_id ⇒ Object
Returns the value of attribute request_id.
4 5 6 |
# File 'lib/resteze/response.rb', line 4 def request_id @request_id end |
Class Method Details
.batch_response?(headers) ⇒ Boolean
21 22 23 |
# File 'lib/resteze/response.rb', line 21 def self.batch_response?(headers) headers["Content-Type".freeze].to_s.include?("boundary=batchresponse".freeze) end |
.from_faraday_response(faraday_response) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/resteze/response.rb', line 6 def self.from_faraday_response(faraday_response) new.tap do |response| response.http_body = faraday_response.body response.http_headers = faraday_response.headers response.http_status = faraday_response.status response.request_id = faraday_response.headers["Request-Id"] response.data = parse_body( faraday_response.body, status: response.http_status, headers: response.http_headers ) end end |
.mime_type(headers = {}) ⇒ Object
43 44 45 46 |
# File 'lib/resteze/response.rb', line 43 def self.mime_type(headers = {}) mime_type = headers.transform_keys(&:downcase)["content-type"].to_s.split(";").first.to_s.strip Mime::LOOKUP[mime_type] || Mime::Type.lookup_by_extension(:json) end |
.parse_body(body, status: nil, headers: {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/resteze/response.rb', line 25 def self.parse_body(body, status: nil, headers: {}) return body if unparsable_body?(status:, headers:) type = mime_type(headers)&.symbol || :json case type when :json JSON.parse(body, symbolize_names: true) when :xml Hash.from_xml(body).deep_symbolize_keys else body end end |
.unparsable_body?(status:, headers:) ⇒ Boolean
39 40 41 |
# File 'lib/resteze/response.rb', line 39 def self.unparsable_body?(status:, headers:) status.to_i == 204 || (300..399).cover?(status.to_i) || batch_response?(headers) end |
Instance Method Details
#ok? ⇒ Boolean
48 49 50 |
# File 'lib/resteze/response.rb', line 48 def ok? http_status == OK end |