Class: Jabber::Robot
- Inherits:
-
Object
- Object
- Jabber::Robot
- Defined in:
- lib/xmpp4r/robot.rb
Instance Attribute Summary collapse
-
#auto_accept_subscription ⇒ Object
Returns the value of attribute auto_accept_subscription.
-
#client ⇒ Object
Returns the value of attribute client.
-
#errback ⇒ Object
Returns the value of attribute errback.
-
#password ⇒ Object
Returns the value of attribute password.
-
#retry_time ⇒ Object
Returns the value of attribute retry_time.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#available ⇒ Object
actions #####.
- #connect ⇒ Object
- #helper ⇒ Object
-
#initialize(username, password, opts = {}, &errback) ⇒ Robot
constructor
A new instance of Robot.
- #inspect ⇒ Object
- #login ⇒ Object
-
#message(to_jid, body) ⇒ Object
e.g.
-
#notify_message ⇒ Object
e.g.
-
#notify_presence ⇒ Object
e.g.
-
#roster(sync = false) ⇒ Object
getters #####.
-
#start ⇒ Object
Start the robot.
- #stop ⇒ Object
-
#subscribe(to_jid) ⇒ Object
e.g.
Constructor Details
#initialize(username, password, opts = {}, &errback) ⇒ Robot
Returns a new instance of Robot.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/xmpp4r/robot.rb', line 12 def initialize username, password, opts={}, &errback @username = username @password = password @errback = errback @roster = {:available => Set.new, :away => Set.new, :unavailable => Set.new} @retry_time = 1 @auto_accept_subscription = opts[:auto_accept_subscription] @roster_mutex = Mutex.new @helper_mutex = Mutex.new @client_mutex = Mutex.new @client = nil # eliminate warning @sleeping = false end |
Instance Attribute Details
#auto_accept_subscription ⇒ Object
Returns the value of attribute auto_accept_subscription.
9 10 11 |
# File 'lib/xmpp4r/robot.rb', line 9 def auto_accept_subscription @auto_accept_subscription end |
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/xmpp4r/robot.rb', line 9 def client @client end |
#errback ⇒ Object
Returns the value of attribute errback.
9 10 11 |
# File 'lib/xmpp4r/robot.rb', line 9 def errback @errback end |
#password ⇒ Object
Returns the value of attribute password.
9 10 11 |
# File 'lib/xmpp4r/robot.rb', line 9 def password @password end |
#retry_time ⇒ Object
Returns the value of attribute retry_time.
9 10 11 |
# File 'lib/xmpp4r/robot.rb', line 9 def retry_time @retry_time end |
#username ⇒ Object
Returns the value of attribute username.
9 10 11 |
# File 'lib/xmpp4r/robot.rb', line 9 def username @username end |
Instance Method Details
#available ⇒ Object
actions #####
86 87 88 |
# File 'lib/xmpp4r/robot.rb', line 86 def available client.send(Jabber::Presence.new.set_type(:available)) end |
#connect ⇒ Object
67 68 69 70 |
# File 'lib/xmpp4r/robot.rb', line 67 def connect client.connect self end |
#helper ⇒ Object
38 39 40 |
# File 'lib/xmpp4r/robot.rb', line 38 def helper @helper ||= Jabber::Roster::Helper.new(client, false) end |
#inspect ⇒ Object
30 31 32 |
# File 'lib/xmpp4r/robot.rb', line 30 def inspect "#<#{self.class.name} username=#{username.inspect}>" end |
#login ⇒ Object
72 73 74 75 |
# File 'lib/xmpp4r/robot.rb', line 72 def login client.auth(password) self end |
#message(to_jid, body) ⇒ Object
e.g. robot.message(‘[email protected]’, ‘Hi!’)
91 92 93 |
# File 'lib/xmpp4r/robot.rb', line 91 def to_jid, body client.send(Jabber::Message::new(to_jid, body).set_type(:chat)) end |
#notify_message ⇒ Object
e.g. robot.notify_message{ |from, body| puts “#from: #body” }
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/xmpp4r/robot.rb', line 124 def client. do || protect_yield do if .body yield(jid_to_username(.from), .body.strip) end false end end end |
#notify_presence ⇒ Object
e.g. robot.notify_presence{ |from, status| puts “#from is #status” } The status could be one of :available, :away, :unavailable
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/xmpp4r/robot.rb', line 104 def notify_presence client.add_presence_callback do |presence| status = presence.type || case presence.show # http://xmpp.org/rfcs/rfc3921.html when nil, :chat :available when :away, :dnd, :xa :away else raise "What's this show? #{presence.show}" end protect_yield do yield(jid_to_username(presence.from), status) false end end end |
#roster(sync = false) ⇒ Object
getters #####
79 80 81 82 |
# File 'lib/xmpp4r/robot.rb', line 79 def roster sync=false sync_roster if sync || @roster[:unknown].nil? @roster_mutex.synchronize{ @roster } end |
#start ⇒ Object
Start the robot. This is not thread safe.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/xmpp4r/robot.rb', line 45 def start @client_mutex.synchronize do @sleeping = false if @client # restart stop @client = nil end initialize_callbacks connect login available roster @retry_time = 1 # reset retry time after successfully connected self end end |
#stop ⇒ Object
62 63 64 65 |
# File 'lib/xmpp4r/robot.rb', line 62 def stop client.close self end |
#subscribe(to_jid) ⇒ Object
e.g. robot.subscribe(‘[email protected]’)
96 97 98 |
# File 'lib/xmpp4r/robot.rb', line 96 def subscribe to_jid client.send(Jabber::Presence.new.set_type(:subscribe).set_to(to_jid)) end |