Class: Superfeedr::Client

Inherits:
Blather::Client
  • Object
show all
Defined in:
lib/superfeedr-rb.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

:nodoc:



29
30
31
32
# File 'lib/superfeedr-rb.rb', line 29

def initialize # :nodoc:
  super
  @deferred = []
end

Class Method Details

.connect(jid, pass, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/superfeedr-rb.rb', line 12

def self.connect(jid, pass, options={} )
  if block_given?
    client = self.setup jid, pass, options[:host], options[:port]
    EM.run {
      yield client

      if options[:subscribe_channel]
        options[:subscribe_channel].subscribe {|url| client.subscribe(url) }
      end

      client.connect
    }
  else
    super
  end
end

Instance Method Details

#client_post_initObject

:nodoc:



47
48
49
50
51
52
# File 'lib/superfeedr-rb.rb', line 47

def client_post_init # :nodoc:
  # overwrite the default actions to take after a client is setup
  status = Blather::Stanza::Presence::Status.new
  status.priority = 100
  write status
end

#defer(*args, &block) ⇒ Object

Allow users to setup callbacks before the connection is setup



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

def defer(*args, &block) # :nodoc:
  if @stream
    false
  else
    @deferred << [args, block]
    true
  end
end

#feed(url, &block) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/superfeedr-rb.rb', line 38

def feed(url, &block)
  return if defer(:feed, url, &block)
  subscribe(url)

  self.register_handler(:pubsub_event, "//ns:items[@node='#{url}']", :ns => Blather::Stanza::PubSub::Event.registered_ns) do |evt, _|
    block.call Status.parse(evt), Entry.parse(evt)
  end
end

#post_init(stream, jid = nil) ⇒ Object

Run all deferred commands after the connection is established



65
66
67
68
69
70
71
# File 'lib/superfeedr-rb.rb', line 65

def post_init(stream, jid = nil) # :nodoc:
  super
  until @deferred.empty?
    args = @deferred.pop
    self.__send__ *(args[0]), &args[1]
  end
end

#subscribe(url) ⇒ Object



34
35
36
# File 'lib/superfeedr-rb.rb', line 34

def subscribe(url)
  self.write Blather::Stanza::PubSub::Subscribe.new(:set, 'firehoser.superfeedr.com', url, self.jid.stripped)
end