Class: AgentX::Response::Headers

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/agentx/response.rb

Defined Under Namespace

Classes: CacheControl

Class Method Summary collapse

Instance Method Summary collapse

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

#ageObject



134
135
136
# File 'lib/agentx/response.rb', line 134

def age
  (@normalized['age'] || (Time.now - date)).to_i
end

#cache_controlObject



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_lengthObject



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_typeObject



162
163
164
# File 'lib/agentx/response.rb', line 162

def content_type
  @normalized['content-type']
end

#dateObject



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

#etagObject



158
159
160
# File 'lib/agentx/response.rb', line 158

def etag
  @normalized['etag']
end

#expiresObject



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

Returns:

  • (Boolean)


170
171
172
# File 'lib/agentx/response.rb', line 170

def html?
  content_type.to_s.downcase['html']
end

#inspectObject



114
115
116
# File 'lib/agentx/response.rb', line 114

def inspect
  "(Headers #{@normalized})"
end

#json?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/agentx/response.rb', line 166

def json?
  content_type.to_s.downcase['json']
end

#last_modifiedObject



154
155
156
# File 'lib/agentx/response.rb', line 154

def last_modified
  @normalized['last-modified']
end

#locationObject



194
195
196
# File 'lib/agentx/response.rb', line 194

def location
  @normalized['location']
end

#max_ageObject



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

#serverObject



126
127
128
# File 'lib/agentx/response.rb', line 126

def server
  @normalized['server']
end


190
191
192
# File 'lib/agentx/response.rb', line 190

def set_cookie
  @normalized['set-cookie']
end

#to_hashObject



198
199
200
# File 'lib/agentx/response.rb', line 198

def to_hash
  @hash
end

#ttlObject



150
151
152
# File 'lib/agentx/response.rb', line 150

def ttl
  max_age && (max_age - age)
end

#xml?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/agentx/response.rb', line 174

def xml?
  content_type.to_s.downcase['xml']
end