Class: Zoe
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#rooms ⇒ Object
readonly
Returns the value of attribute rooms.
-
#roster ⇒ Object
readonly
Returns the value of attribute roster.
Class Method Summary collapse
Instance Method Summary collapse
- #connect(jid: nil, password: nil, rooms: []) ⇒ Object
- #debug! ⇒ Object
- #get_jid_by_name(name) ⇒ Object
-
#initialize ⇒ Zoe
constructor
A new instance of Zoe.
- #jid ⇒ Object
- #on_message(&block) ⇒ Object
- #send(to, message) ⇒ Object
Constructor Details
#initialize ⇒ Zoe
Returns a new instance of Zoe.
14 15 16 |
# File 'lib/zoe.rb', line 14 def initialize @callbacks = [] end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
12 13 14 |
# File 'lib/zoe.rb', line 12 def callbacks @callbacks end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
12 13 14 |
# File 'lib/zoe.rb', line 12 def client @client end |
#rooms ⇒ Object (readonly)
Returns the value of attribute rooms.
12 13 14 |
# File 'lib/zoe.rb', line 12 def rooms @rooms end |
#roster ⇒ Object (readonly)
Returns the value of attribute roster.
12 13 14 |
# File 'lib/zoe.rb', line 12 def roster @roster end |
Class Method Details
.method_missing(method, *args, &block) ⇒ Object
74 75 76 |
# File 'lib/zoe.rb', line 74 def self.method_missing(method, *args, &block) instance.__send__(method, *args, &block) end |
Instance Method Details
#connect(jid: nil, password: nil, rooms: []) ⇒ Object
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 44 45 46 47 |
# File 'lib/zoe.rb', line 18 def connect(jid: nil, password: nil, rooms: []) @jid = jid @client = Jabber::Client.new(jid + '/bot') @client.connect @client.auth(password) @client.send(Jabber::Presence.new.set_type(:available)) @roster = Jabber::Roster::Helper.new(@client) @roster.wait_for_roster @rooms = rooms.map do |room_name| room = Jabber::MUC::MUCClient.new(@client) room.join(room_name + '/' + self.jid.iname) room end @client.(200) do |m| if m.type == :chat # if PM m.reply_to = m.from elsif m.type == :groupchat && m.from.resource # if GM m.reply_to = m.from m.from = get_jid_by_name(m.from.resource) end if m.body @callbacks.each{ |c| c.call(m) } end end end |
#debug! ⇒ Object
69 70 71 72 |
# File 'lib/zoe.rb', line 69 def debug! Jabber.logger = Logger.new(STDOUT) Jabber.debug = true end |
#get_jid_by_name(name) ⇒ Object
53 54 55 |
# File 'lib/zoe.rb', line 53 def get_jid_by_name(name) @roster.items.values.find{ |v| v.iname == name } end |
#jid ⇒ Object
49 50 51 |
# File 'lib/zoe.rb', line 49 def jid @roster[@jid] end |
#on_message(&block) ⇒ Object
65 66 67 |
# File 'lib/zoe.rb', line 65 def (&block) @callbacks << block end |
#send(to, message) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/zoe.rb', line 57 def send(to, ) to = to.is_a?(Jabber::JID) ? to : Jabber::JID.new(to.to_s) = Jabber::Message.new(to, ) .type = (to.domain == "conf.hipchat.com" ? :groupchat : :chat) @client.send() end |