Class: Protocol::WebSocket::Extensions::Client

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 = []) ⇒ Client

Returns a new instance of Client.



33
34
35
36
# File 'lib/protocol/websocket/extensions.rb', line 33

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

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



39
40
41
# File 'lib/protocol/websocket/extensions.rb', line 39

def accepted
  @accepted
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



38
39
40
# File 'lib/protocol/websocket/extensions.rb', line 38

def extensions
  @extensions
end

Class Method Details

.defaultObject



27
28
29
30
31
# File 'lib/protocol/websocket/extensions.rb', line 27

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

Instance Method Details

#accept(headers) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/protocol/websocket/extensions.rb', line 55

def accept(headers)
	named = self.named
	
	# Each response header should map to at least one extension.
	Extensions.parse(headers) do |name, arguments|
		if extension = named.delete(name)
			klass, options = extension
			
			options = klass.accept(arguments, **options)
			
			@accepted << [klass, options]
		end
	end
end

#apply(connection) ⇒ Object



70
71
72
73
74
# File 'lib/protocol/websocket/extensions.rb', line 70

def apply(connection)
	@accepted.each do |(klass, options)|
		klass.client(connection, **options)
	end
end

#namedObject



41
42
43
44
45
# File 'lib/protocol/websocket/extensions.rb', line 41

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

#offerObject



47
48
49
50
51
52
53
# File 'lib/protocol/websocket/extensions.rb', line 47

def offer
	@extensions.each do |extension, options|
		if header = extension.offer(**options)
			yield header
		end
	end
end