Class: Xenon::Headers

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/xenon/headers.rb,
lib/xenon/headers/accept.rb,
lib/xenon/headers/if_match.rb,
lib/xenon/headers/if_range.rb,
lib/xenon/headers/user_agent.rb,
lib/xenon/headers/content_type.rb,
lib/xenon/headers/authorization.rb,
lib/xenon/headers/cache_control.rb,
lib/xenon/headers/if_none_match.rb,
lib/xenon/headers/accept_charset.rb,
lib/xenon/headers/content_length.rb,
lib/xenon/headers/accept_encoding.rb,
lib/xenon/headers/accept_language.rb,
lib/xenon/headers/www_authenticate.rb,
lib/xenon/headers/if_modified_since.rb,
lib/xenon/headers/if_unmodified_since.rb

Defined Under Namespace

Classes: Accept, AcceptCharset, AcceptEncoding, AcceptLanguage, Authorization, CacheControl, Challenge, ContentLength, ContentType, IfMatch, IfModifiedSince, IfNoneMatch, IfRange, IfUnmodifiedSince, Raw, UserAgent, WWWAuthenticate

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeaders

Returns a new instance of Headers.



7
8
9
# File 'lib/xenon/headers.rb', line 7

def initialize
  @hash = {}
end

Class Method Details

.Header(name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xenon/headers.rb', line 67

def Header(name)
  klass = Class.new do
    def name
      self.class.const_get(:NAME)
    end

    def self.inherited(base)
      Headers.register(base)
    end
  end
  Headers.const_set("#{name.tr('-', '_').classify}Header", klass)
  klass.const_set(:NAME, name)
  klass
end

.header_class(name) ⇒ Object



63
64
65
# File 'lib/xenon/headers.rb', line 63

def header_class(name)
  (@registered || {})[name]
end

.ListHeader(name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/xenon/headers.rb', line 82

def ListHeader(name)
  klass = Header(name)
  klass.class_eval do
    attr_reader :values

    def initialize(values)
      @values = values
    end

    def merge(other)
      self.class.new(*(@values + other.values))
    end

    def to_s
      @values.map(&:to_s).join(', ')
    end
  end
  klass
end

.register(klass) ⇒ Object



59
60
61
# File 'lib/xenon/headers.rb', line 59

def register(klass)
  (@registered ||= {})[klass.const_get(:NAME)] = klass
end

Instance Method Details

#add!(header) ⇒ Object Also known as: <<



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/xenon/headers.rb', line 30

def add!(header)
  existing = @hash[header.name]
  if existing
    if existing.respond_to?(:merge)
      set!(existing.merge(header))
    else
      raise "Unmergeable header '#{header.name}' already exists"
    end
  else
    set!(header)
  end
  self
end

#each(&block) ⇒ Object



21
22
23
# File 'lib/xenon/headers.rb', line 21

def each(&block)
  @hash.values.each(&block)
end

#freezeObject



16
17
18
19
# File 'lib/xenon/headers.rb', line 16

def freeze
  @hash.freeze
  super
end

#initialize_dup(other) ⇒ Object



11
12
13
14
# File 'lib/xenon/headers.rb', line 11

def initialize_dup(other)
  super
  @hash = @hash.dup
end

#remove!(header) ⇒ Object



44
45
46
47
48
# File 'lib/xenon/headers.rb', line 44

def remove!(header)
  header = header.name if header.respond_to?(:name)
  @hash.delete(header)
  self
end

#set!(header) ⇒ Object



25
26
27
28
# File 'lib/xenon/headers.rb', line 25

def set!(header)
  @hash[header.name] = header
  self
end