Class: AgentX::Response
- Inherits:
-
Object
- Object
- AgentX::Response
- Defined in:
- lib/agentx/response.rb
Defined Under Namespace
Classes: Headers
Constant Summary collapse
- CACHEABLE_CODES =
[200, 203, 300, 301, 302, 404, 410]
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Class Method Summary collapse
Instance Method Summary collapse
- #cacheable? ⇒ Boolean
- #cookies ⇒ Object
- #expires_at ⇒ Object
- #fresh? ⇒ Boolean
-
#initialize(code, body, headers) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
- #not_modified? ⇒ Boolean
- #ok? ⇒ Boolean
- #parse(type = nil) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/agentx/response.rb', line 5 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/agentx/response.rb', line 5 def code @code end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/agentx/response.rb', line 5 def headers @headers end |
Class Method Details
.from_easy(easy, response = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/agentx/response.rb', line 11 def self.from_easy(easy, response=nil) headers = Headers.parse(easy.response_headers) r = new(easy.response_code, easy.response_body, headers) if response && r.not_modified? r = new(response.code, response.body, response.headers.merge(r.headers)) end r end |
.from_hash(h) ⇒ Object
62 63 64 |
# File 'lib/agentx/response.rb', line 62 def self.from_hash(h) new(h['code'], h['body'], h['headers']) end |
Instance Method Details
#cacheable? ⇒ Boolean
45 46 47 48 49 50 |
# File 'lib/agentx/response.rb', line 45 def cacheable? return false if headers.cache_control && headers.cache_control.no_store? return false unless CACHEABLE_CODES.include?(code) !! (headers.etag || headers.last_modified || fresh?) end |
#cookies ⇒ Object
23 24 25 |
# File 'lib/agentx/response.rb', line 23 def Array(headers. || []) end |
#expires_at ⇒ Object
39 40 41 |
# File 'lib/agentx/response.rb', line 39 def expires_at headers.ttl ? Time.now + headers.ttl : Time.at(0) end |
#fresh? ⇒ Boolean
35 36 37 |
# File 'lib/agentx/response.rb', line 35 def fresh? headers.ttl && headers.ttl > 0 end |
#inspect ⇒ Object
52 53 54 |
# File 'lib/agentx/response.rb', line 52 def inspect "(Response #{code})" end |
#not_modified? ⇒ Boolean
31 32 33 |
# File 'lib/agentx/response.rb', line 31 def not_modified? code == 304 end |
#ok? ⇒ Boolean
27 28 29 |
# File 'lib/agentx/response.rb', line 27 def ok? code == 200 end |
#parse(type = nil) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/agentx/response.rb', line 66 def parse(type=nil) case when type == :json || headers.json? then Oj.load(body) when type == :html || headers.html? then HTML.new(body) when type == :xml || headers.xml? then XML.new(body) else body end end |
#to_hash ⇒ Object
56 57 58 59 60 |
# File 'lib/agentx/response.rb', line 56 def to_hash { 'code' => code, 'body' => body, 'headers' => headers.to_hash } end |