class OmfCommon::Comm::XMPP::Topic

Public Class Methods

new(id, opts = {}, &block) click to toggle source
Calls superclass method OmfCommon::Comm::Topic::new
# File lib/omf_common/comm/xmpp/topic.rb, line 40
def initialize(id, opts = {}, &block)
  id, @pubsub_domain = id.to_s.split("@")
  if id =~ /^xmpp:\/\/(.+)$/
    id = $1
  end
  @pubsub_domain ||= OmfCommon.comm.jid.domain

  super

  @on_subscrided_handlers = []

  topic_block = proc do |stanza|
    if stanza.error?
      block.call(stanza) if block
    else
      block.call(self) if block

      @lock.synchronize do
        @on_subscrided_handlers.each do |handler|
          handler.call
        end
      end
    end
  end

  # Create xmpp pubsub topic, then subscribe to it
  #
  OmfCommon.comm._create(id.to_s, pubsub_domain_addr) do |stanza|
    if stanza.error?
      e_stanza = Blather::StanzaError.import(stanza)
      if e_stanza.name == :conflict
        # Topic exists, just subscribe to it.
        OmfCommon.comm._subscribe(id.to_s, pubsub_domain_addr, &topic_block)
      else
        block.call(stanza) if block
      end
    else
      OmfCommon.comm._subscribe(id.to_s, pubsub_domain_addr, &topic_block)
    end
  end

  event_block = proc do |event|
    OmfCommon::Message.parse(event.items.first.payload) do |parsed_msg|
      on_incoming_message(parsed_msg)
    end
  end

  OmfCommon.comm.topic_event(default_guard, &event_block)
end

Public Instance Methods

address() click to toggle source

def delete_on_message_cbk_by_id(id)

@lock.synchronize do
  @on_message_cbks[id] && @on_message_cbks.reject! { |k| k == id.to_s }
end

end

# File lib/omf_common/comm/xmpp/topic.rb, line 16
def address
  #"xmpp://#{id.to_s}@#{OmfCommon.comm.jid.domain}"
  "xmpp://#{id.to_s}@#{@pubsub_domain}"
end
on_subscribed(&block) click to toggle source
# File lib/omf_common/comm/xmpp/topic.rb, line 21
def on_subscribed(&block)
  return unless block

  @lock.synchronize do
    @on_subscrided_handlers << block
  end
end
unsubscribe(key) click to toggle source
Calls superclass method OmfCommon::Comm::Topic#unsubscribe
# File lib/omf_common/comm/xmpp/topic.rb, line 29
def unsubscribe(key)
  super
  OmfCommon.comm._unsubscribe_one(self.id)
end

Private Instance Methods

_send_message(msg, opts = {}, block) click to toggle source
Calls superclass method OmfCommon::Comm::Topic#_send_message
# File lib/omf_common/comm/xmpp/topic.rb, line 90
def _send_message(msg, opts = {}, block)
  super
  OmfCommon.comm.publish(self.id, msg, pubsub_domain_addr)
end
default_guard() click to toggle source
# File lib/omf_common/comm/xmpp/topic.rb, line 99
def default_guard
  proc do |event|
    event.node == self.id.to_s
  end
end
pubsub_domain_addr() click to toggle source
# File lib/omf_common/comm/xmpp/topic.rb, line 36
def pubsub_domain_addr
  "pubsub.#{@pubsub_domain}"
end
valid_guard?(guard_proc) click to toggle source
# File lib/omf_common/comm/xmpp/topic.rb, line 95
def valid_guard?(guard_proc)
  guard_proc && guard_proc.class == Proc
end