Class: RaptorIO::Protocol::HTTP::Headers
- Defined in:
- lib/raptor-io/protocol/http/headers.rb
Overview
Class Method Summary collapse
Instance Method Summary collapse
-
#[](field) ⇒ String
Field value.
-
#[]=(field, value) ⇒ String
Field ‘value`.
-
#cookies ⇒ Array<Hash>
Request cookies.
-
#delete(field) ⇒ String
Field value.
-
#include?(field) ⇒ String
Field value.
-
#initialize(headers = {}) ⇒ Headers
constructor
A new instance of Headers.
-
#parsed_set_cookie ⇒ Array<Hash>
Cookies as hashes.
-
#set_cookie ⇒ Array<String>
Set-cookie strings.
-
#to_s ⇒ String
HTTP headers formatted for transmission.
Methods inherited from Hash
Constructor Details
#initialize(headers = {}) ⇒ Headers
Returns a new instance of Headers.
17 18 19 20 21 |
# File 'lib/raptor-io/protocol/http/headers.rb', line 17 def initialize( headers = {} ) (headers || {}).each do |k, v| self[k] = v end end |
Class Method Details
.parse(headers_string) ⇒ Headers
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/raptor-io/protocol/http/headers.rb', line 110 def self.parse( headers_string ) return Headers.new if headers_string.to_s.empty? headers = Hash.new { |h, k| h[k] = [] } headers_string.split( CRLF_PATTERN ).each do |header| k, v = header.split( ':', 2 ) headers[k.to_s.strip] << v.to_s.strip end headers.each { |k, v| headers[k] = v.first if v.size == 1 } new headers end |
Instance Method Details
#[](field) ⇒ String
Note:
‘field` will be capitalized appropriately before storing.
Returns Field value.
40 41 42 |
# File 'lib/raptor-io/protocol/http/headers.rb', line 40 def []( field ) super format_field_name( field.to_s.downcase ) end |
#[]=(field, value) ⇒ String
Note:
‘field` will be capitalized appropriately before storing.
Returns Field ‘value`.
48 49 50 51 |
# File 'lib/raptor-io/protocol/http/headers.rb', line 48 def []=( field, value ) super format_field_name( field.to_s.downcase ), value.is_a?( Array ) ? value : value.to_s end |
#cookies ⇒ Array<Hash>
Returns Request cookies.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/raptor-io/protocol/http/headers.rb', line 79 def return [] if !self['cookie'] WEBrick::Cookie.parse( self['cookie'] ).flatten.uniq.map do || = {} .instance_variables.each do |var| [var.to_s.gsub( /@/, '' ).to_sym] = .instance_variable_get( var ) end # Replace the string with a Time object. [:expires] = .expires end end |
#delete(field) ⇒ String
Note:
‘field` will be capitalized appropriately before storing.
Returns Field value.
26 27 28 |
# File 'lib/raptor-io/protocol/http/headers.rb', line 26 def delete( field ) super format_field_name( field.to_s.downcase ) end |
#include?(field) ⇒ String
Note:
‘field` will be capitalized appropriately before storing.
Returns Field value.
33 34 35 |
# File 'lib/raptor-io/protocol/http/headers.rb', line 33 def include?( field ) super format_field_name( field.to_s.downcase ) end |
#parsed_set_cookie ⇒ Array<Hash>
Returns Cookies as hashes.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/raptor-io/protocol/http/headers.rb', line 60 def return [] if .empty? .map { || WEBrick::Cookie.( ).flatten.uniq.map do || = {} .instance_variables.each do |var| [var.to_s.gsub( /@/, '' ).to_sym] = .instance_variable_get( var ) end # Replace the string with a Time object. [:expires] = .expires end }.flatten.compact end |
#set_cookie ⇒ Array<String>
Returns Set-cookie strings.
54 55 56 57 |
# File 'lib/raptor-io/protocol/http/headers.rb', line 54 def return [] if self['set-cookie'].to_s.empty? [self['set-cookie']].flatten end |