Sha256: f50e5bbe91ed284d0a69b65c724b0b7d46f926cfcef506854593f0edd63d3ae7

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

class ForeignOffice::Busses::PubnubBus < ForeignOffice::Busses::GenericBus

  def self.config(config)
    self.publish_key = config[:publish_key]
    self.subscribe_key = config[:subscribe_key]
    self.secret_key = config[:secret_key]
  end

  def self.publish_key=(publish_key)
    @publish_key = publish_key
  end

  def self.publish_key
    @publish_key
  end

  def self.subscribe_key=(subscribe_key)
    @subscribe_key = subscribe_key
  end

  def self.subscribe_key
    @subscribe_key
  end

  def self.secret_key=(secret_key)
    @secret_key = secret_key
  end

  def self.secret_key
    @secret_key
  end

  def self.connection
    @pubnub ||= ::Pubnub.new(
      publish_key:    self.publish_key, # publish_key only required if publishing.
      subscribe_key:  self.subscribe_key, # required
      secret_key:     self.secret_key,
      error_callback: lambda { |msg| Rails.logger.error( msg.inspect )}
    )
  end

  def self.publish(message)
    message.symbolize_keys!
    self.connection.publish(
      channel:  message[:channel],
      message:  message,
      http_sync: true,
      callback: lambda{|response|
        if 0 == JSON.parse(response.instance_variable_get(:@response)).first #pubnub publishing failed
          if attempts <= 3
            self.publish!(message, attempts) #retry if we failed
          else
            Rails.logger.debug("ForeignOffice#publish! failed after #{attempts} attempts. message: #{message.inspect}")
          end
        end
      }
    )
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
foreign_office-0.1.1 lib/foreign_office/busses/pubnub_bus.rb
foreign_office-0.1.0 lib/foreign_office/busses/pubnub_bus.rb
foreign_office-0.0.7 lib/foreign_office/busses/pubnub_bus.rb