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)


10
11
12
# File 'lib/dizby/protocol/manager.rb', line 10

def add_protocol(klass)
  @protocols << klass
end

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


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

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)


55
56
57
58
59
# File 'lib/dizby/protocol/manager.rb', line 55

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

  $~[1..-1]
end

.get_protocol(uri) (private)


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

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)


14
15
16
# File 'lib/dizby/protocol/manager.rb', line 14

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

.open_server(uri, front, config)


18
19
20
# File 'lib/dizby/protocol/manager.rb', line 18

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

.refine_protocol(protocol, refinement) (private)


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

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)


22
23
24
# File 'lib/dizby/protocol/manager.rb', line 22

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