Class: LHC::Response
- Inherits:
-
Object
- Object
- LHC::Response
- Defined in:
- lib/lhc/response.rb
Overview
The response contains the raw response (typhoeus) and provides functionality to access response data.
Defined Under Namespace
Classes: 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.
19 20 21 22 23 |
# File 'lib/lhc/response.rb', line 19 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.
10 11 12 |
# File 'lib/lhc/response.rb', line 10 def body_replacement @body_replacement end |
#from_cache ⇒ Object (readonly) Also known as: from_cache?
Returns the value of attribute from_cache.
11 12 13 |
# File 'lib/lhc/response.rb', line 11 def from_cache @from_cache end |
#request ⇒ Object
Returns the value of attribute request.
10 11 12 |
# File 'lib/lhc/response.rb', line 10 def request @request end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 |
# File 'lib/lhc/response.rb', line 29 def [](key) data[key] end |
#body ⇒ Object
33 34 35 |
# File 'lib/lhc/response.rb', line 33 def body body_replacement || raw.body.presence end |
#data ⇒ Object
25 26 27 |
# File 'lib/lhc/response.rb', line 25 def data @data ||= body.present? ? LHC::Response::Data.new(self) : nil end |
#format ⇒ Object
51 52 53 54 55 |
# File 'lib/lhc/response.rb', line 51 def format return LHC::Formats::JSON.new if request.nil? request.format end |
#time ⇒ Object
Provides response time in seconds
38 39 40 |
# File 'lib/lhc/response.rb', line 38 def time raw.time || 0 end |
#time_ms ⇒ Object
Provides response time in milliseconds
43 44 45 |
# File 'lib/lhc/response.rb', line 43 def time_ms time * 1000 end |
#timeout? ⇒ Boolean
47 48 49 |
# File 'lib/lhc/response.rb', line 47 def timeout? raw.timed_out? end |