Class: Collecta::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

:nodoc:



26
27
28
29
# File 'lib/collecta-rb.rb', line 26

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

Instance Attribute Details

#api_keyObject

:nodoc:



17
18
19
# File 'lib/collecta-rb.rb', line 17

def api_key
  @api_key
end

Class Method Details

.setup(apikey) ⇒ Object

Setup the connection with an API key



20
21
22
23
24
# File 'lib/collecta-rb.rb', line 20

def self.setup(apikey)
  new_client = super "@#{HOST}", nil
  new_client.api_key = apikey
  new_client
end

Instance Method Details

#archive(search, &block) ⇒ Object

Retrieve the last set of entries for search



50
51
52
53
# File 'lib/collecta-rb.rb', line 50

def archive(search, &block)
  return if defer(:archive, search, &block)
  self.write_with_handler Archive.new(self.api_key, search), &block
end

#client_post_initObject

:nodoc:



79
80
81
# File 'lib/collecta-rb.rb', line 79

def client_post_init # :nodoc:
  # overwrite the default actions to take after a client is setup
end

#defer(*args, &block) ⇒ Object

Allow users to setup callbacks before the connection is setup



61
62
63
64
65
66
67
68
# File 'lib/collecta-rb.rb', line 61

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

#notifications(search, &block) ⇒ Object

Subscribe to query notifications



41
42
43
44
45
46
47
# File 'lib/collecta-rb.rb', line 41

def notifications(search, &block)
  return if defer(:notifications, search, &block)
  self.write Subscribe.new self.api_key, nil, search
  self.register_handler(:pubsub_event, "//ns:item[@id='#{search}']", :ns => 'http://jabber.org/protocol/pubsub#event') do |evt, item|
    block.call item.first.find('//ns:count', :ns => 'http://api.collecta.com/ns/search-0#notify').first.content.to_i
  end
end

#post_init(stream, jid = nil) ⇒ Object

Run all deferred commands after the connection is established



71
72
73
74
75
76
77
# File 'lib/collecta-rb.rb', line 71

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

#subscribe(search, &block) ⇒ Object

Subscribe to a query



32
33
34
35
36
37
38
# File 'lib/collecta-rb.rb', line 32

def subscribe(search, &block)
  return if defer(:subscribe, search, &block)
  self.write Subscribe.new self.api_key, search
  self.register_handler(:pubsub_event, "//ns:headers/ns:header[@name='x-collecta#query' and .='#{search}']", :ns => 'http://jabber.org/protocol/shim') do |evt, _|
    block.call Result.new.inherit(evt)
  end
end

#write(stanza) ⇒ Object

Collecta doesn’t seem to like stanzas with whitespace so clear it out



56
57
58
# File 'lib/collecta-rb.rb', line 56

def write(stanza) # :nodoc:
  super stanza.to_xml(:indent => 0).gsub(/\n|\r/,'')
end