slack-rtmapi 1.0.0.rc1 → 1.0.0.rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84fb9330f2c85327c24f9fa0ce1281567b4b9027
4
- data.tar.gz: f8d8197e6d05a98dfd99c635b06e4d10fc7972f0
3
+ metadata.gz: b3b0b5dd151c9091ad6b4475ba67ed442a5bb286
4
+ data.tar.gz: d1cded39a35ba0fe232c48b8822bafb5d046c69c
5
5
  SHA512:
6
- metadata.gz: c98d87e1744b5435edde9379a91fdb7f6de09fd6a4b0c56d50a47f229881824fbb4aabb7ac7757abacc5cf70220b39f1943d3c5a23e080a3f505656f36585eb1
7
- data.tar.gz: cbe53567216bccd0ea79d6ab63bd914cb68598e7e33af2c2e53c4d248634a31900eae15109a7ce8c239497c306ca7abb0245ec65b48aa7fdc78999eaf8638431
6
+ metadata.gz: 3085cc65d5e7760446b8871227ca940479362bf9611c3172662fc9cdce7261958be785deaa1e062fb4c3172c0ab7dfa9e3de25c9199f102c1c2f3703108e136f
7
+ data.tar.gz: f4020460b802359417fe38df5f118e7c68cb823190eab300f44acc830fd206f448d89d0e021829e01aa45dc3cd4c883b0081cdafc6197c7951670d9767822f1a
data/README.md CHANGED
@@ -5,3 +5,21 @@ All you need to use the RTM api of Slack
5
5
 
6
6
  Please note that this gem is GPLv3. You *CAN'T* use it for proprietary software.
7
7
  If you need a licence, please contact me and I will respond in the 24 hours.
