Class: AgentX::Response::Headers
- Inherits:
-
Object
- Object
- AgentX::Response::Headers
- Includes:
- Enumerable
- Defined in:
- lib/agentx/response.rb
Defined Under Namespace
Classes: CacheControl
Class Method Summary collapse
Instance Method Summary collapse
- #[](k) ⇒ Object
- #age ⇒ Object
- #cache_control ⇒ Object
- #content_length ⇒ Object
- #content_type ⇒ Object
- #date ⇒ Object
- #each(&block) ⇒ Object
- #etag ⇒ Object
- #expires ⇒ Object
- #html? ⇒ Boolean
-
#initialize(hash = {}) ⇒ Headers
constructor
A new instance of Headers.
- #inspect ⇒ Object
- #json? ⇒ Boolean
- #last_modified ⇒ Object
- #location ⇒ Object
- #max_age ⇒ Object
- #merge(headers) ⇒ Object
- #server ⇒ Object
- #set_cookie ⇒ Object
- #to_hash ⇒ Object
- #ttl ⇒ Object
- #xml? ⇒ Boolean
Constructor Details
#initialize(hash = {}) ⇒ Headers
Returns a new instance of Headers.
78 79 80 81 82 83 84 85 |
# File 'lib/agentx/response.rb', line 78 def initialize(hash={}) @hash = hash @hash['Date'] ||= Time.now.httpdate @normalized = {} @hash.each do |k,v| @normalized[k.to_s.downcase] = v end end |
Class Method Details
.parse(str) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/agentx/response.rb', line 87 def self.parse(str) return new(str) if str.kind_of?(Hash) return str if str.kind_of?(Headers) hash = {} str.lines.each do |line| next if line =~ /^HTTP\/\d/ k, v = line.split(':', 2).map { |s| s.strip } if k && v if hash[k] hash[k] = Array(hash[k]) hash[k] << v else hash[k] = v end end end new(hash) end |
Instance Method Details
#[](k) ⇒ Object
122 123 124 |
# File 'lib/agentx/response.rb', line 122 def [](k) @hash[k] || @normalized[k.to_s.downcase] end |
#age ⇒ Object
134 135 136 |
# File 'lib/agentx/response.rb', line 134 def age (@normalized['age'] || (Time.now - date)).to_i end |
#cache_control ⇒ Object
184 185 186 187 188 |
# File 'lib/agentx/response.rb', line 184 def cache_control if @normalized['cache-control'] @cache_control ||= CacheControl.parse(@normalized['cache-control']) end end |
#content_length ⇒ Object
178 179 180 181 182 |
# File 'lib/agentx/response.rb', line 178 def content_length if (length = @normalized['content-length']) && length =~ /^\d+$/ length.to_i end end |
#content_type ⇒ Object
162 163 164 |
# File 'lib/agentx/response.rb', line 162 def content_type @normalized['content-type'] end |
#date ⇒ Object
130 131 132 |
# File 'lib/agentx/response.rb', line 130 def date Time.parse(@normalized['date']) end |
#each(&block) ⇒ Object
118 119 120 |
# File 'lib/agentx/response.rb', line 118 def each(&block) @normalized.each(&block) end |
#etag ⇒ Object
158 159 160 |
# File 'lib/agentx/response.rb', line 158 def etag @normalized['etag'] end |
#expires ⇒ Object
138 139 140 141 142 |
# File 'lib/agentx/response.rb', line 138 def expires if d = @normalized['expires'] Time.parse(d) end end |
#html? ⇒ Boolean
170 171 172 |
# File 'lib/agentx/response.rb', line 170 def html? content_type.to_s.downcase['html'] end |
#inspect ⇒ Object
114 115 116 |
# File 'lib/agentx/response.rb', line 114 def inspect "(Headers #{@normalized})" end |
#json? ⇒ Boolean
166 167 168 |
# File 'lib/agentx/response.rb', line 166 def json? content_type.to_s.downcase['json'] end |
#last_modified ⇒ Object
154 155 156 |
# File 'lib/agentx/response.rb', line 154 def last_modified @normalized['last-modified'] end |
#location ⇒ Object
194 195 196 |
# File 'lib/agentx/response.rb', line 194 def location @normalized['location'] end |
#max_age ⇒ Object
144 145 146 147 148 |
# File 'lib/agentx/response.rb', line 144 def max_age (cache_control && (cache_control.shared_max_age || cache_control.max_age)) || (expires && (expires - Time.now)) end |
#merge(headers) ⇒ Object
110 111 112 |
# File 'lib/agentx/response.rb', line 110 def merge(headers) Headers.new(to_hash.merge(headers.to_hash)) end |
#server ⇒ Object
126 127 128 |
# File 'lib/agentx/response.rb', line 126 def server @normalized['server'] end |
#set_cookie ⇒ Object
190 191 192 |
# File 'lib/agentx/response.rb', line 190 def @normalized['set-cookie'] end |
#to_hash ⇒ Object
198 199 200 |
# File 'lib/agentx/response.rb', line 198 def to_hash @hash end |
#ttl ⇒ Object
150 151 152 |
# File 'lib/agentx/response.rb', line 150 def ttl max_age && (max_age - age) end |
#xml? ⇒ Boolean
174 175 176 |
# File 'lib/agentx/response.rb', line 174 def xml? content_type.to_s.downcase['xml'] end |