rhosync_api 0.0.4 → 0.0.5

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.
Files changed (4) hide show
  1. data/History.txt +1 -0
  2. data/LICENSE.txt +10 -0
  3. data/lib/rhosync_api.rb +452 -0
  4. metadata +7 -5
data/History.txt CHANGED
@@ -3,3 +3,4 @@ version 0.0.1 - 5 August 2011
3
3
  version 0.0.2 - 5 August 2011
4
4
  version 0.0.3 - 5 August 2011
5
5
  version 0.0.4 - 6 August 2011
6
+ version 0.0.5 - 6 August 2011
data/LICENSE.txt ADDED
@@ -0,0 +1,10 @@
1
+ Created by Raul Mantilla Assia (2011)
2
+
3
+
4
+
5
+ Based on the rhomobile documentation
6
+
7
+ All the comments in this gem was copied from the Rhomobile web page, and don't belong to the author.
8
+
9
+ http://docs.rhomobile.com/rhosync/rest-api
10
+
@@ -0,0 +1,452 @@
1
+ #Based on the rhomobile documentation
2
+ #http://docs.rhomobile.com/rhosync/rest-api
3
+
4
+ require 'rest_client'
5
+ require 'json'
6
+
7
+ class RhosyncApi
8
+
9
+ def initialize
10
+ $server = ""
11
+ $username = ""
12
+ $password = ""
13
+ $token = ""
14
+ end
15
+
16
+ #login -- rhosync
17
+ def login(server,admin,password)
18
+ $server = server
19
+ $username = admin
20
+ $password = password
21
+ $token = get_api_token
22
+ end
23
+
24
+ #Before you can use RhoSync API you should get API token:
25
+ def get_api_token
26
+ uri = URI.parse($server)
27
+ http = Net::HTTP.new(uri.host,uri.port)
28
+ begin
29
+ res,data = http.post( '/login',
30
+ {:login => $username, :password => $password}.to_json,
31
+ {'Content-Type' => 'application/json'} )
32
+ cookie = res.response['set-cookie'].split('; ')[0].split('=')
33
+ cookie = {cookie[0] => URI.escape(cookie[1])}
34
+ token = RestClient.post("#{$server}/api/get_api_token",'',{:cookies => cookie})
35
+ rescue=>e
36
+ cant_connect_rhosync(e)
37
+ end
38
+ end
39
+
40
+ #Returns license information of the currently used license
41
+ def get_license_info
42
+ unless $token.nil?
43
+ begin
44
+ license_info = RestClient.post(
45
+ "#{$server}/api/get_license_info",
46
+ {:api_token => $token}.to_json, :content_type => :json
47
+ ).body
48
+ JSON.parse(license_info)
49
+ rescue=>e
50
+ cant_connect_rhosync(e)
51
+ end
52
+ else
53
+ access_denied
54
+ end
55
+ end
56
+
57
+ #Reset the server: flush db and re-bootstrap server
58
+ def reset
59
+ unless $token.nil?
60
+ begin
61
+ RestClient.post("#{$server}/api/reset",
62
+ { :api_token => $token }.to_json,
63
+ :content_type => :json
64
+ )
65
+ rescue=>e
66
+ cant_connect_rhosync(e)
67
+ end
68
+ else
69
+ access_denied
70
+ end
71
+ end
72
+
73
+ # :message - message which will be used to display notification popup dialog on the device
74
+ # :badge - iphone specific badge
75
+ # :sound - name of the sound file to play upon receiving PUSH notification
76
+ # :vibrate - number of seconds to vibrate upon receiving PUSH notification
77
+ # :sources - list of data source names to be synced upon receiving PUSH notification
78
+ def ping(user_id,ping_params)
79
+ unless $token.nil?
80
+ unless user_id.nil?
81
+ begin
82
+ RestClient.post(
83
+ "#{$server}/api/ping",ping_params.to_json,
84
+ :content_type => :json
85
+ )
86
+ rescue=>e
87
+ cant_connect_rhosync(e)
88
+ end
89
+ else
90
+ puts "the user's ID can't be null "
91
+ nil
92
+ end
93
+ else
94
+ access_denied
95
+ end
96
+ end
97
+
98
+ #List users registered with this RhoSync application.
99
+ def list_users
100
+ unless $token.nil?
101
+ begin
102
+ users = RestClient.post(
103
+ "#{$server}/api/list_users",
104
+ { :api_token => $token }.to_json,
105
+ :content_type => :json
106
+ ).body
107
+ JSON.parse(users)
108
+ rescue=>e
109
+ cant_connect_rhosync(e)
110
+ end
111
+ else
112
+ access_denied
113
+ end
114
+ end
115
+
116
+ #Create a user in this RhoSync application.
117
+ def create_user(login,password)
118
+ unless $token.nil?
119
+ unless login.nil?
120
+ begin
121
+ RestClient.post("#{$server}/api/create_user",
122
+ {
123
+ :api_token => $token,
124
+ :attributes => {
125
+ :login => login,
126
+ :password => password
127
+ }
128
+ }.to_json,
129
+ :content_type => :json
130
+ )
131
+ rescue=>e
132
+ cant_connect_rhosync(e)
133
+ end
134
+ else
135
+ puts "the user's ID can't be null "
136
+ nil
137
+ end
138
+ else
139
+ access_denied
140
+ end
141
+ end
142
+
143
+ #Delete User and all associated devices from the RhoSync application.
144
+ def delete_user(user_id)
145
+ unless $token.nil?
146
+ unless user_id.nil?
147
+ begin
148
+ RestClient.post(
149
+ "#{$server}/api/delete_user",
150
+ {
151
+ :api_token => $token,
152
+ :user_id => user_id
153
+ }.to_json,
154
+ :content_type => :json
155
+ )
156
+ rescue=>e
157
+ cant_connect_rhosync(e)
158
+ end
159
+ else
160
+ puts "the user's ID can't be null "
161
+ nil
162
+ end
163
+ else
164
+ access_denied
165
+ end
166
+ end
167
+
168
+ #List clients (devices) associated with given user.
169
+ def list_clients(user_id)
170
+ unless $token.nil?
171
+ begin
172
+ clients = RestClient.post("#{$server}/api/list_clients",
173
+ {
174
+ :api_token => $token,
175
+ :user_id => user_id
176
+ }.to_json,
177
+ :content_type => :json
178
+ ).body
179
+ JSON.parse(clients)
180
+ rescue=>e
181
+ cant_connect_rhosync(e)
182
+ end
183
+ else
184
+ access_denied
185
+ end
186
+ end
187
+
188
+ #Creates a client (device) for a given user.
189
+ def create_client(user_id)
190
+ unless $token.nil?
191
+ unless user_id.nil?
192
+ begin
193
+ RestClient.post(
194
+ "#{$server}/api/create_client",
195
+ {
196
+ :api_token => $token,
197
+ :user_id => user_id
198
+ }.to_json,
199
+ :content_type => :json
200
+ ).body
201
+ rescue=>e
202
+ cant_connect_rhosync(e)
203
+ end
204
+ else
205
+ puts "the user's ID can't be null "
206
+ nil
207
+ end
208
+ else
209
+ access_denied
210
+ end
211
+ end
212
+
213
+ #Deletes the specified client (device).
214
+ def delete_client(user_id,client_id)
215
+ unless $token.nil?
216
+ unless user_id.nil? and client_id.nil?
217
+ begin
218
+ RestClient.post(
219
+ "#{$server}/api/delete_client",
220
+ {
221
+ :api_token => $token,
222
+ :user_id => user_id,
223
+ :client_id => client_id
224
+ }.to_json,
225
+ :content_type => :json
226
+ )
227
+ rescue=>e
228
+ cant_connect_rhosync(e)
229
+ end
230
+ else
231
+ puts "the user's ID and client's ID can't be null "
232
+ nil
233
+ end
234
+ else
235
+ access_denied
236
+ end
237
+ end
238
+
239
+ #Returns client (device) attributes, such as device_type, device_pin, device_port. These attributes used by RhoSync push.
240
+ def get_client_params(client_id)
241
+ unless $token.nil?
242
+ begin
243
+ client_params = RestClient.post(
244
+ "#{$server}/api/get_client_params",
245
+ {
246
+ :api_token => $token,
247
+ :client_id => client_id
248
+ }.to_json,
249
+ :content_type => :json
250
+ ).body
251
+ JSON.parse(client_params)
252
+ rescue=>e
253
+ cant_connect_rhosync(e)
254
+ end
255
+ else
256
+ access_denied
257
+ end
258
+ end
259
+
260
+ #Return list of source adapters for this RhoSync application.
261
+ def list_sources(partition = nil)
262
+ unless $token.nil?
263
+ partition = "user" if partition.nil?
264
+ begin
265
+ sources = RestClient.post("#{$server}/api/list_sources",
266
+ {
267
+ :api_token => $token,
268
+ :partition_type => partition
269
+ }.to_json,
270
+ :content_type => :json
271
+ ).body
272
+ JSON.parse(sources)
273
+ rescue=>e
274
+ cant_connect_rhosync(e)
275
+ end
276
+ else
277
+ access_denied
278
+ end
279
+ end
280
+
281
+
282
+ #Return attributes associated with a given source:
283
+
284
+ # name � name of the data source
285
+ # poll_interval � query poll interval; defines how often RhoSync will call source adapter to query for new data, set to -1 to disable polling, 0 to always poll
286
+ # partition_type � to share data across all users, set partition to :app; otherwise use :user partition (default)
287
+ # sync_type � set to :bulk_only to disable :incremental sync; regular sync is :incremental (default)
288
+ # queue � name of the queue for both query and create/update/delete (CUD) jobs (used if no specific queues not specified)
289
+ # query_queue � name of query queue
290
+ # cud_queue � name of CUD queue
291
+ def get_source_params(source_id)
292
+ unless $token.nil?
293
+ begin
294
+ attributes = RestClient.post("#{$server}/api/get_source_params",
295
+ {
296
+ :api_token => $token,
297
+ :source_id => source_id
298
+ }.to_json,
299
+ :content_type => :json
300
+ ).body
301
+ JSON.parse(attributes)
302
+ rescue=>e
303
+ cant_connect_rhosync(e)
304
+ end
305
+ else
306
+ access_denied
307
+ end
308
+ end
309
+
310
+ # Return list of document keys associated with given source and user.
311
+ # If :user_id set to �*�, this call will return list of keys for �shared� documents.
312
+ # MD(:md) � master document; represents state of the backend (set of all objects for the given app/user/source on the backend service).
313
+ def list_source_docs(user_id,source_id)
314
+ unless $token.nil?
315
+ begin
316
+ docs = RestClient.post(
317
+ "#{$server}/api/list_source_docs",
318
+ {
319
+ :api_token => $token,
320
+ :source_id => source_id,
321
+ :user_id => user_id
322
+ }.to_json,
323
+ :content_type => :json
324
+ ).body
325
+ JSON.parse(docs)
326
+ rescue=>e
327
+ cant_connect_rhosync(e)
328
+ end
329
+ else
330
+ access_denied
331
+ end
332
+ end
333
+
334
+ #Return content of a given document (client or source).
335
+ def get_db_doc(doc,data_type = nil)
336
+ unless $token.nil?
337
+ begin
338
+ res = RestClient.post(
339
+ "#{$server}/api/get_db_doc",
340
+ {
341
+ :api_token => $token,
342
+ :doc => doc,
343
+ :data_type => data_type
344
+ }.to_json,
345
+ :content_type => :json
346
+ ).body
347
+ JSON.parse(res)
348
+ rescue=>e
349
+ cant_connect_rhosync(e)
350
+ end
351
+ else
352
+ access_denied
353
+ end
354
+ end
355
+
356
+ #Sets the content of the specified server document.
357
+ #Data should be either a string or hash of hashes. Data type should be set accordingly.
358
+ def set_db_doc(doc,data,data_type = nil)
359
+ unless $token.nil?
360
+ begin
361
+ RestClient.post(
362
+ "#{$server}/api/set_db_doc",
363
+ {
364
+ :api_token => $token,
365
+ :doc => doc,
366
+ :data => data,
367
+ :data_type => data_type
368
+ }.to_json,
369
+ :content_type => :json
370
+ )
371
+ rescue=>e
372
+ cant_connect_rhosync(e)
373
+ end
374
+ else
375
+ access_denied
376
+ end
377
+ end
378
+
379
+ #Returns list of document keys associated with particular client.
380
+ #These documents are used by the server to sync data with the client.
381
+ #CD (:cd) � client document; represents the state of the client (set of all objects on the given client).
382
+ def list_client_docs(client_id,source_id)
383
+ unless $token.nil?
384
+ begin
385
+ docs = RestClient.post(
386
+ "#{$server}/api/list_client_docs",
387
+ {
388
+ :api_token => $token,
389
+ :source_id => source_id,
390
+ :client_id => client_id
391
+ }.to_json,
392
+ :content_type => :json
393
+ ).body
394
+ JSON.parse(docs)
395
+ rescue=>e
396
+ cant_connect_rhosync(e)
397
+ end
398
+ else
399
+ access_denied
400
+ end
401
+ end
402
+
403
+ #NEW METHODS IN THE GEM
404
+ #Return content of a given document
405
+ def get_db_doc_by_type(user_id,source_id,type_doc)
406
+ unless $token.nil?
407
+ begin
408
+ docs = list_source_docs(user_id,source_id)
409
+ res = RestClient.post(
410
+ "#{$server}/api/get_db_doc",
411
+ {
412
+ :api_token => $token,
413
+ :doc => docs["#{type_doc}"],
414
+ :data_type => nil
415
+ }.to_json,
416
+ :content_type => :json
417
+ ).body
418
+ JSON.parse(res)
419
+ rescue=>e
420
+ cant_connect_rhosync(e)
421
+ end
422
+ else
423
+ access_denied
424
+ end
425
+ end
426
+
427
+ #set content of a given document
428
+ def set_db_doc_by_type(user_id,source_id,data,type_doc)
429
+ unless $token.nil?
430
+ begin
431
+ docs = list_source_docs(user_id,source_id)
432
+ set_db_doc(docs["#{type_doc}"],data)
433
+ rescue=>e
434
+ cant_connect_rhosync(e)
435
+ end
436
+ else
437
+ access_denied
438
+ end
439
+ end
440
+
441
+ def access_denied
442
+ puts "you don't have access, please login in"
443
+ nil
444
+ end
445
+
446
+ def cant_connect_rhosync(e)
447
+ puts "rhosync error : #{e.inspect}"
448
+ nil
449
+ end
450
+
451
+
452
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhosync_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-08-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &21681648 !ruby/object:Gem::Requirement
16
+ requirement: &22531452 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *21681648
24
+ version_requirements: *22531452
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &21681360 !ruby/object:Gem::Requirement
27
+ requirement: &22531164 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 1.4.2
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *21681360
35
+ version_requirements: *22531164
36
36
  description: ''
37
37
  email:
38
38
  - rmantilla26@hotmail.com
@@ -42,6 +42,8 @@ extra_rdoc_files:
42
42
  - History.txt
43
43
  files:
44
44
  - History.txt
45
+ - LICENSE.txt
46
+ - lib/rhosync_api.rb
45
47
  homepage:
46
48
  licenses: []
47
49
  post_install_message: