smackr 0.0.1-universal-java-1.6 → 0.0.2-universal-java-1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,76 @@
1
+ java_import 'org.jivesoftware.smack.XMPPConnection'
2
+ java_import 'org.jivesoftware.smack.ChatManager'
3
+ class Smackr
4
+
5
+ attr_reader :xmpp_connection, :chat_manager
6
+ attr_accessor :server, :username, :password, :resource
7
+
8
+ def initialize(opts={})
9
+ [:server, :username, :password, :resource].each do |field|
10
+ self.send("#{field}=", opts[field]) if opts[field]
11
+ end
12
+ end
13
+
14
+ ## Connect to the given server, using either the username/password/resource
15
+ ## given (if self.username is non-null), or anonymously (if !self.username).
16
+ ## Returns true on success.
17
+ def connect!
18
+ !! connection
19
+ end
20
+
21
+ ## Disconnect from the service and obliterate our xmpp_connection object.
22
+ def disconnect!
23
+ self.xmpp_connection.disconnect if self.xmpp_connection &&
24
+ self.xmpp_connection.is_connected
25
+ @xmpp_connection = nil
26
+ end
27
+
28
+
29
+ ## Return a Smackr::Chat object, given a target user.
30
+ ## Pass in a block expecting |conn, msg| to provide a callback to run
31
+ ## on each message.
32
+ def chat(target, opts={}, &message_callback)
33
+ Smackr::Chat.new(opts.merge(:target => target,
34
+ :chat_manager => chat_manager,
35
+ :message_callback => message_callback))
36
+ end
37
+
38
+ private
39
+
40
+ def connection
41
+ return self.xmpp_connection if self.xmpp_connection &&
42
+ self.xmpp_connection.is_connected
43
+
44
+ unless self.server
45
+ raise ArgumentError, "You must set the :server field before connecting"
46
+ end
47
+
48
+ unless self.xmpp_connection
49
+ @xmpp_connection = XMPPConnection.new(self.server)
50
+ end
51
+
52
+ unless self.xmpp_connection.is_connected
53
+ @xmpp_connection.connect
54
+ if self.username
55
+ @xmpp_connection.login(self.username, self.password) #, self.resource)
56
+ else
57
+ @xmpp_connection.login_anonymously
58
+ end
59
+ end
60
+
61
+ unless self.xmpp_connection.is_connected
62
+ raise Exception, "Connection to #{self.server} failed"
63
+ end
64
+
65
+ unless self.xmpp_connection.is_authenticated
66
+ raise Exception, "Authentication for #{self.server} failed"
67
+ end
68
+
69
+ self.xmpp_connection
70
+ end
71
+
72
+ def chat_manager
73
+ @chat_manager ||= connection.get_chat_manager
74
+ end
75
+
76
+ end
@@ -1,3 +1,3 @@
1
1
  class Smackr
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: smackr
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: universal-java-1.6
7
7
  authors:
8
8
  - pete gamache