cwr 0.0.0 → 0.0.1
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.
- data/lib/cwr.rb +337 -2
- metadata +6 -6
data/lib/cwr.rb
CHANGED
@@ -1,5 +1,340 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'httparty'
|
3
|
+
|
4
|
+
class Producer
|
5
|
+
attr_reader :path
|
6
|
+
|
7
|
+
def initialize(cwr,
|
8
|
+
name,
|
9
|
+
path=nil,
|
10
|
+
owner_id=nil)
|
11
|
+
@cwr = cwr
|
12
|
+
@name = name
|
13
|
+
@path = path
|
14
|
+
@owner_id = owner_id
|
15
|
+
end
|
16
|
+
|
17
|
+
def destroy
|
18
|
+
@cwr.destroy_producer(self)
|
19
|
+
return DestroyedProducer.new(path)
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_consumer(name)
|
23
|
+
@cwr.create_consumer self, name
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_webhook(consumer, webhook_post_uri, post_data=nil, post_headers=nil)
|
27
|
+
@cwr.create_webhook(consumer, webhook_post_uri, post_data, post_headers)
|
28
|
+
end
|
29
|
+
|
30
|
+
def create_mass_webhooks
|
31
|
+
raise "Not implemented"
|
32
|
+
end
|
33
|
+
|
34
|
+
def id
|
35
|
+
@path ? @path.split("/")[-1] : nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def list_consumers
|
39
|
+
@cwr.list_consumers(self)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Consumer
|
44
|
+
attr_reader :path
|
45
|
+
|
46
|
+
def initialize(cwr, producer, name, consumer_path=nil)
|
47
|
+
@cwr = cwr
|
48
|
+
@producer = producer
|
49
|
+
@name = name
|
50
|
+
@path = consumer_path
|
51
|
+
end
|
52
|
+
|
53
|
+
def destroy
|
54
|
+
@cwr.destroy_consumer(self)
|
55
|
+
return DestroyedConsumer.new(path)
|
56
|
+
end
|
57
|
+
|
58
|
+
def id
|
59
|
+
@path ? @path.split("/")[-1] : nil
|
60
|
+
end
|
61
|
+
|
62
|
+
def create_webhook(post_uri, post_data=nil, post_headers=nil)
|
63
|
+
@producer.create_webhook(self, post_uri, post_data, post_headers)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class Webhook
|
68
|
+
def initialize(cwr, path, complete=false)
|
69
|
+
@cwr = cwr
|
70
|
+
@path = path
|
71
|
+
@hooked = true
|
72
|
+
@complete = complete
|
73
|
+
end
|
74
|
+
|
75
|
+
def hooked?
|
76
|
+
raise "wtf unhookedwebhook" unless @hooked
|
77
|
+
@hooked
|
78
|
+
end
|
79
|
+
|
80
|
+
def complete?
|
81
|
+
@complete
|
82
|
+
end
|
83
|
+
|
84
|
+
def destroy
|
85
|
+
@hooked = false
|
86
|
+
end
|
87
|
+
|
88
|
+
def update
|
89
|
+
resp = @cwr.update_webhook(@path)
|
90
|
+
@complete = !resp["attempt"]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class Destroyed
|
95
|
+
def initialize(former_path)
|
96
|
+
@former_path = former_path
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class DestroyedProducer < Destroyed
|
101
|
+
end
|
102
|
+
|
103
|
+
class DestroyedConsumer < Destroyed
|
104
|
+
end
|
105
|
+
|
106
|
+
class DestroyedWebhook < Destroyed
|
107
|
+
end
|
108
|
+
|
1
109
|
class CWR
|
2
|
-
|
3
|
-
|
110
|
+
include HTTParty
|
111
|
+
|
112
|
+
attr_accessor :access_token, :email, :password, :producer
|
113
|
+
alias_method :username, :email
|
114
|
+
|
115
|
+
def initialize(captian_webhooks_base_uri='http://0.0.0.0:3000',
|
116
|
+
producer_path=nil,
|
117
|
+
consumer_path=nil,
|
118
|
+
webhook_path=nil)
|
119
|
+
@PRODUCER_PATH = producer_path || "/producers"
|
120
|
+
@CONSUMER_PATH = consumer_path || "/consumers"
|
121
|
+
@WEBHOOK_PATH = webhook_path || "/webhooks"
|
122
|
+
self.class.base_uri captian_webhooks_base_uri
|
123
|
+
end
|
124
|
+
|
125
|
+
def username=(other)
|
126
|
+
@email = other
|
127
|
+
end
|
128
|
+
|
129
|
+
def create_producer(name)
|
130
|
+
body = { producer: { name: name } }
|
131
|
+
resp = securely_post( @PRODUCER_PATH, body )
|
132
|
+
|
133
|
+
location = resp.headers['location']
|
134
|
+
producer_path = location.split(self.class.base_uri)[-1]
|
135
|
+
return Producer.new( self, name, producer_path )
|
136
|
+
end
|
137
|
+
|
138
|
+
def create_consumer(producer, name)
|
139
|
+
consumer = { name: name, producer_id: producer.id }
|
140
|
+
body = { consumer: consumer }
|
141
|
+
resp = securely_post( @CONSUMER_PATH, body )
|
142
|
+
location = resp.headers['location']
|
143
|
+
consumer_path = location.split(self.class.base_uri)[-1]
|
144
|
+
return Consumer.new( self, producer, name, consumer_path )
|
145
|
+
end
|
146
|
+
|
147
|
+
def create_webhook(consumer, post_uri, post_data=nil, post_headers=nil)
|
148
|
+
post_data = post_data.to_json unless post_data.is_a? String or post_data.nil?
|
149
|
+
post_headers = post_headers.to_json unless post_headers.is_a? String or post_headers.nil?
|
150
|
+
|
151
|
+
webhook = { post_uri: post_uri,
|
152
|
+
post_data: post_data,
|
153
|
+
consumer_id: consumer.id }
|
154
|
+
|
155
|
+
webhook[:post_headers] = post_headers if post_headers
|
156
|
+
|
157
|
+
body = { webhook: webhook }
|
158
|
+
resp = securely_post( @WEBHOOK_PATH, body )
|
159
|
+
location = resp.headers['location']
|
160
|
+
webhook_path = location.split(self.class.base_uri)[-1]
|
161
|
+
Webhook.new(self, webhook_path)
|
162
|
+
end
|
163
|
+
|
164
|
+
def update_webhook(webhook_path)
|
165
|
+
securely_get( webhook_path )
|
166
|
+
end
|
167
|
+
|
168
|
+
def destroy_consumer(consumer)
|
169
|
+
resp = securely_delete(consumer.path)
|
170
|
+
end
|
171
|
+
|
172
|
+
def destroy_producer(producer)
|
173
|
+
resp = securely_delete(producer.path)
|
174
|
+
end
|
175
|
+
|
176
|
+
def list_consumers(producer, &block)
|
177
|
+
block_given = !!block
|
178
|
+
params = { producer_id: producer.id }
|
179
|
+
resp = securely_get(@CONSUMER_PATH,
|
180
|
+
params)
|
181
|
+
consumers = resp['consumers']
|
182
|
+
collector = [] unless block_given
|
183
|
+
consumers.map do |p|
|
184
|
+
consumer = p['consumers']
|
185
|
+
name = consumer['name']
|
186
|
+
id = consumer['id']
|
187
|
+
path = "#{@CONSUMER_PATH}/#{id}"
|
188
|
+
|
189
|
+
consumer = Consumer.new( self, producer, name, path )
|
190
|
+
|
191
|
+
if block_given
|
192
|
+
yield consumer
|
193
|
+
else
|
194
|
+
collector << consumer
|
195
|
+
end
|
196
|
+
end
|
197
|
+
return collector
|
198
|
+
end
|
199
|
+
|
200
|
+
def list_producers(&block)
|
201
|
+
block_given = !!block
|
202
|
+
params = { email: @email }
|
203
|
+
resp = securely_get(@PRODUCER_PATH,
|
204
|
+
params)
|
205
|
+
producers = resp['producers']
|
206
|
+
collector = [] unless block_given
|
207
|
+
producers.map do |producer|
|
208
|
+
name = producer['name']
|
209
|
+
id = producer['id']
|
210
|
+
owner_id = producer['owner_id']
|
211
|
+
path = "#{@PRODUCER_PATH}/#{id}"
|
212
|
+
producer = Producer.new(self, name, path, owner_id)
|
213
|
+
if block_given
|
214
|
+
yield producer
|
215
|
+
else
|
216
|
+
collector << producer
|
217
|
+
end
|
218
|
+
end
|
219
|
+
return collector
|
220
|
+
end
|
221
|
+
|
222
|
+
def create_mass_webhook(producer, consumers, post_data=nil)
|
223
|
+
raise "NOT IMPLEMENTED"
|
224
|
+
end
|
225
|
+
|
226
|
+
def list_webhooks
|
227
|
+
return []
|
228
|
+
end
|
229
|
+
|
230
|
+
def yeearr
|
231
|
+
:jolly_roger
|
232
|
+
"yeearr"
|
233
|
+
end
|
234
|
+
|
235
|
+
protected
|
236
|
+
|
237
|
+
def register_producer
|
238
|
+
raise "@producer required to register" unless @producer
|
239
|
+
end
|
240
|
+
|
241
|
+
def require_access_token
|
242
|
+
create_new_access_token_if_able unless access_token
|
243
|
+
raise "access_token required for webhook" unless access_token
|
244
|
+
end
|
245
|
+
|
246
|
+
def create_new_access_token_if_able
|
247
|
+
@username ||= @email
|
248
|
+
@username and @password and new_access_token
|
249
|
+
end
|
250
|
+
|
251
|
+
def new_access_token
|
252
|
+
access_token = { email: @email,
|
253
|
+
password: @password }
|
254
|
+
body = { access_token: access_token }
|
255
|
+
headers = { 'Content-Type' => 'application/json' }
|
256
|
+
|
257
|
+
resp = self.class.post("/access_tokens",
|
258
|
+
body: body.to_json,
|
259
|
+
headers: headers)
|
260
|
+
@access_token = resp["access_token"]["id"]
|
261
|
+
end
|
262
|
+
|
263
|
+
def secure_headers(headers=nil)
|
264
|
+
headers ||= {}
|
265
|
+
# Note that rails converts headers to HTTP_AUTHORIZATION
|
266
|
+
# automatically, so on the server side it will not look
|
267
|
+
# the same as the request, even if we tack on "HTTP_"
|
268
|
+
# ourselves.
|
269
|
+
headers['AUTHORIZATION'] = @access_token
|
270
|
+
headers['Content-Type'] = 'application/json'
|
271
|
+
return headers
|
272
|
+
end
|
273
|
+
|
274
|
+
def http_exception(e, method, path, params=nil, body=nil)
|
275
|
+
error = e.message + "\n"
|
276
|
+
error += "Problem with secure #{method} to: #{path}\n"
|
277
|
+
error += "path: #{path}\n"
|
278
|
+
error += "body: #{body}" if body
|
279
|
+
error += "params: #{params}" if params
|
280
|
+
print error
|
281
|
+
raise e
|
282
|
+
end
|
283
|
+
|
284
|
+
def securely_delete(path, headers=nil)
|
285
|
+
require_access_token
|
286
|
+
headers = secure_headers(headers)
|
287
|
+
begin
|
288
|
+
resp = self.class.delete(path,
|
289
|
+
headers: headers)
|
290
|
+
check_response resp
|
291
|
+
rescue Exception => e
|
292
|
+
http_exception(e, :delete, path)
|
293
|
+
end
|
294
|
+
resp
|
295
|
+
end
|
296
|
+
|
297
|
+
def securely_get(path, params=nil, headers=nil)
|
298
|
+
require_access_token
|
299
|
+
headers = secure_headers(headers)
|
300
|
+
|
301
|
+
begin
|
302
|
+
resp = self.class.get(path, :headers => headers, :query => params)
|
303
|
+
check_response resp
|
304
|
+
rescue Exception => e
|
305
|
+
http_exception(e, :get, path, params)
|
306
|
+
end
|
307
|
+
resp
|
308
|
+
end
|
309
|
+
|
310
|
+
def securely_post(path, body, headers=nil)
|
311
|
+
require_access_token
|
312
|
+
headers = secure_headers(headers)
|
313
|
+
begin
|
314
|
+
resp = self.class.post(path,
|
315
|
+
body: body.to_json,
|
316
|
+
headers: headers)
|
317
|
+
check_response resp
|
318
|
+
rescue Exception => e
|
319
|
+
http_exception(e, :post, path, nil, body)
|
320
|
+
end
|
321
|
+
resp
|
322
|
+
end
|
323
|
+
|
324
|
+
def check_response(resp)
|
325
|
+
case resp.code
|
326
|
+
when 400
|
327
|
+
raise "400 - Bad Request"
|
328
|
+
when 401
|
329
|
+
raise "401 - Unauthorized"
|
330
|
+
when 403
|
331
|
+
raise "403 - Forbidden"
|
332
|
+
when 404
|
333
|
+
raise "404 - Not found"
|
334
|
+
when 400..499
|
335
|
+
raise "Client error with code #{resp.code}"
|
336
|
+
when 500..599
|
337
|
+
raise "Server error with code #{resp.code}"
|
338
|
+
end
|
4
339
|
end
|
5
340
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cwr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-15 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: This fine gem gives you unparalleled access
|
15
|
-
|
16
|
-
Problem with too many people smashing your rails server down by polling
|
17
|
-
anything has changed? Problem no longer!
|
14
|
+
description: This fine gem gives you unparalleled access to the captain webhooks API.
|
15
|
+
Once launched, this enables you to create webhooks beyond your hearts reasonable
|
16
|
+
desires. Problem with too many people smashing your rails server down by polling
|
17
|
+
to see if anything has changed? Problem no longer!
|
18
18
|
email: zachaysan@gmail.com
|
19
19
|
executables: []
|
20
20
|
extensions: []
|