Class: Motion::HTTP::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/common/http/headers.rb

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}) ⇒ Headers

Returns a new instance of Headers.



4
5
6
7
8
9
# File 'lib/common/http/headers.rb', line 4

def initialize(headers = {})
  @headers = {}
  if headers
    headers.each {|key, value| set(key, value) }
  end
end

Instance Method Details

#<<(key, value) ⇒ Object

alias :<< :add # FIXME: doesn’t work in Android



36
37
38
# File 'lib/common/http/headers.rb', line 36

def <<(key, value)
  add(key, value)
end

#[](key) ⇒ Object

alias :[] :get # FIXME: doesn’t work in Android



15
16
17
# File 'lib/common/http/headers.rb', line 15

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object

alias :[]= :set # FIXME: doesn’t work in Android



23
24
25
# File 'lib/common/http/headers.rb', line 23

def []=(key, value)
  set(key, value)
end

#add(key, value) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/common/http/headers.rb', line 27

def add(key, value)
  key = key.downcase
  @headers[key] ||= []
  unless @headers[key].is_a?(Array)
    @headers[key] = [@headers[key]]
  end
  @headers[key] << value
end

#dupObject

FIXME: Android doesn’t support dup (Java exception raised: java.lang.CloneNotSupportedException: Class com.yourcompany.motion_http.Headers doesn’t implement Cloneable)



49
50
51
# File 'lib/common/http/headers.rb', line 49

def dup
  Headers.new(@headers)
end

#each(&block) ⇒ Object



40
41
42
# File 'lib/common/http/headers.rb', line 40

def each(&block)
  @headers.each(&block)
end

#get(key) ⇒ Object



11
12
13
# File 'lib/common/http/headers.rb', line 11

def get(key)
  @headers[key.downcase]
end

#set(key, value) ⇒ Object



19
20
21
# File 'lib/common/http/headers.rb', line 19

def set(key, value)
  @headers[key.downcase] = value
end

#to_hashObject



44
45
46
# File 'lib/common/http/headers.rb', line 44

def to_hash
  @headers # TODO: flatten array values
end