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(operation) ⇒ Message

Returns a new instance of Message.



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

def initialize(operation)
  @operation = operation
  freeze
end

Class Method Details

.parse_structured_dictionary(str, field_name = nil) ⇒ Object



45
46
47
48
49
# File 'lib/linzer/message.rb', line 45

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/linzer/message.rb', line 28

def [](field_name)
  if !field_name.start_with?("@")
    return @operation.env[Request.rack_header_name(field_name)] if request?
    return @operation.headers[field_name]                     # if response?
  end

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

#field?(f) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/linzer/message.rb', line 24

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

#headersObject



18
19
20
21
22
# File 'lib/linzer/message.rb', line 18

def headers
  return @operation.headers if response? || @operation.respond_to?(:headers)

  Request.headers(@operation)
end

#request?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/linzer/message.rb', line 10

def request?
  @operation.is_a?(Rack::Request) || @operation.respond_to?(:request_method)
end

#response?Boolean

Returns:

  • (Boolean)


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

def response?
  @operation.is_a?(Rack::Response) || @operation.respond_to?(:status)
end