Class: Hx::Interop::Headers

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

Overview

Represents MIME headers for a message.

Constant Summary collapse

ID =
'Interop-Rpc-Id'.freeze
CLASS =
'Interop-Rpc-Class'.freeze
ERROR =
'Interop-Error'.freeze
CONTENT_TYPE =
'Content-Type'.freeze
CONTENT_LENGTH =
'Content-Length'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(headers = nil) ⇒ Headers

Returns a new instance of Headers.



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

def initialize(headers = nil)
  @headers = {}
  merge! headers unless headers.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object (private)



69
70
71
72
73
74
75
# File 'lib/interop/headers.rb', line 69

def method_missing(symbol, *args, &block)
  if @headers.respond_to? symbol
    @headers.__send__ symbol, *args, &block
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  @headers[canonical_key key]
end

#[]=(key, value) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/interop/headers.rb', line 20

def []=(key, value)
  key = canonical_key(key)
  if value.nil?
    @headers.delete key
  else
    @headers[key] = value.to_s
  end
end

#dupObject



46
47
48
49
50
51
52
# File 'lib/interop/headers.rb', line 46

def dup
  headers = @headers.dup
  self.class.new.instance_exec do
    @headers = headers
    self
  end
end

#fetch(key, *args, &block) ⇒ Object



42
43
44
# File 'lib/interop/headers.rb', line 42

def fetch(key, *args, &block)
  @headers.fetch canonical_key(key, *args, &block)
end

#freezeObject



54
55
56
57
# File 'lib/interop/headers.rb', line 54

def freeze
  @headers.freeze
  super
end

#inspectObject



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

def inspect
  "<##{self.class.name} #{@headers.inspect[1..-2]}>"
end

#merge(*others) ⇒ Object



38
39
40
# File 'lib/interop/headers.rb', line 38

def merge(*others)
  dup.merge! others
end

#merge!(*others) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/interop/headers.rb', line 29

def merge!(*others)
  others.each do |other|
    other.each do |key, value|
      self[key] = value
    end
  end
  self
end

#to_sObject



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

def to_s
  map { |k, v| "#{k}: #{v}" }.join "\n"
end