cisco-spark-ruby 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/ciscospark +417 -397
- data/bin/cs +265 -0
- data/lib/base.rb +34 -30
- data/lib/cisco-spark-ruby.rb +120 -105
- data/lib/cli.rb +21 -0
- data/lib/collection.rb +28 -20
- data/lib/membership.rb +28 -28
- data/lib/memberships.rb +14 -15
- data/lib/message.rb +28 -27
- data/lib/messages.rb +42 -16
- data/lib/people.rb +14 -15
- data/lib/person.rb +29 -26
- data/lib/room.rb +27 -28
- data/lib/rooms.rb +21 -19
- data/lib/team.rb +27 -18
- data/lib/teammembership.rb +28 -28
- data/lib/teammemberships.rb +14 -15
- data/lib/teams.rb +14 -15
- data/lib/webhook.rb +24 -24
- data/lib/webhooks.rb +14 -14
- metadata +4 -2
data/bin/cs
ADDED
@@ -0,0 +1,265 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse/subcommand'
|
4
|
+
$LOAD_PATH.push("/Users/robertlabrie/Documents/code/cisco-spark-ruby/lib")
|
5
|
+
require 'cisco-spark-ruby'
|
6
|
+
options = {}
|
7
|
+
parser = OptionParser.new do |opts|
|
8
|
+
opts.on('-h', '--help', 'Show Help') do
|
9
|
+
options[:help] = "Available subcommands:\npeople\nrooms\nmemberships\nmessages\nteams\nteammemberships\nwebhooks"
|
10
|
+
end
|
11
|
+
opts.subcommand 'people' do |subcommand|
|
12
|
+
options[:entity] = 'people'
|
13
|
+
subcommand.on('-h', '--help', 'Show help') { |_o| options[:help] = "Available subcommands:\nlist\ncreate\nget\nupdate\ndelete" }
|
14
|
+
subcommand.subcommand 'list' do |action|
|
15
|
+
options[:action] = 'list'
|
16
|
+
action.on('-e', '--email email', 'List people with this email address. For non-admin requests, either this or displayName are required.') { |o| options[:email] = o }
|
17
|
+
action.on('-d', '--displayName displayName ', 'List people whose name starts with this string. For non-admin requests, either this or email are required.') { |o| options[:displayName] = o }
|
18
|
+
action.on('-i', '--id id', 'List people by ID. Accepts up to 85 person IDs separated by commas.') { |o| options[:id] = o }
|
19
|
+
action.on('-o', '--orgId orgId', 'List people in this organization. Only admin users of another organization (such as partners) may use this parameter.') { |o| options[:orgId] = o }
|
20
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
21
|
+
end
|
22
|
+
subcommand.subcommand 'create' do |action|
|
23
|
+
options[:action] = 'get'
|
24
|
+
action.on('-e', '--emails emails', 'Email addresses of the person') { |o| options[:emails] = o }
|
25
|
+
action.on('-d', '--displayName displayName', 'Full name of the person') { |o| options[:displayName] = o }
|
26
|
+
action.on('-f', '--firstName firstName', 'First name of the person') { |o| options[:firstName] = o }
|
27
|
+
action.on('-l', '--lastName lastName', 'Last name of the person') { |o| options[:lastName] = o }
|
28
|
+
action.on('-a', '--avatar avatar', 'URL to persons avatar in PNG format') { |o| options[:avatar] = o }
|
29
|
+
action.on('-o', '--orgId orgId', 'ID of the organization to which the person belongs') { |o| options[:orgId] = o }
|
30
|
+
action.on('-r', '--roles roles', 'Roles of the person') { |o| options[:roles] = o }
|
31
|
+
action.on('-c', '--licenses licenses', 'Licenses allocated to the person') { |o| options[:licenses] = o }
|
32
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
33
|
+
end
|
34
|
+
subcommand.subcommand 'get' do |action|
|
35
|
+
options[:action] = 'get'
|
36
|
+
action.on('-i', '--id id', 'personId') { |o| options[:id] = o }
|
37
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
38
|
+
end
|
39
|
+
subcommand.subcommand 'update' do |action|
|
40
|
+
options[:action] = 'update'
|
41
|
+
action.on('-i', '--id id', 'personId') { |o| options[:id] = o }
|
42
|
+
action.on('-e', '--emails emails', 'email') { |o| options[:emails] = o }
|
43
|
+
action.on('-d', '--displayName displayName', 'Full name of the person') { |o| options[:displayName] = o }
|
44
|
+
action.on('-f', '--firstName firstName', 'First name of the person') { |o| options[:firstName] = o }
|
45
|
+
action.on('-l', '--lastName lastName', 'Last name of the person') { |o| options[:lastName] = o }
|
46
|
+
action.on('-a', '--avatar avatar', 'URL to persons avatar in PNG format') { |o| options[:avatar] = o }
|
47
|
+
action.on('-o', '--orgId orgId', 'ID of the organization to which the person belongs') { |o| options[:orgId] = o }
|
48
|
+
action.on('-r', '--roles roles', 'Roles of the person') { |o| options[:roles] = o }
|
49
|
+
action.on('-c', '--licenses licenses', 'Licenses allocated to the person') { |o| options[:licenses] = o }
|
50
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
51
|
+
end
|
52
|
+
subcommand.subcommand 'delete' do |action|
|
53
|
+
options[:action] = 'get'
|
54
|
+
action.on('-i', '--id id', 'personIdID') { |o| options[:id] = o }
|
55
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
opts.subcommand 'rooms' do |subcommand|
|
59
|
+
options[:entity] = 'rooms'
|
60
|
+
subcommand.on('-h', '--help', 'Show help') { |_o| options[:help] = "Available subcommands:\nlist\ncreate\nget\nupdate\ndelete" }
|
61
|
+
subcommand.subcommand 'list' do |action|
|
62
|
+
options[:action] = 'list'
|
63
|
+
action.on('-i', '--teamId teamId', 'Limit the rooms to those associated with a team, by ID.') { |o| options[:teamId] = o }
|
64
|
+
action.on('-t', '--type type', 'direct returns all 1-to-1 rooms. group returns all group rooms.') { |o| options[:type] = o }
|
65
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
66
|
+
end
|
67
|
+
subcommand.subcommand 'create' do |action|
|
68
|
+
options[:action] = 'create'
|
69
|
+
action.on('-t', '--title title', 'A user-friendly name for the room.') { |o| options[:title] = o }
|
70
|
+
action.on('-i', '--teamId teamId', 'The ID for the team with which this room is associated.') { |o| options[:teamId] = o }
|
71
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
72
|
+
end
|
73
|
+
subcommand.subcommand 'get' do |action|
|
74
|
+
options[:action] = 'get'
|
75
|
+
action.on('-i', '--id id', 'roomId') { |o| options[:id] = o }
|
76
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
77
|
+
end
|
78
|
+
subcommand.subcommand 'update' do |action|
|
79
|
+
options[:action] = 'update'
|
80
|
+
action.on('-i', '--id id', 'roomId') { |o| options[:id] = o }
|
81
|
+
action.on('-t', '--title title', 'A user-friendly name for the room.') { |o| options[:title] = o }
|
82
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
83
|
+
end
|
84
|
+
subcommand.subcommand 'delete' do |action|
|
85
|
+
options[:action] = 'delete'
|
86
|
+
action.on('-i', '--id id', 'roomId') { |o| options[:id] = o }
|
87
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
opts.subcommand 'memberships' do |subcommand|
|
91
|
+
options[:entity] = 'memberships'
|
92
|
+
subcommand.on('-h', '--help', 'Show help') { |_o| options[:help] = "Available subcommands:\nlist\ncreate\nget\nupdate\ndelete" }
|
93
|
+
subcommand.subcommand 'list' do |action|
|
94
|
+
options[:action] = 'list'
|
95
|
+
action.on('-r', '--roomId roomId ', 'Limit results to a specific room, by ID.') { |o| options[:roomId] = o }
|
96
|
+
action.on('-p', '--personId personId', 'Limit results to a specific person, by ID.') { |o| options[:personId] = o }
|
97
|
+
action.on('-e', '--personEmail personEmail', 'Limit results to a specific person, by email address.') { |o| options[:personEmail] = o }
|
98
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
99
|
+
end
|
100
|
+
subcommand.subcommand 'create' do |action|
|
101
|
+
options[:action] = 'create'
|
102
|
+
action.on('-r', '--roomId roomId', 'The room ID') { |o| options[:roomId] = o }
|
103
|
+
action.on('-p', '--personId personId', 'The person ID') { |o| options[:personId] = o }
|
104
|
+
action.on('-e', '--personEmail personEmail', 'The email address of the person') { |o| options[:personEmail] = o }
|
105
|
+
action.on('-m', '--isModerator isModerator', 'Set true to make the person a moderator') { |o| options[:isModerator] = o }
|
106
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
107
|
+
end
|
108
|
+
subcommand.subcommand 'get' do |action|
|
109
|
+
options[:action] = 'get'
|
110
|
+
action.on('-i', '--id id', 'membershipId') { |o| options[:id] = o }
|
111
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
112
|
+
end
|
113
|
+
subcommand.subcommand 'delete' do |action|
|
114
|
+
options[:action] = 'delete'
|
115
|
+
action.on('-i', '--id id', 'membershipId') { |o| options[:id] = o }
|
116
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
117
|
+
end
|
118
|
+
subcommand.subcommand 'update' do |action|
|
119
|
+
options[:action] = 'update'
|
120
|
+
action.on('-i', '--id id', 'membershipId') { |o| options[:id] = o }
|
121
|
+
action.on('-m', '--isModerator isModerator', 'Set true to make the person a moderator') { |o| options[:isModerator] = o }
|
122
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
123
|
+
end
|
124
|
+
end
|
125
|
+
opts.subcommand 'messages' do |subcommand|
|
126
|
+
options[:entity] = 'messages'
|
127
|
+
subcommand.on('-h', '--help', 'Show help') { |_o| options[:help] = "Available subcommands:\nlist\ncreate\nget\delete" }
|
128
|
+
subcommand.subcommand 'list' do |action|
|
129
|
+
options[:action] = 'list'
|
130
|
+
action.on('-r', '--roomId roomId ', 'List messages for a room, by .') { |o| options[:roomId] = o }
|
131
|
+
action.on('-p', '--mentionedPeople mentionedPeople', 'List messages where the caller is mentioned by specifying "me" or the caller personId') { |o| options[:mentionedPeople] = o }
|
132
|
+
action.on('-b', '--before before ', 'List messages before a sent time in ISO8601 format') { |o| options[:before] = o }
|
133
|
+
action.on('-b', '--beforeMessage beforeMessage ', 'List messages before a message, by ID') { |o| options[:beforeMessage] = o }
|
134
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
135
|
+
end
|
136
|
+
subcommand.subcommand 'create' do |action|
|
137
|
+
options[:action] = 'create'
|
138
|
+
action.on('-r', '--roomId roomId', 'The room ID') { |o| options[:roomId] = o }
|
139
|
+
action.on('-p', '--toPersonId toPersonId', 'The ID of the recipient when sending a private 1:1 message') { |o| options[:toPersonId] = o }
|
140
|
+
action.on('-e', '--toPersonEmail toPersonEmail', 'The ID of the recipient when sending a private 1:1 message') { |o| options[:toPersonEmail] = o }
|
141
|
+
action.on('-t', '--text text', 'The message, in plain text. If markdown is specified this parameter may be optionally used to provide alternate text for UI clients that do not support rich text.') { |o| options[:text] = o }
|
142
|
+
action.on('-m', '--markdown markdown', 'The message, in markdown format') { |o| options[:markdown] = o }
|
143
|
+
action.on('-f', '--files files', 'The public URL to a file to be posted in the room') { |o| options[:files] = o }
|
144
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
145
|
+
end
|
146
|
+
subcommand.subcommand 'get' do |action|
|
147
|
+
options[:action] = 'get'
|
148
|
+
action.on('-i', '--id id', 'ID') { |o| options[:id] = o }
|
149
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
150
|
+
end
|
151
|
+
subcommand.subcommand 'delete' do |action|
|
152
|
+
options[:action] = 'delete'
|
153
|
+
action.on('-i', '--id id', 'ID') { |o| options[:id] = o }
|
154
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
155
|
+
end
|
156
|
+
end
|
157
|
+
opts.subcommand 'teams' do |subcommand|
|
158
|
+
options[:entity] = 'teams'
|
159
|
+
subcommand.on('-h', '--help', 'Show help') { |_o| options[:help] = "Available subcommands:\nlist\ncreate\nget\nupdate\ndelete" }
|
160
|
+
subcommand.subcommand 'list' do |action|
|
161
|
+
options[:action] = 'list'
|
162
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
163
|
+
end
|
164
|
+
subcommand.subcommand 'create' do |action|
|
165
|
+
options[:action] = 'create'
|
166
|
+
action.on('-n', '--name name', 'A user-friendly name for the team.') { |o| options[:name] = o }
|
167
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
168
|
+
end
|
169
|
+
subcommand.subcommand 'get' do |action|
|
170
|
+
options[:action] = 'get'
|
171
|
+
action.on('-i', '--id id', 'teamId') { |o| options[:id] = o }
|
172
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
173
|
+
end
|
174
|
+
subcommand.subcommand 'update' do |action|
|
175
|
+
options[:action] = 'update'
|
176
|
+
action.on('-i', '--id id', 'teamId') { |o| options[:id] = o }
|
177
|
+
action.on('-n', '--name name', 'A user-friendly name for the team.') { |o| options[:name] = o }
|
178
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
179
|
+
end
|
180
|
+
subcommand.subcommand 'delete' do |action|
|
181
|
+
options[:action] = 'delete'
|
182
|
+
action.on('-i', '--id id', 'teamId') { |o| options[:id] = o }
|
183
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
184
|
+
end
|
185
|
+
end
|
186
|
+
opts.subcommand 'teammemberships' do |subcommand|
|
187
|
+
options[:entity] = 'teammemberships'
|
188
|
+
subcommand.on('-h', '--help', 'Show help') { |_o| options[:help] = "Available subcommands:\nlist\ncreate\nget\nupdate\ndelete" }
|
189
|
+
subcommand.subcommand 'list' do |action|
|
190
|
+
options[:action] = 'list'
|
191
|
+
action.on('-t', '--teamId teamId ', 'List team memberships for a team, by ID') { |o| options[:teamId] = o }
|
192
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
193
|
+
end
|
194
|
+
subcommand.subcommand 'create' do |action|
|
195
|
+
options[:action] = 'create'
|
196
|
+
action.on('-t', '--teamId teamId ', 'The team ID') { |o| options[:teamId] = o }
|
197
|
+
action.on('-p', '--personId personId', 'The person ID') { |o| options[:personId] = o }
|
198
|
+
action.on('-e', '--personEmail personEmail', 'The person email') { |o| options[:personEmail] = o }
|
199
|
+
action.on('-m', '--isModerator isModerator', 'Set to true to make the person a moderator') { |o| options[:isModerator] = o }
|
200
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
201
|
+
end
|
202
|
+
subcommand.subcommand 'get' do |action|
|
203
|
+
options[:action] = 'get'
|
204
|
+
action.on('-i', '--id id', 'teamMembershipId') { |o| options[:id] = o }
|
205
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
206
|
+
end
|
207
|
+
subcommand.subcommand 'update' do |action|
|
208
|
+
options[:action] = 'update'
|
209
|
+
action.on('-i', '--id id', 'teamMembershipId') { |o| options[:id] = o }
|
210
|
+
action.on('-m', '--isModerator isModerator', 'Set true to make the person a moderator') { |o| options[:isModerator] = o }
|
211
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
212
|
+
end
|
213
|
+
subcommand.subcommand 'delete' do |action|
|
214
|
+
options[:action] = 'delete'
|
215
|
+
action.on('-i', '--id id', 'teamMembershipId') { |o| options[:id] = o }
|
216
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
217
|
+
end
|
218
|
+
end
|
219
|
+
opts.subcommand 'webhooks' do |subcommand|
|
220
|
+
options[:entity] = 'webhooks'
|
221
|
+
subcommand.on('-h', '--help', 'Show help') { |_o| options[:help] = "Available subcommands:\nlist\ncreate\nget\nupdate\ndelete" }
|
222
|
+
subcommand.subcommand 'list' do |action|
|
223
|
+
options[:action] = 'list'
|
224
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
225
|
+
end
|
226
|
+
subcommand.subcommand 'create' do |action|
|
227
|
+
options[:action] = 'create'
|
228
|
+
action.on('-n', '--name name', 'A user-friendly name for the webhook.') { |o| options[:name] = o }
|
229
|
+
action.on('-u', '--targetUrl targetUrl', 'The URL that receives POST requests for each event.') { |o| options[:targetUrl] = o }
|
230
|
+
action.on('-r', '--resource resource', 'The resource type for the webhook. Creating a webhook requires read scope on the resource the webhook is for.') { |o| options[:resource] = o }
|
231
|
+
action.on('-e', '--event event', 'The event type for the webhook.') { |o| options[:event] = o }
|
232
|
+
action.on('-f', '--filter filter', 'The filter that defines the webhook scope.') { |o| options[:filter] = o }
|
233
|
+
action.on('-s', '--secret secret', 'The secret used to generate payload signature.') { |o| options[:secret] = o }
|
234
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
235
|
+
end
|
236
|
+
subcommand.subcommand 'get' do |action|
|
237
|
+
options[:action] = 'get'
|
238
|
+
action.on('-i', '--id id', 'ID') { |o| options[:id] = o }
|
239
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
240
|
+
end
|
241
|
+
subcommand.subcommand 'update' do |action|
|
242
|
+
options[:action] = 'update'
|
243
|
+
action.on('-n', '--name name', 'A user-friendly name for the webhook.') { |o| options[:name] = o }
|
244
|
+
action.on('-u', '--targetUrl targetUrl', 'The URL that receives POST requests for each event.') { |o| options[:targetUrl] = o }
|
245
|
+
action.on('-s', '--secret secret', 'The secret used to generate payload signature.') { |o| options[:secret] = o }
|
246
|
+
action.on('-t', '--status status', 'The status of the webhook. Use active to reactivate a disabled webhook.') { |o| options[:status] = o }
|
247
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
248
|
+
end
|
249
|
+
subcommand.subcommand 'delete' do |action|
|
250
|
+
options[:action] = 'delete'
|
251
|
+
action.on('-i', '--id id', 'webhookId') { |o| options[:id] = o }
|
252
|
+
action.on('-h', '--help', 'Show help') { |_o| options[:help] = action }
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
256
|
+
parser.parse!
|
257
|
+
|
258
|
+
if options[:help]
|
259
|
+
puts options[:help].to_s
|
260
|
+
exit 0
|
261
|
+
end
|
262
|
+
puts "the options are #{options}"
|
263
|
+
CiscoSpark::configure()
|
264
|
+
cli = CiscoSpark::CLI.new()
|
265
|
+
cli.run(options)
|
data/lib/base.rb
CHANGED
@@ -1,31 +1,35 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
8
|
-
def update(data={})
|
9
|
-
data.each {|k,v| public_send("#{k}=",v)}
|
10
|
-
payload = {}
|
11
|
-
@update_fields.each { |k| payload[k] = self[k] }
|
12
|
-
res = Spark::rest('PUT',"/#{@api_endpoint}/#{@id}", {:payload => payload})
|
13
|
-
if res.ok
|
14
|
-
self.refresh(JSON.parse(res.body))
|
15
|
-
return true
|
16
|
-
end
|
17
|
-
return false
|
18
|
-
end
|
19
|
-
def refresh(data)
|
20
|
-
data.each {|k,v| public_send("#{k}=",v)}
|
21
|
-
end
|
22
|
-
def delete()
|
23
|
-
res = Spark::rest('DELETE',"/#{@api_endpoint}/#{@id}")
|
24
|
-
end
|
25
|
-
def [](key)
|
26
|
-
return nil unless respond_to?(key)
|
27
|
-
public_send(key)
|
28
|
-
end
|
29
|
-
|
1
|
+
module CiscoSpark
|
2
|
+
class Base
|
3
|
+
@api_endpoint = nil
|
4
|
+
@update_fileds = []
|
5
|
+
def initialize(data)
|
6
|
+
refresh(data)
|
30
7
|
end
|
31
|
-
|
8
|
+
|
9
|
+
def update(data = {})
|
10
|
+
data.each { |k, v| public_send("#{k}=", v) }
|
11
|
+
payload = {}
|
12
|
+
@update_fields.each { |k| payload[k] = self[k] }
|
13
|
+
res = CiscoSpark.rest('PUT', "/#{@api_endpoint}/#{@id}", payload: payload)
|
14
|
+
if res.ok
|
15
|
+
refresh(JSON.parse(res.body))
|
16
|
+
return true
|
17
|
+
end
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
def refresh(data)
|
22
|
+
data.each { |k, v| public_send("#{k}=", v) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete
|
26
|
+
res = CiscoSpark.rest('DELETE', "/#{@api_endpoint}/#{@id}")
|
27
|
+
res.ok
|
28
|
+
end
|
29
|
+
|
30
|
+
def [](key)
|
31
|
+
return nil unless respond_to?(key)
|
32
|
+
public_send(key)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/cisco-spark-ruby.rb
CHANGED
@@ -2,118 +2,133 @@ gem 'rest-client', '>2.0.0'
|
|
2
2
|
require 'rest-client'
|
3
3
|
require 'logger'
|
4
4
|
|
5
|
-
module
|
6
|
-
|
7
|
-
|
5
|
+
module CiscoSpark
|
6
|
+
autoload :Base, 'base'
|
7
|
+
autoload :Collection, 'collection'
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
9
|
+
autoload :Room, 'room'
|
10
|
+
autoload :Rooms, 'rooms'
|
11
|
+
autoload :Team, 'team'
|
12
|
+
autoload :Teams, 'teams'
|
13
|
+
autoload :Person, 'person'
|
14
|
+
autoload :People, 'people'
|
15
|
+
autoload :Memberships, 'memberships'
|
16
|
+
autoload :Membership, 'membership'
|
17
|
+
autoload :Messages, 'messages'
|
18
|
+
autoload :Message, 'message'
|
19
|
+
autoload :TeamMemberships, 'teammemberships'
|
20
|
+
autoload :TeamMembership, 'teammembership'
|
21
|
+
autoload :Webhooks, 'webhooks'
|
22
|
+
autoload :Webhook, 'webhook'
|
23
|
+
autoload :CLI, 'cli'
|
24
|
+
@token = nil
|
25
|
+
@logger = nil
|
26
|
+
class << self
|
27
|
+
def configure(opts = {})
|
28
|
+
@token = opts[:token] || ENV['SPARK_TOKEN']
|
29
|
+
@logger = Logger.new(STDOUT)
|
30
|
+
@logger.level = Logger::FATAL
|
31
|
+
# @logger.level = Logger::DEBUG
|
32
|
+
case opts[:loglevel]
|
33
|
+
when :debug
|
34
|
+
@logger.level = Logger::DEBUG
|
35
|
+
when :error
|
36
|
+
@logger.level = Logger::ERROR
|
37
|
+
when :info
|
38
|
+
@logger.level = Logger::INFO
|
39
|
+
when :fatal
|
40
|
+
@logger.level = Logger::FATAL
|
41
|
+
when :warn
|
42
|
+
@logger.level = Logger::WARN
|
43
|
+
when :unknown
|
44
|
+
@logger.level = Logger::UKNOWN
|
44
45
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
end
|
47
|
+
|
48
|
+
def logger
|
49
|
+
@logger
|
50
|
+
end
|
51
|
+
|
52
|
+
def token
|
53
|
+
@token
|
54
|
+
end
|
55
|
+
|
56
|
+
def headers
|
57
|
+
{
|
58
|
+
:content_type => :json,
|
59
|
+
:Authorization => "Bearer #{token}"
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def log(message, level = :debug)
|
64
|
+
case level
|
65
|
+
when :debug
|
66
|
+
logger.debug(message)
|
67
|
+
when :error
|
68
|
+
logger.error(message)
|
69
|
+
when :info
|
70
|
+
logger.info(message)
|
71
|
+
when :fatal
|
72
|
+
logger.fatal(message)
|
73
|
+
when :warn
|
74
|
+
logger.warn(message)
|
75
|
+
when :unknown
|
76
|
+
logger.unknown(message)
|
50
77
|
end
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
when :fatal
|
60
|
-
@@logger.fatal(message)
|
61
|
-
when :warn
|
62
|
-
@@logger.warn(message)
|
63
|
-
when :unknown
|
64
|
-
@@logger.unknown(message)
|
65
|
-
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def rest(method, uri_stub, opts = {})
|
81
|
+
url = "https://api.ciscospark.com/v1#{uri_stub}"
|
82
|
+
|
83
|
+
# bolt on query string params if passed
|
84
|
+
if opts[:params]
|
85
|
+
url = "#{url}?#{URI.encode_www_form(opts[:params])}" unless opts[:params].empty?
|
66
86
|
end
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
if _opts[:params]
|
72
|
-
url = "#{url}?#{URI.encode_www_form(_opts[:params])}" unless _opts[:params].empty?
|
73
|
-
end
|
74
|
-
Spark::Log("REST method=#{method} url=#{url} _opts=#{_opts}", :info)
|
75
|
-
# assmeble the headers
|
76
|
-
headers = Spark::headers
|
77
|
-
headers = headers.merge(_opts[:headers]) if _opts[:headers]
|
87
|
+
CiscoSpark::log("REST method=#{method} url=#{url} opts=#{opts}", :info)
|
88
|
+
# assmeble the headers
|
89
|
+
headers = CiscoSpark.headers
|
90
|
+
headers = headers.merge(opts[:headers]) if opts[:headers]
|
78
91
|
|
79
|
-
|
92
|
+
payload = opts[:payload] || {}
|
80
93
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
97
|
-
Spark::Log("REST method=#{method} url=#{url} response=#{out[:code]}", :info)
|
98
|
-
Spark::Log("REST body=#{out[:body]}", :debug)
|
99
|
-
Spark::Response.new(out)
|
94
|
+
out = {}
|
95
|
+
begin
|
96
|
+
rsp = RestClient::Request.execute(
|
97
|
+
method: method,
|
98
|
+
url: url,
|
99
|
+
payload: payload.to_json,
|
100
|
+
headers: headers,
|
101
|
+
)
|
102
|
+
out[:code] = rsp.code
|
103
|
+
out[:body] = rsp.body
|
104
|
+
out[:object] = rsp
|
105
|
+
rescue RestClient::Exception => e
|
106
|
+
out[:code] = e.http_code
|
107
|
+
out[:body] = e.http_body
|
108
|
+
out[:object] = e
|
100
109
|
end
|
110
|
+
CiscoSpark::log("REST method=#{method} url=#{url} response=#{out[:code]}", :info)
|
111
|
+
CiscoSpark::log("REST body=#{out[:body]}", :debug)
|
112
|
+
CiscoSpark::Response.new(out)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
class Response
|
116
|
+
attr_accessor 'code'
|
117
|
+
attr_accessor 'body'
|
118
|
+
attr_accessor 'object'
|
119
|
+
def initialize(data = {})
|
120
|
+
@code = data[:code]
|
121
|
+
@body = data[:body]
|
122
|
+
@object = data[:object]
|
101
123
|
end
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
end
|
111
|
-
def to_s
|
112
|
-
return body
|
113
|
-
end
|
114
|
-
def ok
|
115
|
-
return true if code < 400
|
116
|
-
false
|
117
|
-
end
|
124
|
+
|
125
|
+
def to_s
|
126
|
+
body
|
127
|
+
end
|
128
|
+
|
129
|
+
def ok
|
130
|
+
return true if code < 400
|
131
|
+
false
|
118
132
|
end
|
133
|
+
end
|
119
134
|
end
|