Class: Protocol::WebSocket::Extensions::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/websocket/extensions.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extensions) ⇒ Server

Returns a new instance of Server.



86
87
88
89
# File 'lib/protocol/websocket/extensions.rb', line 86

def initialize(extensions)
  @extensions = extensions
  @accepted = []
end

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



92
93
94
# File 'lib/protocol/websocket/extensions.rb', line 92

def accepted
  @accepted
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



91
92
93
# File 'lib/protocol/websocket/extensions.rb', line 91

def extensions
  @extensions
end

Class Method Details

.defaultObject



80
81
82
83
84
# File 'lib/protocol/websocket/extensions.rb', line 80

def self.default
  self.new([
    [Extension::Compression, {}]
  ])
end

Instance Method Details

#accept(headers) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/protocol/websocket/extensions.rb', line 100

def accept(headers)
  extensions = []
  
  named = self.named
  response = []
  
  # Each response header should map to at least one extension.
  Extensions.parse(headers) do |name, arguments|
    if extension = named[name]
      klass, options = extension
      
      if result = klass.negotiate(arguments, **options)
        header, options = result
        
        # The extension is accepted and no further offers will be considered:
        named.delete(name)
        
        yield header if block_given?
        
        @accepted << [klass, options]
      end
    end
  end
  
  return @accepted
end

#apply(connection) ⇒ Object



127
128
129
130
131
# File 'lib/protocol/websocket/extensions.rb', line 127

def apply(connection)
  @accepted.reverse_each do |(klass, options)|
    klass.server(connection, **options)
  end
end

#namedObject



94
95
96
97
98
# File 'lib/protocol/websocket/extensions.rb', line 94

def named
  @extensions.map do |extension|
    [extension.first::NAME, extension]
  end.to_h
end