Class: RhosyncApi

Inherits:
Object
  • Object
show all
Defined in:
lib/rhosync_api.rb

Instance Method Summary collapse

Constructor Details

#initializeRhosyncApi

Returns a new instance of RhosyncApi.



6
7
8
9
10
11
# File 'lib/rhosync_api.rb', line 6

def initialize
  $server = ""
  $username = ""
  $password = ""
  $token = ""
end

Instance Method Details

#access_deniedObject



253
254
255
256
# File 'lib/rhosync_api.rb', line 253

def access_denied
  puts "you don't have access, please login in"
  nil
end

#cant_connect_rhosync(e) ⇒ Object



258
259
260
261
# File 'lib/rhosync_api.rb', line 258

def cant_connect_rhosync(e)
  puts "rhosync error : #{e.inspect}"
  nil
end

#create_client(user_id) ⇒ Object

Creates a client (device) for a given user.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rhosync_api.rb', line 161

def create_client(user_id)
  unless $token.nil?
    unless user_id.nil?
      begin
        RestClient.post(
          "#{$server}/api/create_client",
          {
            :api_token => $token,
            :user_id => user_id
          }.to_json,
          :content_type => :json
        ).body
      rescue=>e
        cant_connect_rhosync(e)
      end
    else
      puts "the user's ID can't be null "
      nil
    end
  else
    access_denied
  end
end

#create_user(login, password) ⇒ Object

Create a user in this RhoSync application.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rhosync_api.rb', line 89

def create_user(,password)
  unless $token.nil?
    unless .nil?
      begin
        RestClient.post("#{$server}/api/create_user",
          {
            :api_token => $token,
            :attributes => {
              :login => ,
              :password => password
            }
          }.to_json,
          :content_type => :json
        )
      rescue=>e
        cant_connect_rhosync(e)
      end
    else
      puts "the user's ID can't be null "
      nil
    end
  else
    access_denied
  end
end

#delete_client(user_id, client_id) ⇒ Object

Deletes the specified client (device).



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rhosync_api.rb', line 186

def delete_client(user_id,client_id)
  unless $token.nil?
    unless user_id.nil? and client_id.nil?
      begin
        RestClient.post(
          "#{$server}/api/delete_client",
          {
            :api_token => $token,
            :user_id => user_id,
            :client_id => client_id
          }.to_json,
          :content_type => :json
        )   
      rescue=>e
        cant_connect_rhosync(e)
      end
    else
      puts "the user's ID  and client's ID can't be null "
      nil
    end
  else
    access_denied
  end
end

#delete_user(user_id) ⇒ Object

Delete User and all associated devices from the RhoSync application.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rhosync_api.rb', line 116

def delete_user(user_id)
  unless $token.nil?
    unless user_id.nil?
      begin
        RestClient.post(
          "#{$server}/api/delete_user",
          {
            :api_token => $token,
            :user_id => user_id
          }.to_json,
          :content_type => :json
        )
      rescue=>e
        cant_connect_rhosync(e)
      end
    else
      puts "the user's ID can't be null "
      nil
    end
  else
    access_denied
  end
end

#get_api_tokenObject

Before you can use RhoSync API you should get API token:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rhosync_api.rb', line 22

def get_api_token
  uri = URI.parse($server)
  http = Net::HTTP.new(uri.host,uri.port)
  begin
    res,data = http.post( '/login',
      {:login => $username, :password => $password}.to_json,
      {'Content-Type' => 'application/json'} )
    cookie = res.response['set-cookie'].split('; ')[0].split('=')
    cookie = {cookie[0] => URI.escape(cookie[1])}
    token = RestClient.post("#{$server}/api/get_api_token",'',{:cookies => cookie})
  rescue=>e
    cant_connect_rhosync(e)
  end
end

#get_client_params(client_id) ⇒ Object

Returns client (device) attributes, such as device_type, device_pin, device_port. These attributes used by RhoSync push.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rhosync_api.rb', line 212

def get_client_params(client_id)
  unless $token.nil?
    begin
      client_params = RestClient.post(
        "#{$server}/api/get_client_params",
        {
          :api_token => $token,
          :client_id => client_id
        }.to_json,
        :content_type => :json
      ).body
      JSON.parse(client_params)
    rescue=>e
      cant_connect_rhosync(e)
    end
  else
    access_denied
  end
end

#get_license_infoObject

Returns license information of the currently used license



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rhosync_api.rb', line 38

def get_license_info
  unless $token.nil?
    begin
      license_info = RestClient.post(
        "#{$server}/api/get_license_info",
        {:api_token => $token}.to_json, :content_type => :json
      ).body
      JSON.parse(license_info)
    rescue=>e
      cant_connect_rhosync(e)
    end
  else
    access_denied
  end
end

#list_clients(user_id) ⇒ Object

List clients (devices) associated with given user.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rhosync_api.rb', line 141

def list_clients(user_id)
  unless $token.nil?
    begin
      clients = RestClient.post("#{$server}/api/list_clients",
        {
          :api_token => $token,
          :user_id => user_id
        }.to_json,
        :content_type => :json
      ).body
      JSON.parse(clients)
    rescue=>e
      cant_connect_rhosync(e)
    end
  else
    access_denied
  end
end

#list_sources(partition = nil) ⇒ Object

Return list of source adapters for this RhoSync application.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rhosync_api.rb', line 233

def list_sources(partition = nil)
  unless $token.nil?
    partition = "user" if partition.nil?
    begin
      sources = RestClient.post("#{$server}/api/list_sources",
        {
          :api_token => $token,
          :partition_type => partition
        }.to_json,
        :content_type => :json
      ).body
      JSON.parse(sources)
    rescue=>e
      cant_connect_rhosync(e)
    end
  else
    access_denied
  end
end

#list_usersObject

List users registered with this RhoSync application.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rhosync_api.rb', line 71

def list_users
  unless $token.nil?
    begin
      users = RestClient.post(
        "#{$server}/api/list_users",
        { :api_token => $token }.to_json,
        :content_type => :json
      ).body
      JSON.parse(users)
    rescue=>e
      cant_connect_rhosync(e)
    end
  else
    access_denied
  end
end

#login(server, admin, password) ⇒ Object

login – rhosync



14
15
16
17
18
19
# File 'lib/rhosync_api.rb', line 14

def (server,admin,password)
  $server = server
  $username = admin
  $password = password
  $token = get_api_token
end

#resetObject

Reset the server: flush db and re-bootstrap server



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rhosync_api.rb', line 55

def reset
  unless $token.nil?
    begin
      RestClient.post("#{$server}/api/reset",
        { :api_token => $token }.to_json,
        :content_type => :json
      )
    rescue=>e
      cant_connect_rhosync(e)
    end
  else
    access_denied
  end
end