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.



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

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
    


262
263
264
265
266
267
268
269
270
271
# File 'lib/resourceful/header.rb', line 262

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



273
274
275
# File 'lib/resourceful/header.rb', line 273

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

.non_modifiable_fieldsObject



277
278
279
# File 'lib/resourceful/header.rb', line 277

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

Instance Method Details

#[](k) ⇒ Object



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

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

#[]=(k, v) ⇒ Object



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

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

#dupObject



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

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

#each(&blk) ⇒ Object



59
60
61
# File 'lib/resourceful/header.rb', line 59

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

#each_field(&blk) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/resourceful/header.rb', line 65

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)


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

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

#merge(another) ⇒ Object



84
85
86
# File 'lib/resourceful/header.rb', line 84

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

#merge!(another) ⇒ Object



77
78
79
80
81
82
# File 'lib/resourceful/header.rb', line 77

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

#reverse_merge(another) ⇒ Object



88
89
90
# File 'lib/resourceful/header.rb', line 88

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

#to_hashObject



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

def to_hash
  @raw_fields.dup
end