Class: NOMS::Command::UserAgent::Response
- Defined in:
- lib/noms/command/useragent/response.rb,
lib/noms/command/useragent/response/typhoeus.rb,
lib/noms/command/useragent/response/httpclient.rb
Direct Known Subclasses
Defined Under Namespace
Classes: HTTPClient, Typhoeus
Instance Attribute Summary collapse
-
#auth_hash ⇒ Object
Returns the value of attribute auth_hash.
-
#cache_control ⇒ Object
readonly
Returns the value of attribute cache_control.
-
#date ⇒ Object
Returns the value of attribute date.
-
#etag ⇒ Object
readonly
Returns the value of attribute etag.
-
#expires ⇒ Object
readonly
Returns the value of attribute expires.
-
#from_cache ⇒ Object
Returns the value of attribute from_cache.
-
#last_modified ⇒ Object
readonly
Returns the value of attribute last_modified.
-
#original_date ⇒ Object
Returns the value of attribute original_date.
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #age ⇒ Object
-
#body ⇒ Object
The response body.
- #cacheable? ⇒ Boolean
- #cacheable_copy ⇒ Object
- #cached! ⇒ Object
- #content_encoding ⇒ Object
-
#content_type ⇒ Object
MIME Type of content.
- #current? ⇒ Boolean
- #from_cache? ⇒ Boolean
- #get_expires ⇒ Object
- #get_header_time(hdr) ⇒ Object
-
#header(hdr = nil) ⇒ Object
A hash or headers, or specific header.
- #header_params(s) ⇒ Object
-
#initialize(httpresponse, opts = {}) ⇒ Response
constructor
Constructor, pass in HTTP response subclass object In the base clase (which is what gets unfrozen) This is a hash.
-
#status ⇒ Object
An integer status code.
-
#statusText ⇒ Object
The status message including code.
-
#success? ⇒ Boolean
The success status.
- #to_cache ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(httpresponse, opts = {}) ⇒ Response
Constructor, pass in HTTP response subclass object In the base clase (which is what gets unfrozen) This is a hash.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/noms/command/useragent/response.rb', line 39 def initialize(httpresponse, opts={}) @log = opts[:logger] || default_logger @response = httpresponse self.from_cache = false self.auth_hash = nil if httpresponse.respond_to? :[] self.from_cache = httpresponse['from_cache'] self.auth_hash = httpresponse['auth_hash'] self.original_date = Time.httpdate(httpresponse['original_date']) self.date = Time.httpdate(httpresponse['date']) end @cache_control = self.header 'Cache-Control' @date = get_header_time('Date') || Time.now @original_date ||= @date @expires = get_expires @etag = self.header 'Etag' @last_modified = get_header_time 'Last-modified' end |
Instance Attribute Details
#auth_hash ⇒ Object
Returns the value of attribute auth_hash.
24 25 26 |
# File 'lib/noms/command/useragent/response.rb', line 24 def auth_hash @auth_hash end |
#cache_control ⇒ Object (readonly)
Returns the value of attribute cache_control.
23 24 25 |
# File 'lib/noms/command/useragent/response.rb', line 23 def cache_control @cache_control end |
#date ⇒ Object
Returns the value of attribute date.
24 25 26 |
# File 'lib/noms/command/useragent/response.rb', line 24 def date @date end |
#etag ⇒ Object (readonly)
Returns the value of attribute etag.
23 24 25 |
# File 'lib/noms/command/useragent/response.rb', line 23 def etag @etag end |
#expires ⇒ Object (readonly)
Returns the value of attribute expires.
23 24 25 |
# File 'lib/noms/command/useragent/response.rb', line 23 def expires @expires end |
#from_cache ⇒ Object
Returns the value of attribute from_cache.
24 25 26 |
# File 'lib/noms/command/useragent/response.rb', line 24 def from_cache @from_cache end |
#last_modified ⇒ Object (readonly)
Returns the value of attribute last_modified.
23 24 25 |
# File 'lib/noms/command/useragent/response.rb', line 23 def last_modified @last_modified end |
#original_date ⇒ Object
Returns the value of attribute original_date.
24 25 26 |
# File 'lib/noms/command/useragent/response.rb', line 24 def original_date @original_date end |
Class Method Details
.from_cache(cache_text, opts = {}) ⇒ Object
26 27 28 29 30 |
# File 'lib/noms/command/useragent/response.rb', line 26 def self.from_cache(cache_text, opts={}) unless cache_text.nil? or cache_text.empty? self.from_json(cache_text, opts) end end |
.from_json(json_text, opts = {}) ⇒ Object
32 33 34 |
# File 'lib/noms/command/useragent/response.rb', line 32 def self.from_json(json_text, opts={}) return self.new(JSON.parse(json_text), opts) end |
Instance Method Details
#age ⇒ Object
192 193 194 |
# File 'lib/noms/command/useragent/response.rb', line 192 def age Time.now - self.original_date end |
#body ⇒ Object
The response body
61 62 63 |
# File 'lib/noms/command/useragent/response.rb', line 61 def body @response['body'] end |
#cacheable? ⇒ Boolean
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/noms/command/useragent/response.rb', line 166 def cacheable? return false unless self.status == 200 return false unless @expires return false if self.from_cache? return false if /no-cache/.match @cache_control return false if /no-store/.match @cache_control return false if self.header 'Pragma' and /no-cache/.match self.header('Pragma') true end |
#cacheable_copy ⇒ Object
196 197 198 199 200 |
# File 'lib/noms/command/useragent/response.rb', line 196 def cacheable_copy other = self.dup other.logger = nil other end |
#cached! ⇒ Object
177 178 179 |
# File 'lib/noms/command/useragent/response.rb', line 177 def cached! @from_cache = true end |
#content_encoding ⇒ Object
103 104 105 106 107 |
# File 'lib/noms/command/useragent/response.rb', line 103 def content_encoding if self.content_type header_params(self.content_type)['charset'] end end |
#content_type ⇒ Object
MIME Type of content
91 92 93 |
# File 'lib/noms/command/useragent/response.rb', line 91 def content_type self.header 'Content-Type' end |
#current? ⇒ Boolean
185 186 187 188 189 190 |
# File 'lib/noms/command/useragent/response.rb', line 185 def current? return false if (@cache_control and /must-revalidate/.match @cache_control) return false unless @expires return false if Time.now > @expires true end |
#from_cache? ⇒ Boolean
181 182 183 |
# File 'lib/noms/command/useragent/response.rb', line 181 def from_cache? @from_cache end |
#get_expires ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/noms/command/useragent/response.rb', line 134 def get_expires expires = [ ] if @cache_control and (m = /max-age=(\d+)/.match(@cache_control)) expires << (@date + m[1].to_i) end if self.header('Expires') begin expires << Time.httpdate(self.header('Expires')) rescue ArgumentError => e @log.debug "Response had 'Expires' header but could not parse (#{e.class}): #{e.}" end end return nil if expires.empty? expires.empty? ? nil : expires.min end |
#get_header_time(hdr) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/noms/command/useragent/response.rb', line 155 def get_header_time(hdr) value = self.header hdr if value begin Time.httpdate(value) rescue ArgumentError => e @log.debug "Response has a '#{hdr}' but could not parse (#{e.class}): #{e.}" end end end |
#header(hdr = nil) ⇒ Object
A hash or headers, or specific header
72 73 74 75 76 77 78 |
# File 'lib/noms/command/useragent/response.rb', line 72 def header(hdr=nil) if hdr.nil? @response['header'] else Hash[@response['header'].map { |h, v| [h.downcase, v] }][hdr.downcase] unless @response.nil? end end |
#header_params(s) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/noms/command/useragent/response.rb', line 95 def header_params(s) Hash[s.split(';').map do |field| param, value = field.split('=', 2) value ||= '' [param.strip, value.strip] end] end |
#status ⇒ Object
An integer status code
81 82 83 |
# File 'lib/noms/command/useragent/response.rb', line 81 def status @response['status'] end |
#statusText ⇒ Object
The status message including code
86 87 88 |
# File 'lib/noms/command/useragent/response.rb', line 86 def statusText @response['statusText'] end |
#success? ⇒ Boolean
The success status
66 67 68 69 |
# File 'lib/noms/command/useragent/response.rb', line 66 def success? # We created this object explicitly, so it's always successful true end |
#to_cache ⇒ Object
113 114 115 |
# File 'lib/noms/command/useragent/response.rb', line 113 def to_cache self.to_json end |
#to_hash ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/noms/command/useragent/response.rb', line 117 def to_hash { 'body' => self.body, 'header' => self.header, 'status' => self.status, 'statusText' => self.statusText, 'auth_hash' => self.auth_hash, 'from_cache' => self.from_cache, 'date' => self.date.httpdate, 'original_date' => self.original_date.httpdate } end |
#to_json ⇒ Object
109 110 111 |
# File 'lib/noms/command/useragent/response.rb', line 109 def to_json self.to_hash.to_json end |