Class: Resourceful::Header

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/resourceful/header.rb

Defined Under Namespace

Classes: FieldDesc

Constant Summary collapse

@@known_fields =

FieldDesc

Set.new
@@known_fields_lookup =
Hash.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Header

Returns a new instance of Header.



36
37
38
39
# File 'lib/resourceful/header.rb', line 36

def initialize(hash={})
  @raw_fields = {}
  hash.each { |k, v| self[k] = v }
end

Class Method Details

.header_field(name, options = {}) ⇒ Object

Declares a common header field. Header fields do not have to be defined this way but accessing them is easier, safer and faster if you do. Declaring a field does the following things:

* defines accessor methods (e.g. `#content_type` and
  `#content_type=`) on `Header`

* defines constant that can be used to reference there field
  name (e.g. `some_header[Header::CONTENT_TYPE]`)

* includes the field in the appropriate *_fields groups (e.g. `Header.non_modifiable_fields`)

* provides improved multiple value parsing

Create a new header field descriptor.

Parameters:

  • name (String)

    The canonical name of this field.

  • options (Hash) (defaults to: {})

    hash containing extra information about this header fields. Valid keys are:

    `:multivalued`
    `:multivalue`
    `:repeatable`
    :   Values of this field are comma separated list of values.  
        (n#VALUE per HTTP spec.) Default: false
    
    `:hop_by_hop`
    :   True if the header is a hop-by-hop header. Default: false
    
    `:modifiable`
    :   False if the header should not be modified by intermediates or caches. Default: true
    


273
274
275
276
277
278
279
280
281
282
# File 'lib/resourceful/header.rb', line 273

def self.header_field(name, options = {})
  hfd = FieldDesc.new(name, options)
  
  @@known_fields << hfd      
  hfd.lookup_keys do |a_key|
    @@known_fields_lookup[a_key] = hfd
  end

  include(hfd.accessor_module)
end

.hop_by_hop_fieldsObject



284
285
286
# File 'lib/resourceful/header.rb', line 284

def self.hop_by_hop_fields
  @@known_fields.select{|hfd| hfd.hop_by_hop?}
end

.non_modifiable_fieldsObject



288
289
290
# File 'lib/resourceful/header.rb', line 288

def self.non_modifiable_fields
  @@known_fields.reject{|hfd| hfd.modifiable?}
end

Instance Method Details

#[](k) ⇒ Object



45
46
47
# File 'lib/resourceful/header.rb', line 45

def [](k)
  field_def(k).get_from(@raw_fields)
end

#[]=(k, v) ⇒ Object



49
50
51
# File 'lib/resourceful/header.rb', line 49

def []=(k, v)
  field_def(k).set_to(v, @raw_fields)
end

#delete(k) ⇒ Object



53
54
55
# File 'lib/resourceful/header.rb', line 53

def delete(k)
  field_def(k).delete(@raw_fields)
end

#dupObject



99
100
101
# File 'lib/resourceful/header.rb', line 99

def dup
  self.class.new(@raw_fields.dup)
end

#each(&blk) ⇒ Object



62
63
64
# File 'lib/resourceful/header.rb', line 62

def each(&blk)
  @raw_fields.each(&blk)
end

#each_field(&blk) ⇒ Object

Iterates through the fields with values provided as message ready strings.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/resourceful/header.rb', line 68

def each_field(&blk)
  each do |k,v|
    str_v = if field_def(k).multivalued?
              v.join(', ')
            else
              v.to_s
            end

    yield k, str_v
  end
end

#has_key?(k) ⇒ Boolean Also known as: has_field?

Returns:

  • (Boolean)


57
58
59
# File 'lib/resourceful/header.rb', line 57

def has_key?(k)
  field_def(k).exists_in?(@raw_fields)
end

#merge(another) ⇒ Object



91
92
93
# File 'lib/resourceful/header.rb', line 91

def merge(another)
  self.class.new(self).merge!(another)
end

#merge!(another) ⇒ Object



80
81
82
83
84
85
# File 'lib/resourceful/header.rb', line 80

def merge!(another)
  another.each do |k,v|
    self[k] = v
  end
  self
end

#reverse_merge(another) ⇒ Object



95
96
97
# File 'lib/resourceful/header.rb', line 95

def reverse_merge(another)
  self.class.new(another).merge!(self)
end

#to_hashObject



41
42
43
# File 'lib/resourceful/header.rb', line 41

def to_hash
  @raw_fields.dup
end