class GorgMessageSender

Add configuration features to GorgService

Constants

JSON_SCHEMA
VERSION

Attributes

configuration[W]

Public Class Methods

configuration() click to toggle source
# File lib/gorg_message_sender/configuration.rb, line 6
def configuration
  @configuration ||= GorgMessageSender::Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/gorg_message_sender/configuration.rb, line 11
def configure
  @configuration = GorgMessageSender::Configuration.new
  yield(configuration)
end
new(host: self.class.configuration.host, port: self.class.configuration.port, user: self.class.configuration.user, pass:self.class.configuration.password, exchange_name:self.class.configuration.exchange_name, vhost: self.class.configuration.vhost, app_id: self.class.configuration.application_id, durable_exchange: self.class.configuration.durable_exchange) click to toggle source
# File lib/gorg_message_sender.rb, line 9
def initialize(host: self.class.configuration.host,
               port: self.class.configuration.port,
               user: self.class.configuration.user,
               pass:self.class.configuration.password,
               exchange_name:self.class.configuration.exchange_name,
               vhost: self.class.configuration.vhost,
               app_id: self.class.configuration.application_id,
               durable_exchange: self.class.configuration.durable_exchange)
  @r_host=host
  @r_port=port
  @r_user=user
  @r_pass=pass
  @r_exchange=exchange_name
  @r_vhost=vhost
  @app_id=app_id
  @r_durable=durable_exchange
end

Protected Class Methods

conn(url) click to toggle source
# File lib/gorg_message_sender.rb, line 83
def self.conn(url)
  @conns||=Hash.new(nil)
  @conns[url]||=Bunny.new(url)
  @conns[url].start unless @conns[url].connected?
  @conns[url]
end

Public Instance Methods

message(data,routing_key, opts={})
Alias for: to_message
send_batch_raw(msgs,opts={}) click to toggle source
# File lib/gorg_message_sender.rb, line 59
def send_batch_raw(msgs,opts={})
  self.start(verbose: opts[:verbose])
  p_opts={}
  msgs.each do |msg|
    @x.publish(msg[:content], routing_key: msg[:routing_key] )
  end
end
send_message(data,routing_key,opts={}) click to toggle source
# File lib/gorg_message_sender.rb, line 40
def send_message(data,routing_key,opts={})
  self.start(verbose: opts[:verbose])
  p_opts={}
  p_opts[:routing_key]= routing_key if routing_key
  msg=self.to_message(data,routing_key,opts)
  @x.publish(msg, p_opts)
  puts " [#] Message sent to exchange '#{@r_exchange}' (#{@r_durable ? "" : "not "}durable) with routing key '#{routing_key}'" if opts[:verbose]
  msg
end
send_raw(msg,routing_key,opts={}) click to toggle source
# File lib/gorg_message_sender.rb, line 50
def send_raw(msg,routing_key,opts={})
  self.start(verbose: opts[:verbose])
  p_opts={}
  p_opts[:routing_key]= routing_key if routing_key
  @x.publish(msg, p_opts)
  puts " [#] Message sent to exchange '#{@r_exchange}' (#{@r_durable ? "" : "not "}durable) with routing key '#{routing_key}'" if opts[:verbose]
  msg
end
to_message(data,routing_key, opts={}) click to toggle source
# File lib/gorg_message_sender.rb, line 27
def to_message(data,routing_key, opts={})
  json_msg={
    "event_uuid" => opts[:event_uuid]||SecureRandom.uuid,
    "event_name" => routing_key,
    "event_creation_time" => (opts[:event_creation_time]&&(opts[:event_creation_time].respond_to?(:iso8601) ? opts[:event_creation_time].iso8601 : opts[:event_creation_time].to_s)) ||DateTime.now.iso8601,
    "event_sender_id" => opts[:event_sender_id]|| @app_id,
    "data"=> data,
  }.to_json
  JSON::Validator.validate!(GorgMessageSender::JSON_SCHEMA,json_msg) unless opts[:skip_validation]
  json_msg
end
Also aliased as: message

Protected Instance Methods

ch() click to toggle source
# File lib/gorg_message_sender.rb, line 90
def ch
  @_ch = (@_ch && @_ch.status == :open) ? @_ch : self.class.conn(conn_id).create_channel
end
conn_id() click to toggle source
# File lib/gorg_message_sender.rb, line 70
def conn_id
  userpart=""
  if @r_user
    userpart=URI.escape(@r_user.to_s,"@:/")
    userpart+=":#{URI.escape(@r_pass.to_s,'%@:/\#^')}" if @r_pass
    userpart+="@"
  end
  portpart= @r_port ? ":#{URI.escape(@r_port.to_s,"@:/")}" : ""
  vhostpart= @r_vhost ? "/#{URI.escape(@r_vhost.to_s,"@:/")}" : ""

  "amqp://#{userpart}#{URI.escape(@r_host,"@:/")}#{portpart}#{vhostpart}"
end
start(opts) click to toggle source
# File lib/gorg_message_sender.rb, line 94
def start(opts)
  @x  = ch.topic(@r_exchange, :durable => @r_durable)
  puts " [#] Connected as user '#{@r_user}' to #{@r_host}:#{@r_port} on vhost '#{@r_vhost}'" if opts[:verbose]
end