Class: Dizby::ProtocolManager

Inherits:
Object
  • Object
show all
Defined in:
lib/dizby/protocol/manager.rb

Class Method Summary collapse

Class Method Details

.add_protocol(klass)


8
9
10
# File 'lib/dizby/protocol/manager.rb', line 8

def add_protocol(klass)
  @protocols << klass
end

.call_refined(uri, refiner, *base_args) (private)


26
27
28
29
30
31
# File 'lib/dizby/protocol/manager.rb', line 26

def call_refined(uri, refiner, *base_args)
  klass = get_protocol(uri)
  refined = refine_protocol(klass, refiner)
  args = get_arguments(refined, uri)
  refined.call(*base_args, args)
end

.get_arguments(refined, uri) (private)


53
54
55
56
57
# File 'lib/dizby/protocol/manager.rb', line 53

def get_arguments(refined, uri)
  fail BadURI, "can't parse uri: #{uri}" unless refined.regex =~ uri

  $~[1..-1]
end

.get_protocol(uri) (private)


33
34
35
36
37
38
39
40
# File 'lib/dizby/protocol/manager.rb', line 33

def get_protocol(uri)
  scheme = '' if uri.empty?
  scheme ||= uri.split(':').first
  fail BadScheme, "can't retrieve scheme: #{uri}" unless scheme

  protocol = @protocols.find { |klass| klass.scheme == scheme }
  protocol || fail(BadScheme, "scheme not found: #{scheme}")
end

.open_client(server, uri)


12
13
14
# File 'lib/dizby/protocol/manager.rb', line 12

def open_client(server, uri)
  call_refined(uri, :client, server)
end

.open_server(uri, front, config)


16
17
18
# File 'lib/dizby/protocol/manager.rb', line 16

def open_server(uri, front, config)
  call_refined(uri, :server, front, config)
end

.refine_protocol(protocol, refinement) (private)


42
43
44
45
46
47
48
49
50
51
# File 'lib/dizby/protocol/manager.rb', line 42

def refine_protocol(protocol, refinement)
  refined = protocol.get_refinement(refinement)

  unless refined
    fail NotImplementedError,
         "#{refinement} refinement not supported for #{protocol}"
  end

  refined
end

.spawn_server(server, command, uri)


20
21
22
# File 'lib/dizby/protocol/manager.rb', line 20

def spawn_server(server, command, uri)
  call_refined(uri, :spawn, server, command)
end