Class: Redhead::Header
- Inherits:
-
Object
- Object
- Redhead::Header
- Defined in:
- lib/redhead/header.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.parse(header_string) ⇒ Object
Parses a string representing a header.
Instance Method Summary collapse
-
#==(other_header) ⇒ Object
Returns true if other_header has the same raw header name and value as self.
-
#initialize(key, raw, value) ⇒ Header
constructor
A new instance of Header.
- #inspect ⇒ Object
-
#to_s(raw_name = nil) ⇒ Object
Returns the header as a string.
-
#to_s!(raw_name = nil) ⇒ Object
Does the same thing as #to_s, but instead of calling #raw in the last case, it computes the raw header name dynamically from the key.
Constructor Details
#initialize(key, raw, value) ⇒ Header
Returns a new instance of Header.
5 6 7 8 9 |
# File 'lib/redhead/header.rb', line 5 def initialize(key, raw, value) @key = key @raw = raw @value = value end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
3 4 5 |
# File 'lib/redhead/header.rb', line 3 def key @key end |
#raw ⇒ Object
Returns the value of attribute raw.
3 4 5 |
# File 'lib/redhead/header.rb', line 3 def raw @raw end |
#value ⇒ Object
Returns the value of attribute value.
3 4 5 |
# File 'lib/redhead/header.rb', line 3 def value @value end |
Class Method Details
.parse(header_string) ⇒ Object
Parses a string representing a header. Uses HEADER_NAME_VALUE_SEPARATOR_PATTERN to determine the name and value parts of the string.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/redhead/header.rb', line 13 def self.parse(header_string) header_string =~ HEADER_NAME_VALUE_SEPARATOR_PATTERN raw = $` value = $' key = TO_KEY[raw] header = new(key, raw, value) header end |
Instance Method Details
#==(other_header) ⇒ Object
Returns true if other_header has the same raw header name and value as self.
46 47 48 49 |
# File 'lib/redhead/header.rb', line 46 def ==(other_header) raw == other_header.raw && value == other_header.value end |
#inspect ⇒ Object
41 42 43 |
# File 'lib/redhead/header.rb', line 41 def inspect "{ #{key.inspect} => #{value.inspect} }" end |
#to_s(raw_name = nil) ⇒ Object
Returns the header as a string. If raw_name is given, this value is used as the raw header name. If raw_name is not given, do one of two things:
-
If a block is given, pass #key to the block and use the result as the raw header name.
-
If a block is not given, use #raw as the raw header name.
29 30 31 32 |
# File 'lib/redhead/header.rb', line 29 def to_s(raw_name = nil) r = raw_name || (block_given? ? yield(key) : raw) "#{r}#{Redhead::HEADER_NAME_VALUE_SEPARATOR_CHARACTER} #{value}" end |
#to_s!(raw_name = nil) ⇒ Object
Does the same thing as #to_s, but instead of calling #raw in the last case, it computes the raw header name dynamically from the key.
36 37 38 39 |
# File 'lib/redhead/header.rb', line 36 def to_s!(raw_name = nil) r = raw_name || (block_given? ? yield(key) : TO_RAW[key]) "#{r}#{Redhead::HEADER_NAME_VALUE_SEPARATOR_CHARACTER} #{value}" end |