rhosync_api 0.0.6 → 0.1.3

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