simperium 0.0.1 → 0.0.2
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/simperium/version.rb +1 -1
- data/lib/simperium.rb +346 -344
- metadata +4 -4
data/lib/simperium/version.rb
CHANGED
data/lib/simperium.rb
CHANGED
@@ -9,417 +9,419 @@ require File.join(File.dirname(__FILE__), 'simperium/error_handling')
|
|
9
9
|
#state file is not shared between processes on Heroku
|
10
10
|
UUID.state_file = false
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
host
|
12
|
+
module Simperium
|
13
|
+
class Auth
|
14
|
+
def initialize(appname, api_key, host=nil,scheme='https')
|
15
|
+
if host == nil
|
16
|
+
host = ENV['SIMPERIUM_AUTHHOST'] || 'auth.simperium.com'
|
17
|
+
end
|
18
|
+
|
19
|
+
@appname = appname
|
20
|
+
@api_key = api_key
|
21
|
+
@host = host
|
22
|
+
@scheme = scheme
|
16
23
|
end
|
17
|
-
|
18
|
-
@appname = appname
|
19
|
-
@api_key = api_key
|
20
|
-
@host = host
|
21
|
-
@scheme = scheme
|
22
|
-
end
|
23
|
-
|
24
|
-
def _auth_header
|
25
|
-
return {"X-Simperium-API-Key" => "#{@api_key}"}
|
26
|
-
end
|
27
24
|
|
28
|
-
|
29
|
-
|
30
|
-
opts = {:url => url,
|
31
|
-
:method => :post,
|
32
|
-
:open_timeout => 30,
|
33
|
-
:timeout => 80}
|
34
|
-
|
35
|
-
if data
|
36
|
-
opts = opts.merge({:payload => data})
|
37
|
-
end
|
38
|
-
|
39
|
-
if headers.nil?
|
40
|
-
headers = {}
|
41
|
-
end
|
42
|
-
opts = opts.merge({:headers => headers})
|
43
|
-
|
44
|
-
if method
|
45
|
-
opts = opts.merge({:method => method})
|
25
|
+
def _auth_header
|
26
|
+
return {"X-Simperium-API-Key" => "#{@api_key}"}
|
46
27
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
raise
|
28
|
+
|
29
|
+
def _request(url, data=nil, headers=nil, method=nil)
|
30
|
+
url = "#{@scheme}://#{@host}/1/#{url}"
|
31
|
+
opts = {:url => url,
|
32
|
+
:method => :post,
|
33
|
+
:open_timeout => 30,
|
34
|
+
:timeout => 80}
|
35
|
+
|
36
|
+
if data
|
37
|
+
opts = opts.merge({:payload => data})
|
58
38
|
end
|
59
|
-
|
60
|
-
if
|
61
|
-
|
62
|
-
|
39
|
+
|
40
|
+
if headers.nil?
|
41
|
+
headers = {}
|
42
|
+
end
|
43
|
+
opts = opts.merge({:headers => headers})
|
44
|
+
|
45
|
+
if method
|
46
|
+
opts = opts.merge({:method => method})
|
47
|
+
end
|
48
|
+
|
49
|
+
begin
|
50
|
+
response = RestClient::Request.execute(opts)
|
51
|
+
rescue SocketError => e
|
52
|
+
ErrorHandling.handle_restclient_error(e)
|
53
|
+
rescue NoMethodError => e
|
54
|
+
if e.message =~ /\WRequestFailed\W/
|
55
|
+
e = StandardError.new('Unexpected HTTP response code')
|
56
|
+
ErrorHandling.handle_restclient_error(e)
|
57
|
+
else
|
58
|
+
raise
|
59
|
+
end
|
60
|
+
rescue RestClient::ExceptionWithResponse => e
|
61
|
+
if rcode = e.http_code and rbody = e.http_body
|
62
|
+
ErrorHandling.handle_api_error(rcode, rbody)
|
63
|
+
else
|
64
|
+
ErrorHandling.handle_restclient_error(e)
|
65
|
+
end
|
66
|
+
rescue RestClient::Exception, Errno::ECONNREFUSED => e
|
63
67
|
ErrorHandling.handle_restclient_error(e)
|
64
68
|
end
|
65
|
-
rescue RestClient::Exception, Errno::ECONNREFUSED => e
|
66
|
-
ErrorHandling.handle_restclient_error(e)
|
67
|
-
end
|
68
|
-
|
69
|
-
return response
|
70
|
-
end
|
71
|
-
|
72
|
-
def create(username, password)
|
73
|
-
data = {
|
74
|
-
'client_id' => @api_key,
|
75
|
-
'username' => username,
|
76
|
-
'password'=> password }
|
77
|
-
|
78
|
-
response = self._request(@appname+'/create/', data)
|
79
|
-
return JSON.load(response.body)['access_token']
|
80
|
-
end
|
81
69
|
|
82
|
-
|
83
|
-
data = {
|
84
|
-
'username' => username,
|
85
|
-
'password' => password }
|
86
|
-
response = self._request(@appname+'/authorize/', data, headers=_auth_header())
|
87
|
-
return JSON.load(response.body)['access_token']
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
class Bucket
|
92
|
-
def initialize(appname, auth_token, bucket, options={})
|
93
|
-
defaults = { :userid => nil, :host => nil, :scheme => 'https', :clientid => nil }
|
94
|
-
unless options.empty?
|
95
|
-
options = defaults.merge(options)
|
96
|
-
else
|
97
|
-
options = defaults
|
70
|
+
return response
|
98
71
|
end
|
99
72
|
|
100
|
-
|
101
|
-
|
73
|
+
def create(username, password)
|
74
|
+
data = {
|
75
|
+
'client_id' => @api_key,
|
76
|
+
'username' => username,
|
77
|
+
'password'=> password }
|
78
|
+
|
79
|
+
response = self._request(@appname+'/create/', data)
|
80
|
+
return JSON.load(response.body)['access_token']
|
102
81
|
end
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
if options[:clientid] == nil
|
112
|
-
uuid = UUID.new
|
113
|
-
random_string = uuid.generate(:compact)
|
114
|
-
@clientid = "rb-#{random_string}"
|
115
|
-
else
|
116
|
-
@clientid = options[:clientid]
|
82
|
+
|
83
|
+
def authorize(username, password)
|
84
|
+
data = {
|
85
|
+
'username' => username,
|
86
|
+
'password' => password }
|
87
|
+
response = self._request(@appname+'/authorize/', data, headers=_auth_header())
|
88
|
+
return JSON.load(response.body)['access_token']
|
117
89
|
end
|
118
90
|
end
|
119
91
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
92
|
+
class Bucket
|
93
|
+
def initialize(appname, auth_token, bucket, options={})
|
94
|
+
defaults = { :userid => nil, :host => nil, :scheme => 'https', :clientid => nil }
|
95
|
+
unless options.empty?
|
96
|
+
options = defaults.merge(options)
|
97
|
+
else
|
98
|
+
options = defaults
|
99
|
+
end
|
127
100
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
end
|
101
|
+
if options[:host] == nil
|
102
|
+
options[:host] = ENV['SIMPERIUM_APIHOST'] || 'api.simperium.com'
|
103
|
+
end
|
132
104
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
105
|
+
@userid = options[:userid]
|
106
|
+
@host = options[:host]
|
107
|
+
@scheme = options[:scheme]
|
108
|
+
@appname = appname
|
109
|
+
@bucket = bucket
|
110
|
+
@auth_token = auth_token
|
111
|
+
|
112
|
+
if options[:clientid] == nil
|
113
|
+
uuid = UUID.new
|
114
|
+
random_string = uuid.generate(:compact)
|
115
|
+
@clientid = "rb-#{random_string}"
|
116
|
+
else
|
117
|
+
@clientid = options[:clientid]
|
118
|
+
end
|
146
119
|
end
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
120
|
+
|
121
|
+
def _auth_header
|
122
|
+
headers = {"X-Simperium-Token" => "#{@auth_token}"}
|
123
|
+
unless @userid.nil?
|
124
|
+
headers["X-Simperium-User"] = @userid
|
125
|
+
end
|
126
|
+
return headers
|
151
127
|
end
|
152
|
-
|
153
|
-
|
154
|
-
|
128
|
+
|
129
|
+
def _gen_ccid
|
130
|
+
ccid = UUID.new
|
131
|
+
return ccid.generate(:compact)
|
155
132
|
end
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
else
|
167
|
-
raise
|
133
|
+
|
134
|
+
def _request(url, data=nil, headers=nil, method=nil, timeout=nil)
|
135
|
+
url = "#{@scheme}://#{@host}/1/#{url}"
|
136
|
+
opts = {:url => url,
|
137
|
+
:method => :post,
|
138
|
+
:open_timeout => 30,
|
139
|
+
:timeout => 80}
|
140
|
+
|
141
|
+
if data
|
142
|
+
opts = opts.merge({:payload => data})
|
168
143
|
end
|
169
|
-
|
170
|
-
if
|
171
|
-
|
172
|
-
|
144
|
+
|
145
|
+
if headers.nil?
|
146
|
+
headers = {}
|
147
|
+
end
|
148
|
+
opts = opts.merge({:headers => headers})
|
149
|
+
|
150
|
+
if method
|
151
|
+
opts = opts.merge({:method => method})
|
152
|
+
end
|
153
|
+
|
154
|
+
if timeout
|
155
|
+
opts = opts.merge({:timeout => timeout})
|
156
|
+
end
|
157
|
+
|
158
|
+
puts opts
|
159
|
+
begin
|
160
|
+
response = RestClient::Request.execute(opts)
|
161
|
+
rescue SocketError => e
|
162
|
+
ErrorHandling.handle_restclient_error(e)
|
163
|
+
rescue NoMethodError => e
|
164
|
+
if e.message =~ /\WRequestFailed\W/
|
165
|
+
e = StandardError.new('Unexpected HTTP response code')
|
166
|
+
ErrorHandling.handle_restclient_error(e)
|
167
|
+
else
|
168
|
+
raise
|
169
|
+
end
|
170
|
+
rescue RestClient::ExceptionWithResponse => e
|
171
|
+
if rcode = e.http_code and rbody = e.http_body
|
172
|
+
ErrorHandling.handle_api_error(rcode, rbody)
|
173
|
+
else
|
174
|
+
ErrorHandling.handle_restclient_error(e)
|
175
|
+
end
|
176
|
+
rescue RestClient::Exception, Errno::ECONNREFUSED => e
|
173
177
|
ErrorHandling.handle_restclient_error(e)
|
174
178
|
end
|
175
|
-
rescue RestClient::Exception, Errno::ECONNREFUSED => e
|
176
|
-
ErrorHandling.handle_restclient_error(e)
|
177
|
-
end
|
178
179
|
|
179
|
-
|
180
|
-
end
|
181
|
-
|
182
|
-
def index(options={})
|
183
|
-
defaults = {:data=>nil, :mark=>nil, :limit=>nil, :since=>nil}
|
184
|
-
unless options.empty?
|
185
|
-
options = defaults.merge(options)
|
186
|
-
else
|
187
|
-
options = defaults
|
180
|
+
return response
|
188
181
|
end
|
182
|
+
|
183
|
+
def index(options={})
|
184
|
+
defaults = {:data=>nil, :mark=>nil, :limit=>nil, :since=>nil}
|
185
|
+
unless options.empty?
|
186
|
+
options = defaults.merge(options)
|
187
|
+
else
|
188
|
+
options = defaults
|
189
|
+
end
|
189
190
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
191
|
+
data = options[:data]
|
192
|
+
mark = options[:mark]
|
193
|
+
limit = options[:limit]
|
194
|
+
since = options[:since]
|
194
195
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
196
|
+
url = "#{@appname}/#{@bucket}/index?"
|
197
|
+
|
198
|
+
if data
|
199
|
+
url += "&data=1"
|
200
|
+
end
|
200
201
|
|
201
|
-
|
202
|
-
|
203
|
-
|
202
|
+
if mark
|
203
|
+
url += "&mark=#{mark.to_str}"
|
204
|
+
end
|
204
205
|
|
205
|
-
|
206
|
-
|
206
|
+
if limit
|
207
|
+
url += "&limit=#{limit.to_s}"
|
208
|
+
end
|
209
|
+
|
210
|
+
if since
|
211
|
+
url += "&since=#{since.to_str}"
|
212
|
+
end
|
213
|
+
|
214
|
+
response = self._request(url, data=nil, headers=_auth_header(), method='GET')
|
215
|
+
return JSON.load(response.body)
|
207
216
|
end
|
208
217
|
|
209
|
-
|
210
|
-
|
218
|
+
def get(item, options={})
|
219
|
+
defaults = {:default=>nil, :version=>nil}
|
220
|
+
unless options.empty?
|
221
|
+
options = defaults.merge(options)
|
222
|
+
else
|
223
|
+
options = defaults
|
224
|
+
end
|
225
|
+
default = options[:default]
|
226
|
+
version = options[:version]
|
227
|
+
|
228
|
+
url = "#{@appname}/#{@bucket}/i/#{item}"
|
229
|
+
unless version.nil?
|
230
|
+
url += "/v/#{version}"
|
231
|
+
end
|
232
|
+
|
233
|
+
response = self._request(url, data=nil, headers=_auth_header(), method='GET')
|
234
|
+
return JSON.load(response.body)
|
211
235
|
end
|
212
236
|
|
213
|
-
|
214
|
-
|
215
|
-
|
237
|
+
def post(item, data, options={})
|
238
|
+
defaults = {:version=>nil, :ccid=>nil, :include_response=>false, :replace=>false}
|
239
|
+
unless options.empty?
|
240
|
+
options = defaults.merge(options)
|
241
|
+
else
|
242
|
+
options = defaults
|
243
|
+
end
|
216
244
|
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
else
|
222
|
-
options = defaults
|
223
|
-
end
|
224
|
-
default = options[:default]
|
225
|
-
version = options[:version]
|
245
|
+
version = options[:version]
|
246
|
+
ccid = options[:ccid]
|
247
|
+
include_response = options[:include_response]
|
248
|
+
replace = options[:replace]
|
226
249
|
|
227
|
-
|
228
|
-
|
229
|
-
|
250
|
+
if ccid.nil?
|
251
|
+
ccid = self._gen_ccid()
|
252
|
+
end
|
253
|
+
url = "#{@appname}/#{@bucket}/i/#{item}"
|
254
|
+
|
255
|
+
if version
|
256
|
+
url += "/v/#{version}"
|
257
|
+
end
|
258
|
+
url += "?clientid=#{@clientid}&ccid=#{ccid}"
|
259
|
+
|
260
|
+
if include_response
|
261
|
+
url += "&response=1"
|
262
|
+
end
|
263
|
+
|
264
|
+
if replace
|
265
|
+
url += "&replace=1"
|
266
|
+
end
|
267
|
+
data = JSON.dump(data)
|
268
|
+
|
269
|
+
response = self._request(url, data, headers=_auth_header())
|
270
|
+
if include_response
|
271
|
+
return item, JSON.load(response.body)
|
272
|
+
else
|
273
|
+
return item
|
274
|
+
end
|
230
275
|
end
|
231
276
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
def post(item, data, options={})
|
237
|
-
defaults = {:version=>nil, :ccid=>nil, :include_response=>false, :replace=>false}
|
238
|
-
unless options.empty?
|
239
|
-
options = defaults.merge(options)
|
240
|
-
else
|
241
|
-
options = defaults
|
277
|
+
def new(data, ccid=nil)
|
278
|
+
uuid = UUID.new
|
279
|
+
return self.post(uuid.generate(:compact), data, ccid=ccid)
|
242
280
|
end
|
243
281
|
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
replace = options[:replace]
|
282
|
+
def set(item, data, options={})
|
283
|
+
return self.post(item, data, options)
|
284
|
+
end
|
248
285
|
|
249
|
-
|
286
|
+
def delete(item, version=nil)
|
250
287
|
ccid = self._gen_ccid()
|
288
|
+
url = "#{@appname}/#{@bucket}/i/{item}"
|
289
|
+
|
290
|
+
if version
|
291
|
+
url += "/v/#{version}"
|
292
|
+
end
|
293
|
+
|
294
|
+
url += "?clientid=#{@clientid}&ccid=#{ccid}"
|
295
|
+
response = self._request(url, data=nil, headers=_auth_header(), method='DELETE')
|
296
|
+
if response.body.strip.nil?
|
297
|
+
return ccid
|
298
|
+
end
|
251
299
|
end
|
252
|
-
url = "#{@appname}/#{@bucket}/i/#{item}"
|
253
|
-
|
254
|
-
if version
|
255
|
-
url += "/v/#{version}"
|
256
|
-
end
|
257
|
-
url += "?clientid=#{@clientid}&ccid=#{ccid}"
|
258
|
-
|
259
|
-
if include_response
|
260
|
-
url += "&response=1"
|
261
|
-
end
|
262
|
-
|
263
|
-
if replace
|
264
|
-
url += "&replace=1"
|
265
|
-
end
|
266
|
-
data = JSON.dump(data)
|
267
|
-
|
268
|
-
response = self._request(url, data, headers=_auth_header())
|
269
|
-
if include_response
|
270
|
-
return item, JSON.load(response.body)
|
271
|
-
else
|
272
|
-
return item
|
273
|
-
end
|
274
|
-
end
|
275
300
|
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
301
|
+
def changes(options={})
|
302
|
+
defautls = {:cv=>nil, :timeout=>nil}
|
303
|
+
unless options.empty?
|
304
|
+
options = defaults.merge(options)
|
305
|
+
else
|
306
|
+
options = defaults
|
307
|
+
end
|
280
308
|
|
281
|
-
|
282
|
-
|
283
|
-
end
|
309
|
+
cv = option[:cv]
|
310
|
+
timeout = option[:timeout]
|
284
311
|
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
312
|
+
url = "#{@appname}/#{@bucket}/changes?clientid=#{@clientid}"
|
313
|
+
unless cv.nil?
|
314
|
+
url += "&cv=#{cv}"
|
315
|
+
end
|
316
|
+
headers = _auth_header()
|
317
|
+
|
318
|
+
response = self._request(url, data=nil, headers=headers, method='GET', timeout=timeout)
|
319
|
+
return JSON.load(response.body)
|
291
320
|
end
|
292
321
|
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
322
|
+
def all(options={})
|
323
|
+
defaults = {:cv=>nil, :data=>nil, :username=>false, :most_recent=>false, :timeout=>nil}
|
324
|
+
unless options.empty?
|
325
|
+
options = defaults.merge(options)
|
326
|
+
else
|
327
|
+
options = defaults
|
328
|
+
end
|
299
329
|
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
options = defaults
|
306
|
-
end
|
330
|
+
cv = options[:cv]
|
331
|
+
data = options[:data]
|
332
|
+
username = options[:username]
|
333
|
+
most_recent = options[:most_recent]
|
334
|
+
timeout = options[:timeout]
|
307
335
|
|
308
|
-
|
309
|
-
|
336
|
+
url = "#{@appname}/#{@bucket}/all?clientid=#{@clientid}"
|
337
|
+
unless cv.nil?
|
338
|
+
url += "&cv=#{cv}"
|
339
|
+
end
|
310
340
|
|
311
|
-
|
312
|
-
|
313
|
-
|
341
|
+
if username
|
342
|
+
url += "&username=1"
|
343
|
+
end
|
344
|
+
|
345
|
+
if data
|
346
|
+
url += "&data=1"
|
347
|
+
end
|
348
|
+
|
349
|
+
if most_recent
|
350
|
+
url += "&most_recent=1"
|
351
|
+
end
|
352
|
+
|
353
|
+
headers = _auth_header()
|
354
|
+
|
355
|
+
response = self._request(url, data=nil, headers=headers, method='GET', timeout=timeout)
|
356
|
+
return JSON.load(response.body)
|
314
357
|
end
|
315
|
-
headers = _auth_header()
|
316
|
-
|
317
|
-
response = self._request(url, data=nil, headers=headers, method='GET', timeout=timeout)
|
318
|
-
return JSON.load(response.body)
|
319
358
|
end
|
320
359
|
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
options
|
325
|
-
|
326
|
-
|
327
|
-
|
360
|
+
class User
|
361
|
+
def initialize(appname, auth_token, options={})
|
362
|
+
defaults = {:host=>nil, :scheme=>'https', :clientid=>nil}
|
363
|
+
unless options.empty?
|
364
|
+
options = defaults.merge(options)
|
365
|
+
else
|
366
|
+
options = defaults
|
367
|
+
end
|
328
368
|
|
329
|
-
|
330
|
-
|
331
|
-
username = options[:username]
|
332
|
-
most_recent = options[:most_recent]
|
333
|
-
timeout = options[:timeout]
|
369
|
+
@bucket = Bucket.new(appname, auth_token, 'user',
|
370
|
+
options=options)
|
334
371
|
|
335
|
-
|
336
|
-
|
337
|
-
|
372
|
+
url = "#{appname}/user"
|
373
|
+
response = @bucket._request(url, data=nil, headers=@bucket._auth_header(), method='GET')
|
374
|
+
response = JSON.load(response.body)
|
375
|
+
@userid = response['userid']
|
338
376
|
end
|
339
377
|
|
340
|
-
|
341
|
-
|
378
|
+
def get
|
379
|
+
return @bucket.get(@userid)
|
342
380
|
end
|
343
381
|
|
344
|
-
|
345
|
-
|
346
|
-
end
|
347
|
-
|
348
|
-
if most_recent
|
349
|
-
url += "&most_recent=1"
|
350
|
-
end
|
351
|
-
|
352
|
-
headers = _auth_header()
|
353
|
-
|
354
|
-
response = self._request(url, data=nil, headers=headers, method='GET', timeout=timeout)
|
355
|
-
return JSON.load(response.body)
|
356
|
-
end
|
357
|
-
end
|
358
|
-
|
359
|
-
class User
|
360
|
-
def initialize(appname, auth_token, options={})
|
361
|
-
defaults = {:host=>nil, :scheme=>'https', :clientid=>nil}
|
362
|
-
unless options.empty?
|
363
|
-
options = defaults.merge(options)
|
364
|
-
else
|
365
|
-
options = defaults
|
382
|
+
def post(data)
|
383
|
+
@bucket.post(@userid, data)
|
366
384
|
end
|
367
|
-
|
368
|
-
@bucket = Bucket.new(appname, auth_token, 'user',
|
369
|
-
options=options)
|
370
|
-
|
371
|
-
url = "#{appname}/user"
|
372
|
-
response = @bucket._request(url, data=nil, headers=@bucket._auth_header(), method='GET')
|
373
|
-
response = JSON.load(response.body)
|
374
|
-
@userid = response['userid']
|
375
385
|
end
|
376
386
|
|
377
|
-
|
378
|
-
|
379
|
-
|
387
|
+
class Api
|
388
|
+
def initialize(appname, auth_token, options={})
|
389
|
+
@appname = appname
|
390
|
+
@token = auth_token
|
391
|
+
@_options = options
|
380
392
|
|
381
|
-
|
382
|
-
|
383
|
-
end
|
384
|
-
end
|
385
|
-
|
386
|
-
class Api
|
387
|
-
def initialize(appname, auth_token, options={})
|
388
|
-
@appname = appname
|
389
|
-
@token = auth_token
|
390
|
-
@_options = options
|
393
|
+
@getitem = {}
|
394
|
+
end
|
391
395
|
|
392
|
-
|
393
|
-
|
396
|
+
def method_missing(method_sym, *arguments, &block)
|
397
|
+
#the first argument is a Symbol, so you need to_s it you want to pattern match
|
398
|
+
unless method_sym.to_s =~ /=$/
|
399
|
+
if method_sym.to_s == 'user'
|
400
|
+
@getitem[method_sym] ||= User.new(@appname, @token)
|
401
|
+
else
|
402
|
+
@getitem[method_sym] ||= Bucket.new(@appname, @token, method_sym)
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
394
406
|
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
if method_sym.to_s == 'user'
|
399
|
-
@getitem[method_sym] ||= User.new(@appname, @token)
|
407
|
+
def respond_to?(method_sym, include_private = false)
|
408
|
+
if method_sym.to_s =~ /^(.*)$/
|
409
|
+
true
|
400
410
|
else
|
401
|
-
|
411
|
+
super
|
402
412
|
end
|
403
|
-
|
413
|
+
end
|
404
414
|
end
|
405
415
|
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
416
|
+
class Admin < Api
|
417
|
+
def initialize(appname, admin_token, options={})
|
418
|
+
@appname = appname
|
419
|
+
@token = admin_token
|
420
|
+
@_options = options
|
411
421
|
end
|
412
|
-
end
|
413
|
-
end
|
414
|
-
|
415
|
-
class Admin < Api
|
416
|
-
def initialize(appname, admin_token, options={})
|
417
|
-
@appname = appname
|
418
|
-
@token = admin_token
|
419
|
-
@_options = options
|
420
|
-
end
|
421
422
|
|
422
|
-
|
423
|
-
|
423
|
+
def as_user(userid)
|
424
|
+
return Api.new(@appname, @token, userid=userid, @_options)
|
425
|
+
end
|
424
426
|
end
|
425
|
-
end
|
427
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simperium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ray Ventura
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-06-
|
18
|
+
date: 2012-06-21 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rest-client
|