Class: Linzer::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/linzer/message.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_data) ⇒ Message

Returns a new instance of Message.



5
6
7
8
9
10
11
# File 'lib/linzer/message.rb', line 5

def initialize(request_data)
  @http    = Hash(request_data[:http].clone).freeze
  @headers = Hash(request_data.fetch(:headers, {})
                    .transform_keys(&:downcase)
                    .clone).freeze
  freeze
end

Class Method Details

.parse_structured_dictionary(str, field_name = nil) ⇒ Object



39
40
41
42
43
# File 'lib/linzer/message.rb', line 39

def parse_structured_dictionary(str, field_name = nil)
  Starry.parse_dictionary(str)
rescue Starry::ParseError => _
  raise Error.new "Cannot parse \"#{field_name}\" field. Bailing out!"
end

Instance Method Details

#[](field_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/linzer/message.rb', line 25

def [](field_name)
  return @headers[field_name] if !field_name.start_with?("@")

  case field_name
  when "@method"    then @http["method"]
  when "@authority" then @http["host"]
  when "@path"      then @http["path"]
  when "@status"    then @http["status"]
  else # XXX: improve this and add support for all fields in the RFC
    raise Error.new "Unknown/unsupported field: \"#{field_name}\""
  end
end

#empty?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/linzer/message.rb', line 13

def empty?
  @headers.empty?
end

#field?(f) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/linzer/message.rb', line 21

def field?(f)
  !!self[f]
end

#header?(header) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/linzer/message.rb', line 17

def header?(header)
  @headers.key?(header)
end