Class: Switchboard::Commands::PubSub::Publish

Inherits:
Switchboard::Command show all
Defined in:
lib/switchboard/commands/pubsub/publish.rb

Constant Summary

Constants inherited from Switchboard::Command

Switchboard::Command::DEFAULT_OPTIONS, Switchboard::Command::OPTIONS

Class Method Summary collapse

Methods inherited from Switchboard::Command

description, help, hide!, inherited, to_command, to_command_name, unregister!

Class Method Details

.options(opts) ⇒ Object



7
8
9
10
# File 'lib/switchboard/commands/pubsub/publish.rb', line 7

def self.options(opts)
  super(opts)
  opts.on("--item-id=id", String, "Specifies the item id to use.") { |v| OPTIONS["pubsub.publish.id"] = v }
end

.run!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/switchboard/commands/pubsub/publish.rb', line 12

def self.run!
  switchboard = Switchboard::Core.new do
    defer :item_published do
      begin
        item = Jabber::PubSub::Item.new
        item.text = STDIN.read
        if OPTIONS["pubsub.publish.id"]
          publish_item_with_id_to(OPTIONS["pubsub.node"], item, OPTIONS["pubsub.publish.id"])
        else
          publish_item_to(OPTIONS["pubsub.node"], item)
        end
      rescue Jabber::ServerError => e
        puts e
      end
    end

    def item_published(success)
      if success
        puts "Item was published."
      else
        puts "Item could not be published."
      end
    end
  end

  if OPTIONS["oauth"]
    switchboard.plug!(OAuthPubSubJack)
  else
    switchboard.plug!(PubSubJack)
  end
  switchboard.run!
end