Class: DHC::Response
- Inherits:
-
Object
- Object
- DHC::Response
- Defined in:
- lib/dhc/response.rb
Overview
The response contains the raw response (typhoeus) and provides functionality to access response data.
Instance Attribute Summary collapse
-
#body_replacement ⇒ Object
Returns the value of attribute body_replacement.
-
#from_cache ⇒ Object
(also: #from_cache?)
readonly
Returns the value of attribute from_cache.
-
#request ⇒ Object
Returns the value of attribute request.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #body ⇒ Object
- #data ⇒ Object
- #format ⇒ Object
-
#initialize(raw, request, from_cache: false) ⇒ Response
constructor
A response is initalized with the underlying raw response (typhoeus in our case) and the associated request.
-
#time ⇒ Object
Provides response time in seconds.
-
#time_ms ⇒ Object
Provides response time in milliseconds.
- #timeout? ⇒ Boolean
Constructor Details
#initialize(raw, request, from_cache: false) ⇒ Response
A response is initalized with the underlying raw response (typhoeus in our case) and the associated request.
18 19 20 21 22 |
# File 'lib/dhc/response.rb', line 18 def initialize(raw, request, from_cache: false) self.request = request self.raw = raw @from_cache = from_cache end |
Instance Attribute Details
#body_replacement ⇒ Object
Returns the value of attribute body_replacement.
9 10 11 |
# File 'lib/dhc/response.rb', line 9 def body_replacement @body_replacement end |
#from_cache ⇒ Object (readonly) Also known as: from_cache?
Returns the value of attribute from_cache.
10 11 12 |
# File 'lib/dhc/response.rb', line 10 def from_cache @from_cache end |
#request ⇒ Object
Returns the value of attribute request.
9 10 11 |
# File 'lib/dhc/response.rb', line 9 def request @request end |
Instance Method Details
#[](key) ⇒ Object
28 29 30 |
# File 'lib/dhc/response.rb', line 28 def [](key) data[key] end |
#body ⇒ Object
32 33 34 |
# File 'lib/dhc/response.rb', line 32 def body body_replacement || raw.body.presence end |
#data ⇒ Object
24 25 26 |
# File 'lib/dhc/response.rb', line 24 def data @data ||= body.present? ? JSON.parse(body) : nil end |
#format ⇒ Object
50 51 52 53 |
# File 'lib/dhc/response.rb', line 50 def format return DHC::Formats::JSON.new if request.nil? request.format end |
#time ⇒ Object
Provides response time in seconds
37 38 39 |
# File 'lib/dhc/response.rb', line 37 def time raw.time || 0 end |
#time_ms ⇒ Object
Provides response time in milliseconds
42 43 44 |
# File 'lib/dhc/response.rb', line 42 def time_ms time * 1000 end |
#timeout? ⇒ Boolean
46 47 48 |
# File 'lib/dhc/response.rb', line 46 def timeout? raw.timed_out? end |