lita-glip 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b5ad1ab8e2ee2211e91c806e42c3ba28425ce05
4
+ data.tar.gz: 8fbadcaac6d7cd9b64735f3d654099d21d313569
5
+ SHA512:
6
+ metadata.gz: f822c89fb67841fd3df3d6d01b15d3b58080d667869ea1f5a0fd90819eb9a695e42fbe83f8e3c571daaa106f3e02fb0cccf570f01e490ea7b9699682b3cecbed
7
+ data.tar.gz: fa3be31719f98fba57554df83ae9cc34ef938c988b345c7ecabee14191bfc12ac62b594480e4e21ecc67e89520c1ba554ede4f39e538dab91b1158f7fc00b422
@@ -0,0 +1,58 @@
1
+ require 'glip_sdk'
2
+ require 'ringcentral_sdk'
3
+ require 'multi_json'
4
+
5
+ require 'lita/adapters/glip/message_handler'
6
+ require 'lita/adapters/glip/room_creator'
7
+ require 'lita/adapters/glip/user_creator'
8
+
9
+ module Lita
10
+ module Adapters
11
+ class Glip < Adapter
12
+ class Connector
13
+ attr_reader :robot
14
+
15
+ def initialize(robot, app_key, app_secret, server, username, extension, password, token)
16
+ @robot = robot
17
+ @logger_prefix = " -- #{self.class.name}: "
18
+ @rc_sdk = RingCentralSdk::REST::Client.new do |config|
19
+ config.app_key = app_key
20
+ config.app_secret = app_secret
21
+ config.server_url = server
22
+ end
23
+ @glip_sdk = GlipSdk::REST::Client.new @rc_sdk
24
+ @username = username
25
+ @extension = extension
26
+ @password = password
27
+ @token = token
28
+ end
29
+
30
+ def connect
31
+ client_connect
32
+ end
33
+
34
+ def client_connect
35
+ Lita.logger.info("#{@logger_prefix}Authorizing with RingCentral.")
36
+ if @token.nil?
37
+ @rc_sdk.authorize_password @username, @extension, @password
38
+ token = MultiJson.encode @rc_sdk.token.to_hash
39
+ Lita.logger.debug("#{@logger_prefix}Authorized with token: #{token}.")
40
+ else
41
+ @rc_sdk.set_token @token
42
+ end
43
+
44
+ @glip_sdk = GlipSdk::REST::Client.new @rc_sdk
45
+ observer = Lita::Adapters::Glip::MessageHandler.new @robot, @glip_sdk
46
+ @glip_sdk.posts.observe observer
47
+ end
48
+
49
+ def message(group_id, strings)
50
+ strings.each do |s|
51
+ @glip_sdk.posts.post groupId: group_id, text: s
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,37 @@
1
+ module Lita
2
+ module Adapters
3
+ class Glip < Adapter
4
+ class MessageHandler
5
+ attr_reader :robot
6
+
7
+ def initialize(robot, glip_sdk)
8
+ @robot = robot
9
+ @glip_sdk = glip_sdk
10
+ @logger_prefix = " -- #{self.class.name}: "
11
+ end
12
+
13
+ def update(message)
14
+ m = message
15
+ unless m.is_a?(Hash) && m.key?('event') && m['event'].index('/glip/posts').is_a?(Fixnum)
16
+ return
17
+ end
18
+ unless m.key?('body') && m['body'].key?('messageType') && m['body']['messageType'] == "PostAdded"
19
+ return
20
+ end
21
+
22
+ user_id = message['body']['post']['creatorId']
23
+ user = UserCreator.find_or_create_user user_id, @glip_sdk
24
+
25
+ room_id = message['body']['post']['groupId']
26
+ room = RoomCreator.find_or_create_room room_id, @glip_sdk
27
+
28
+ source = Lita::Source.new user: user, room: room
29
+ post = message['body']['post']['text'].to_s
30
+ msg = Lita::Message.new @robot, post, source
31
+
32
+ @robot.receive msg
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,22 @@
1
+ module Lita
2
+ module Adapters
3
+ class Glip < Adapter
4
+ class RoomCreator
5
+ class << self
6
+ def find_or_create_room(room_id, glip_sdk)
7
+ Lita::Room.find_by_id(room_id) || create_room(room_id, glip_sdk)
8
+ end
9
+
10
+ def create_room(room_id, glip_sdk)
11
+ res = glip_sdk.groups.get groupId: room_id
12
+ if res.status == 200
13
+ glip_room = res.body
14
+ return Lita::Room.create_or_update room_id, name: glip_room['name']
15
+ end
16
+ Lita::Room.create_or_update room_id
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ module Lita
2
+ module Adapters
3
+ class Glip < Adapter
4
+ class UserCreator
5
+ class << self
6
+ def find_or_create_user(user_id, glip_sdk)
7
+ Lita::User.find_by_id(user_id) || create_user(user_id, glip_sdk)
8
+ end
9
+
10
+ def create_user(user_id, glip_sdk)
11
+ res = glip_sdk.persons.get personId: user_id
12
+ if res.status == 200
13
+ glip_user_name = real_name res.body
14
+ return Lita::User.create(
15
+ glip_user['id'],
16
+ name: glip_user_name,
17
+ mention_name: glip_user_name
18
+ )
19
+ end
20
+ User.create user_id
21
+ end
22
+
23
+ def real_name(glip_user)
24
+ "#{glip_user['firstName']} #{glip_user['lastName']}".gsub(/\s+/, ' ').strip
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,55 @@
1
+ require 'lita'
2
+ require 'lita/adapters/glip/connector'
3
+ require 'lita/adapters/glip/message_handler'
4
+ require 'lita/adapters/glip/room_creator'
5
+ require 'lita/adapters/glip/user_creator'
6
+
7
+ module Lita
8
+ module Adapters
9
+ class Glip < Adapter
10
+ namespace 'glip'
11
+
12
+ # Required attributes
13
+ config :app_key, type: String, required: true
14
+ config :app_secret, type: String, required: true
15
+ config :server_url, type: String, default: 'platform.devtest.ringcentral.com'
16
+
17
+ config :username, type: String, required: true
18
+ config :extension, type: String
19
+ config :password, type: String, required: true
20
+ config :token, type: String
21
+
22
+ def initialize(robot)
23
+ super
24
+ @connector = Connector.new(
25
+ robot,
26
+ config.app_key,
27
+ config.app_secret,
28
+ config.server_url,
29
+ config.username,
30
+ config.extension,
31
+ config.password,
32
+ config.token
33
+ )
34
+ end
35
+
36
+ def run
37
+ @connector.connect
38
+ sleep
39
+ rescue Interrupt
40
+ shut_down
41
+ end
42
+
43
+ def send_messages(target, strings)
44
+ Lita.logger.info 'Sending Messages via Glip'
45
+ @connector.message target.room_object.id, strings
46
+ end
47
+
48
+ def shut_down
49
+ robot.trigger :disconnected
50
+ end
51
+ end
52
+
53
+ Lita.register_adapter :glip, Glip
54
+ end
55
+ end
data/lib/lita-glip.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'lita'
2
+
3
+ Lita.load_locales Dir[File.expand_path(
4
+ File.join('..', '..', 'locales', '*.yml'), __FILE__
5
+ )]
6
+
7
+ require 'lita/adapters/glip'
metadata ADDED
@@ -0,0 +1,233 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-glip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Wang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.4.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.4.3
33
+ - !ruby/object:Gem::Dependency
34
+ name: glip_sdk
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.0.3
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.0.3
53
+ - !ruby/object:Gem::Dependency
54
+ name: multi_json
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.12.1
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.12.1
73
+ - !ruby/object:Gem::Dependency
74
+ name: bundler
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '1.3'
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '1.3'
87
+ - !ruby/object:Gem::Dependency
88
+ name: pry-byebug
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '3'
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.4.2
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 3.4.2
107
+ - !ruby/object:Gem::Dependency
108
+ name: rack-test
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 0.6.3
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 0.6.3
127
+ - !ruby/object:Gem::Dependency
128
+ name: rake
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: '12'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - "~>"
139
+ - !ruby/object:Gem::Version
140
+ version: '12'
141
+ - !ruby/object:Gem::Dependency
142
+ name: rspec
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '3'
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 3.0.0
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '3'
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: 3.0.0
161
+ - !ruby/object:Gem::Dependency
162
+ name: simplecov
163
+ requirement: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: 0.9.2
171
+ type: :development
172
+ prerelease: false
173
+ version_requirements: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - "~>"
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: 0.9.2
181
+ - !ruby/object:Gem::Dependency
182
+ name: coveralls
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description: A Glip adapter for Lita.
196
+ email:
197
+ - johncwang@gmail.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - lib/lita-glip.rb
203
+ - lib/lita/adapters/glip.rb
204
+ - lib/lita/adapters/glip/connector.rb
205
+ - lib/lita/adapters/glip/message_handler.rb
206
+ - lib/lita/adapters/glip/room_creator.rb
207
+ - lib/lita/adapters/glip/user_creator.rb
208
+ homepage: https://github.com/ringcentral-ruby/lita-glip
209
+ licenses:
210
+ - MIT
211
+ metadata:
212
+ lita_plugin_type: adapter
213
+ post_install_message:
214
+ rdoc_options: []
215
+ require_paths:
216
+ - lib
217
+ required_ruby_version: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - ">="
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ required_rubygems_version: !ruby/object:Gem::Requirement
223
+ requirements:
224
+ - - ">="
225
+ - !ruby/object:Gem::Version
226
+ version: '0'
227
+ requirements: []
228
+ rubyforge_project:
229
+ rubygems_version: 2.5.2
230
+ signing_key:
231
+ specification_version: 4
232
+ summary: A Glip adapter for the Lita chat robot.
233
+ test_files: []