Class: BumpyBridge

Inherits:
Object
  • Object
show all
Includes:
Trickery::Config
Defined in:
lib/bumpy_bridge.rb,
lib/bumpy_bridge/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ BumpyBridge

Returns a new instance of BumpyBridge.



21
22
23
# File 'lib/bumpy_bridge.rb', line 21

def initialize(file)
  self.config = read_config(file)
end

Instance Attribute Details

#bunnyObject

Returns the value of attribute bunny.



19
20
21
# File 'lib/bumpy_bridge.rb', line 19

def bunny
  @bunny
end

#bunny_channelObject

Returns the value of attribute bunny_channel.



19
20
21
# File 'lib/bumpy_bridge.rb', line 19

def bunny_channel
  @bunny_channel
end

#configObject

Returns the value of attribute config.



19
20
21
# File 'lib/bumpy_bridge.rb', line 19

def config
  @config
end

#fayeObject

Returns the value of attribute faye.



19
20
21
# File 'lib/bumpy_bridge.rb', line 19

def faye
  @faye
end

Instance Method Details

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bumpy_bridge.rb', line 25

def run
  # setup rabbitmq
  self.bunny = Bunny.new read_timeout: 10, heartbeat: 10
  bunny.start
  self.bunny_channel = bunny.create_channel

  # setup faye
  EM.run {
    self.faye = Faye::Client.new(config.faye.server)
    auth_ext = Faye::Authentication::ClientExtension.new(config.faye.secret_token)
    faye.add_extension(auth_ext)

    # establish callbacks
    config.mappings.each do |mapping|
      sprot, schan = mapping.source.split('://')
      tprot, tchan = mapping.target.split('://')
      case [sprot, tprot] * ' -> '
      when 'faye -> rabbitmq' then faye2bunny(schan, tchan, mapping)
      when 'rabbitmq -> faye' then bunny2faye(schan, tchan, mapping)
      else warn "Unknown mapping: #{sprot} -> #{tprot}"
      end
    end
  }
rescue => e
  puts "FATAL: #{e}"
end