class Blather::DSL::PubSub

A helper class for providing a simplified PubSub interface to the DSL

Attributes

host[RW]

Public Class Methods

new(client, host) click to toggle source

Create a new pubsub DSL

@param [Blather::Client] client the client who's connection will be used @param [#to_s] host the PubSub host

# File lib/blather/client/dsl/pubsub.rb, line 13
def initialize(client, host)
  @client = client
  @host = host
end

Public Instance Methods

affiliations(host = nil, &callback) click to toggle source

Retrieve Affiliations

@param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Hash] affiliations See {Blather::Stanza::PubSub::Affiliations#list}

# File lib/blather/client/dsl/pubsub.rb, line 22
def affiliations(host = nil, &callback)
  request Stanza::PubSub::Affiliations.new(:get, send_to(host)), :list, callback
end
create(node, host = nil, configuration = nil) { |n| ... } click to toggle source

Create a node

@param [#to_s] node the node to create @param [#to_s] host the PubSub host (defaults to the initialized host) @param [optional, Blather::Stanza::X] configuration the additional configuration to be set to created node @yield [Blather::Stanza] stanza the reply stanza

# File lib/blather/client/dsl/pubsub.rb, line 126
def create(node, host = nil, configuration = nil)
  stanza = Stanza::PubSub::Create.new(:set, send_to(host), node)
  stanza.configure_node << configuration if configuration
  request(stanza) { |n| yield n if block_given? }
end
delete(node, host = nil) { |n| ... } click to toggle source

Delete a node

@param [#to_s] node the node to delete @param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Blather::Stanza] stanza the reply stanza

# File lib/blather/client/dsl/pubsub.rb, line 147
def delete(node, host = nil)
  stanza = Stanza::PubSubOwner::Delete.new(:set, send_to(host), node)
  request(stanza) { |n| yield n if block_given? }
end
items(path, list = [], max = nil, host = nil, &callback) click to toggle source

Retrieve items for a node

@param [#to_s] path the node path @param [Array<#to_s>] list a list of IDs to retrieve @param [Fixnum, to_s] max the maximum number of items to return @param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Array<Blather::Stanza::PubSub::PubSubItem>] items see {Blather::Stanza::PubSub::Items#items}

# File lib/blather/client/dsl/pubsub.rb, line 64
def items(path, list = [], max = nil, host = nil, &callback)
  request(
    Stanza::PubSub::Items.request(send_to(host), path, list, max),
    :items,
    callback
  )
end
node(path, host = nil, &callback) click to toggle source

Discover node information

@param [#to_s] path the node path @param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Blather::Stanza::DiscoInfo>] info

# File lib/blather/client/dsl/pubsub.rb, line 51
def node(path, host = nil, &callback)
  stanza = Stanza::DiscoInfo.new(:get, path)
  stanza.to = send_to(host)
  request stanza, nil, callback
end
nodes(path = nil, host = nil, &callback) click to toggle source

Discover Nodes

@param [#to_s] path the node path @param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Array<Blather::Stanza::DiscoItems::Item>] items

# File lib/blather/client/dsl/pubsub.rb, line 39
def nodes(path = nil, host = nil, &callback)
  path ||= '/'
  stanza = Stanza::DiscoItems.new(:get, path)
  stanza.to = send_to(host)
  request stanza, :items, callback
end
publish(node, payload, host = nil) { |n| ... } click to toggle source

Publish an item to a node

@param [#to_s] node the node to publish to @param [#to_s] payload the payload to send see {Blather::Stanza::PubSub::Publish} @param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Blather::Stanza] stanza the reply stanza

# File lib/blather/client/dsl/pubsub.rb, line 104
def publish(node, payload, host = nil)
  stanza = Stanza::PubSub::Publish.new(send_to(host), node, :set, payload)
  request(stanza) { |n| yield n if block_given? }
end
purge(node, host = nil) { |n| ... } click to toggle source

Purge all node items

@param [#to_s] node the node to purge @param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Blather::Stanza] stanza the reply stanza

# File lib/blather/client/dsl/pubsub.rb, line 137
def purge(node, host = nil)
  stanza = Stanza::PubSubOwner::Purge.new(:set, send_to(host), node)
  request(stanza) { |n| yield n if block_given? }
end
retract(node, ids = [], host = nil) { |n| ... } click to toggle source

Delete items from a node

@param [#to_s] node the node to delete from @param [Array<#to_s>] ids a list of ids to delete @param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Blather::Stanza] stanza the reply stanza

# File lib/blather/client/dsl/pubsub.rb, line 115
def retract(node, ids = [], host = nil)
  stanza = Stanza::PubSub::Retract.new(send_to(host), node, :set, ids)
  request(stanza) { |n| yield n if block_given? }
end
subscribe(node, jid = nil, host = nil) { |n| ... } click to toggle source

Subscribe to a node

@param [#to_s] node the node to subscribe to @param [Blather::JID, to_s] jid is the jid that should be used. Defaults to the stripped current JID @param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Blather::Stanza] stanza the reply stanza

# File lib/blather/client/dsl/pubsub.rb, line 79
def subscribe(node, jid = nil, host = nil)
  jid ||= client.jid.stripped
  stanza = Stanza::PubSub::Subscribe.new(:set, send_to(host), node, jid)
  request(stanza) { |n| yield n if block_given? }
end
subscriptions(host = nil, &callback) click to toggle source

Retrieve Subscriptions

@param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Hash] affiliations See {Blather::Stanza::PubSub::Subscriptions#list}

# File lib/blather/client/dsl/pubsub.rb, line 30
def subscriptions(host = nil, &callback)
  request Stanza::PubSub::Subscriptions.new(:get, send_to(host)), :list, callback
end
unsubscribe(node, jid = nil, subid = nil, host = nil) { |n| ... } click to toggle source

Unsubscribe from a node

@param [#to_s] node the node to unsubscribe from @param [Blather::JID, to_s] jid is the jid that should be used. Defaults to the stripped current JID @param [#to_s] host the PubSub host (defaults to the initialized host) @yield [Blather::Stanza] stanza the reply stanza

# File lib/blather/client/dsl/pubsub.rb, line 92
def unsubscribe(node, jid = nil, subid = nil, host = nil)
  jid ||= client.jid.stripped
  stanza = Stanza::PubSub::Unsubscribe.new(:set, send_to(host), node, jid, subid)
  request(stanza) { |n| yield n if block_given? }
end

Private Instance Methods

client() click to toggle source
# File lib/blather/client/dsl/pubsub.rb, line 168
def client
  @client
end
request(node, method = nil, callback = nil, &block) click to toggle source
# File lib/blather/client/dsl/pubsub.rb, line 153
def request(node, method = nil, callback = nil, &block)
  unless block_given?
    block = lambda do |node|
      callback.call(method ? node.__send__(method) : node)
    end
  end

  client.write_with_handler(node, &block)
end
send_to(host = nil) click to toggle source
# File lib/blather/client/dsl/pubsub.rb, line 163
def send_to(host = nil)
  raise 'You must provide a host' unless (host ||= @host)
  host
end