Module: Myxi

Defined in:
lib/myxi.rb,
lib/myxi/action.rb,
lib/myxi/server.rb,
lib/myxi/railtie.rb,
lib/myxi/session.rb,
lib/myxi/version.rb,
lib/myxi/exchange.rb,
lib/myxi/listener.rb,
lib/myxi/environment.rb,
lib/myxi/eventable_socket.rb

Defined Under Namespace

Classes: Action, Environment, Error, EventableSocket, Exchange, Listener, Railtie, Server, Session

Constant Summary collapse

VERSION =
'1.4.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.bunnyObject

Return a bunny client instance which will be used by the web socket service. This can be overriden if you already have a connection RabbitMQ available if your application. By default, it will connect to localhost or use the RABBITMQ_URL environment variable.



24
25
26
27
28
29
30
31
# File 'lib/myxi.rb', line 24

def bunny
  @bunny ||= begin
    require 'bunny'
    bunny = Bunny.new(ENV['RABBITMQ_URL'])
    bunny.start
    bunny
  end
end

.loggerObject

Return a logger



13
14
15
# File 'lib/myxi.rb', line 13

def logger
  @logger ||= Logger.new(STDOUT)
end

Class Method Details

.channelObject

Return a channel which this process can always use



37
38
39
# File 'lib/myxi.rb', line 37

def channel
  @channel ||= bunny.create_channel
end

.exchangesObject

Store a bool of configured exchanges



44
45
46
47
48
49
50
51
# File 'lib/myxi.rb', line 44

def exchanges
  @exchanges ||= begin
    Myxi::Exchange::EXCHANGES.keys.inject({}) do |hash, name|
      hash[name.to_sym] = channel.direct(name.to_s)
      hash
    end
  end
end

.push(exchange, routing_key, &block) ⇒ Object

Push data to a given



56
57
58
59
60
61
62
# File 'lib/myxi.rb', line 56

def push(exchange, routing_key, &block)
  if exch = exchanges[exchange.to_sym]
    block.call(exch)
  else
    raise Error, "Couldn't send message to '#{exchange}' as it isn't configured"
  end
end

.push_event(exchange, routing_key, event, payload = {}) ⇒ Object

Send an event to the given exchange



67
68
69
70
71
72
# File 'lib/myxi.rb', line 67

def push_event(exchange, routing_key, event, payload = {})
  push(exchange, routing_key) do |exch|
    exch.publish({:event => event, :payload => payload}.to_json, :routing_key => routing_key.to_s)
  end
  true
end