Class: Mhtml::HttpHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/mhtml/http_header.rb

Defined Under Namespace

Classes: Value

Constant Summary collapse

KEY_VALUE_SEP =
':'.freeze
VALUE_SEP =
';'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_or_hash, value_lines = nil) ⇒ HttpHeader

Returns a new instance of HttpHeader.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mhtml/http_header.rb', line 10

def initialize(key_or_hash, value_lines = nil)
  if key_or_hash.is_a?(Hash)
    @key = key_or_hash[:key]
    @values = key_or_hash[:values]
    return
  end

  @key = key_or_hash
  @values = []
  values_str = value_lines.join('')

  values_str.split(VALUE_SEP).each do |val_str|
    val_str.strip!
    val = Value.new(val_str)

    if val.nil?
      raise "Invalid value:\n#{val_str}\n\nFrom string:\n#{val_str}"
    end

    @values << val
  end
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/mhtml/http_header.rb', line 5

def key
  @key
end

#valuesObject

Returns the value of attribute values.



5
6
7
# File 'lib/mhtml/http_header.rb', line 5

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
# File 'lib/mhtml/http_header.rb', line 33

def ==(other)
  @key == other.key && @values == other.values
end

#cloneObject



55
56
57
58
# File 'lib/mhtml/http_header.rb', line 55

def clone
  vals = @values.collect { |v| v.clone }
  HttpHeader.new(key: @key.clone, values: vals)
end

#to_sObject

following methods are for debugging only - no spec implemented



51
52
53
# File 'lib/mhtml/http_header.rb', line 51

def to_s
  "#{@key}#{KEY_VALUE_SEP} #{@values.join(VALUE_SEP + ' ')}"
end

#value(key) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mhtml/http_header.rb', line 37

def value(key)
  value = nil

  @values.each do |v|
    if v.key == key
      value = v
      break
    end
  end

  value
end