rhosync_api 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.
Files changed (4) hide show
  1. data/History.txt +2 -0
  2. data/LICENSE.txt +4 -0
  3. data/lib/rhosync_api.rb +264 -0
  4. metadata +100 -0
data/History.txt ADDED
@@ -0,0 +1,2 @@
1
+
2
+ version 0.0.1 - 5 August 2011
data/LICENSE.txt ADDED
@@ -0,0 +1,4 @@
1
+ Copyright (c) 2011 Raul Mantilla Assia
2
+
3
+ This gem is free, every one can use it.
4
+
@@ -0,0 +1,264 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+
4
+ class RhosyncApi
5
+
6
+ def initialize
7
+ $server = ""
8
+ $username = ""
9
+ $password = ""
10
+ $token = ""
11
+ end
12
+
13
+ #login -- rhosync
14
+ def login(server,admin,password)
15
+ $server = server
16
+ $username = admin
17
+ $password = password
18
+ $token = get_api_token
19
+ end
20
+
21
+ #Before you can use RhoSync API you should get API token:
22
+ def get_api_token
23
+ uri = URI.parse($server)
24
+ http = Net::HTTP.new(uri.host,uri.port)
25
+ begin
26
+ res,data = http.post( '/login',
27
+ {:login => $username, :password => $password}.to_json,
28
+ {'Content-Type' => 'application/json'} )
29
+ cookie = res.response['set-cookie'].split('; ')[0].split('=')
30
+ cookie = {cookie[0] => URI.escape(cookie[1])}
31
+ token = RestClient.post("#{$server}/api/get_api_token",'',{:cookies => cookie})
32
+ rescue=>e
33
+ cant_connect_rhosync(e)
34
+ end
35
+ end
36
+
37
+ #Returns license information of the currently used license
38
+ def get_license_info
39
+ unless $token.nil?
40
+ begin
41
+ license_info = RestClient.post(
42
+ "#{$server}/api/get_license_info",
43
+ {:api_token => $token}.to_json, :content_type => :json
44
+ ).body
45
+ JSON.parse(license_info)
46
+ rescue=>e
47
+ cant_connect_rhosync(e)
48
+ end
49
+ else
50
+ access_denied
51
+ end
52
+ end
53
+
54
+ #Reset the server: flush db and re-bootstrap server
55
+ def reset
56
+ unless $token.nil?
57
+ begin
58
+ RestClient.post("#{$server}/api/reset",
59
+ { :api_token => $token }.to_json,
60
+ :content_type => :json
61
+ )
62
+ rescue=>e
63
+ cant_connect_rhosync(e)
64
+ end
65
+ else
66
+ access_denied
67
+ end
68
+ end
69
+
70
+ #List users registered with this RhoSync application.
71
+ def list_users
72
+ unless $token.nil?
73
+ begin
74
+ users = RestClient.post(
75
+ "#{$server}/api/list_users",
76
+ { :api_token => $token }.to_json,
77
+ :content_type => :json
78
+ ).body
79
+ JSON.parse(users)
80
+ rescue=>e
81
+ cant_connect_rhosync(e)
82
+ end
83
+ else
84
+ access_denied
85
+ end
86
+ end
87
+
88
+ #Create a user in this RhoSync application.
89
+ def create_user(login,password)
90
+ unless $token.nil?
91
+ unless login.nil?
92
+ begin
93
+ RestClient.post("#{$server}/api/create_user",
94
+ {
95
+ :api_token => $token,
96
+ :attributes => {
97
+ :login => login,
98
+ :password => password
99
+ }
100
+ }.to_json,
101
+ :content_type => :json
102
+ )
103
+ rescue=>e
104
+ cant_connect_rhosync(e)
105
+ end
106
+ else
107
+ puts "the user's ID can't be null "
108
+ nil
109
+ end
110
+ else
111
+ access_denied
112
+ end
113
+ end
114
+
115
+ #Delete User and all associated devices from the RhoSync application.
116
+ def delete_user(user_id)
117
+ unless $token.nil?
118
+ unless user_id.nil?
119
+ begin
120
+ RestClient.post(
121
+ "#{$server}/api/delete_user",
122
+ {
123
+ :api_token => $token,
124
+ :user_id => user_id
125
+ }.to_json,
126
+ :content_type => :json
127
+ )
128
+ rescue=>e
129
+ cant_connect_rhosync(e)
130
+ end
131
+ else
132
+ puts "the user's ID can't be null "
133
+ nil
134
+ end
135
+ else
136
+ access_denied
137
+ end
138
+ end
139
+
140
+ #List clients (devices) associated with given user.
141
+ def list_clients(user_id)
142
+ unless $token.nil?
143
+ begin
144
+ clients = RestClient.post("#{$server}/api/list_clients",
145
+ {
146
+ :api_token => $token,
147
+ :user_id => user_id
148
+ }.to_json,
149
+ :content_type => :json
150
+ ).body
151
+ JSON.parse(clients)
152
+ rescue=>e
153
+ cant_connect_rhosync(e)
154
+ end
155
+ else
156
+ access_denied
157
+ end
158
+ end
159
+
160
+ #Creates a client (device) for a given user.
161
+ def create_client(user_id)
162
+ unless $token.nil?
163
+ unless user_id.nil?
164
+ begin
165
+ RestClient.post(
166
+ "#{$server}/api/create_client",
167
+ {
168
+ :api_token => $token,
169
+ :user_id => user_id
170
+ }.to_json,
171
+ :content_type => :json
172
+ ).body
173
+ rescue=>e
174
+ cant_connect_rhosync(e)
175
+ end
176
+ else
177
+ puts "the user's ID can't be null "
178
+ nil
179
+ end
180
+ else
181
+ access_denied
182
+ end
183
+ end
184
+
185
+ #Deletes the specified client (device).
186
+ def delete_client(user_id,client_id)
187
+ unless $token.nil?
188
+ unless user_id.nil? and client_id.nil?
189
+ begin
190
+ RestClient.post(
191
+ "#{$server}/api/delete_client",
192
+ {
193
+ :api_token => $token,
194
+ :user_id => user_id,
195
+ :client_id => client_id
196
+ }.to_json,
197
+ :content_type => :json
198
+ )
199
+ rescue=>e
200
+ cant_connect_rhosync(e)
201
+ end
202
+ else
203
+ puts "the user's ID and client's ID can't be null "
204
+ nil
205
+ end
206
+ else
207
+ access_denied
208
+ end
209
+ end
210
+
211
+ #Returns client (device) attributes, such as device_type, device_pin, device_port. These attributes used by RhoSync push.
212
+ def get_client_params(client_id)
213
+ unless $token.nil?
214
+ begin
215
+ client_params = RestClient.post(
216
+ "#{$server}/api/get_client_params",
217
+ {
218
+ :api_token => $token,
219
+ :client_id => client_id
220
+ }.to_json,
221
+ :content_type => :json
222
+ ).body
223
+ JSON.parse(client_params)
224
+ rescue=>e
225
+ cant_connect_rhosync(e)
226
+ end
227
+ else
228
+ access_denied
229
+ end
230
+ end
231
+
232
+ #Return list of source adapters for this RhoSync application.
233
+ def list_sources(partition = nil)
234
+ unless $token.nil?
235
+ partition = "user" if partition.nil?
236
+ begin
237
+ sources = RestClient.post("#{$server}/api/list_sources",
238
+ {
239
+ :api_token => $token,
240
+ :partition_type => partition
241
+ }.to_json,
242
+ :content_type => :json
243
+ ).body
244
+ JSON.parse(sources)
245
+ rescue=>e
246
+ cant_connect_rhosync(e)
247
+ end
248
+ else
249
+ access_denied
250
+ end
251
+ end
252
+
253
+ def access_denied
254
+ puts "you don't have access, please login in"
255
+ nil
256
+ end
257
+
258
+ def cant_connect_rhosync(e)
259
+ puts "rhosync error : #{e.inspect}"
260
+ nil
261
+ end
262
+
263
+
264
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rhosync_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Raul Mantilla
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: newgem
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 1
31
+ - 5
32
+ - 2
33
+ version: 1.5.2
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: hoe
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 27
45
+ segments:
46
+ - 2
47
+ - 5
48
+ - 0
49
+ version: 2.5.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: ""
53
+ email:
54
+ - rmantilla26@hotmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - History.txt
61
+ files:
62
+ - History.txt
63
+ - LICENSE.txt
64
+ - lib/rhosync_api.rb
65
+ homepage:
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --main
71
+ - README.txt
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project: rhosync_api
95
+ rubygems_version: 1.8.6
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: RhosyncApi
99
+ test_files: []
100
+