8
+
9
+ HOW TO USE
10
+ ----------
11
+
12
+ ```ruby
13
+ require 'slack-rtmapi'
14
+
15
+ url = SlackRTM.get_url token: 'xxx'
16
+ client = SlackRTM::Client.new websocket_url: url
17
+
18
+ client.on :message {|data| p data}
19
+
20
+ client.main_loop
21
+ assert false # never ending loop
22
+ ```
23
+
24
+ For more informations about the Slack Real Time API, please check https://api.slack.com/rtm
25
+
@@ -0,0 +1,144 @@
1
+
2
+ require 'JSON'
3
+ require 'socket'
4
+ require 'websocket/driver'
5
+
6
+ module SlackRTM
7
+
8
+ class Client
9
+ attr_accessor :stop
10
+
11
+ def initialize(conf = {})
12
+ if conf[:websocket_url].nil?
13
+ raise ArgumentError.new 'conf[:websocket_url] should be provided'
14
+ else
15
+ @url = init_url conf[:websocket_url]
16
+ end
17
+
18
+ @socket = conf[:socket]
19
+ @driver = conf[:websocket_driver]
20
+ @msg_queue = conf[:msg_queue] || []
21
+ @has_been_init = false
22
+ @stop = false
23
+ end
24
+
25
+ VALID = [:open, :message, :error]
26
+ def on(type, &block)
27
+ unless VALID.include? type
28
+ raise ArgumentError.new "Client#on(type) takes on type of #{VALID.inspect}"
29
+ end
30
+
31
+ @callbacks ||= {}
32
+ @callbacks[type] = block
33
+ end
34
+
35
+ def send(data)
36
+ data[:id] ||= SecureRandom.random_number 9999999
37
+ @msg_queue << data.to_json
38
+ end
39
+
40
+ # This init has been delayed because the SSL handshake is a blocking and
41
+ # expensive call
42
+ def init
43
+ return if @has_been_init
44
+
45
+ puts "begin init. Init SSL"
46
+
47
+ @socket = init_socket(@socket)
48
+ @socket.connect # costly and blocking !
49
+
50
+ puts "end connect. Init wrapper"
51
+
52
+ internalWrapper = (Struct.new :url, :socket do
53
+ def write(*args)
54
+ puts 'write called'
55
+ self.socket.write(*args)
56
+ end
57
+ end).new @url.to_s, @socket
58
+
59
+ puts 'end wrapper. Init driver'
60
+
61
+ # this, also, is costly and blocking
62
+ @driver = WebSocket::Driver.client internalWrapper
63
+
64
+ puts "end driver. Init callbacks"
65
+
66
+ @driver.on :open do
67
+ puts "connected !"
68
+ @connected = true
69
+ unless @callbacks[:open].nil?
70
+ @callbacks[:open].call
71
+ end
72
+ end
73
+
74
+ @driver.on :error do |event|
75
+ @connected = false
76
+ unless @callbacks[:error].nil?
77
+ @callbacks[:error].call
78
+ end
79
+ raise event
80
+ end
81
+
82
+ @driver.on :message do |event|
83
+ data = JSON.parse event.data
84
+ unless @callbacks[:message].nil?
85
+ @callbacks[:message].call data
86
+ end
87
+ end
88
+
89
+ puts "end callbacks. Starting driver"
90
+
91
+ @driver.start
92
+
93
+ puts 'end init'
94
+
95
+ @has_been_init = true
96
+ end
97
+
98
+ def connected?
99
+ @connected || false
100
+ end
101
+
102
+ # All the polling work is done here
103
+ def inner_loop
104
+ putc '.'
105
+ return if @stop
106
+ data = @socket.readpartial 4096
107
+ return if data.nil? or data.empty?
108
+
109
+ @driver.parse data
110
+ @msg_queue.each {|msg| driver.text msg}
111
+ @msg_queue.clear
112
+ end
113
+
114
+ # A dumb simple main loop.
115
+ def main_loop
116
+ init
117
+ loop do
118
+ inner_loop
119
+ end
120
+ end
121
+
122
+ private
123
+
124
+ def init_url(url)
125
+ url = if url.kind_of? URI then url else URI(url) end
126
+ if url.scheme != 'wss'
127
+ raise ArgumentError.new "config[:websocket_url] should be a valid websocket secure url !"
128
+ end
129
+
130
+ url
131
+ end
132
+
133
+ def init_socket(socket = nil)
134
+ if socket.kind_of? OpenSSL::SSL::SSLSocket
135
+ socket
136
+ elsif socket.kind_of? TCPSocket
137
+ OpenSSL::SSL::SSLSocket.new socket
138
+ else
139
+ OpenSSL::SSL::SSLSocket.new TCPSocket.new @url.host, 443
140
+ end
141
+ end
142
+ end
143
+
144
+ end
@@ -1,3 +1,3 @@
1
1
  module SlackRTM
2
- VERSION = "1.0.0.rc1"
2
+ VERSION = "1.0.0.rc2"
3
3
  end
@@ -0,0 +1,20 @@
1
+ require_relative "slack-rtmapi/client"
2
+ require_relative "slack-rtmapi/version"
3
+
4
+ require 'JSON'
5
+ require 'net/http'
6
+
7
+ module SlackRTM
8
+
9
+ def self.get_url(options)
10
+ if options[:token].nil?
11
+ raise ArgumentError.new "#get_url needs a valid slack token"
12
+ end
13
+
14
+ url = options[:slack_api_url] || 'https://slack.com/api'
15
+ req = Net::HTTP.post_form URI(url + '/rtm.start'), token: options[:token]
16
+ body = JSON.parse req.body
17
+ URI(body['url'])
18
+ end
19
+
20
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-rtmapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc1
4
+ version: 1.0.0.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - mackwic
@@ -53,6 +53,8 @@ files:
53
53
  - .local.vimrc
54
54
  - LICENSE
55
55
  - README.md
56
+ - lib/slack-rtmapi.rb
57
+ - lib/slack-rtmapi/client.rb
56
58
  - lib/slack-rtmapi/version.rb
57
59
  - slack-rtmapi.gemspec
58
60
  homepage: https://github.com/mackwic/slack-rtmapi