quickblox 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/config.yml.example +6 -0
  2. data/quickblox.rb +895 -0
  3. metadata +46 -0
@@ -0,0 +1,6 @@
1
+ config:
2
+ application_id: 22
3
+ auth_key: LNFYqsasda43dVPJ
4
+ auth_secret: WyXEsdfdLWAe-q
5
+ server: api.quickblox.com
6
+ user_owner_id: 33
@@ -0,0 +1,895 @@
1
+ require 'net/http'
2
+ require 'hmac-sha1'
3
+ require 'json'
4
+ require 'rest_client'
5
+ require 'yaml'
6
+ require "uri"
7
+ require "net/http/post/multipart"
8
+ require 'base64'
9
+
10
+
11
+ class Quickblox
12
+
13
+ def configs
14
+ config = YAML.load_file("config.yml")
15
+ @application_id = config["config"]["application_id"]
16
+ @auth_key = config["config"]["auth_key"]
17
+ @auth_secret = config["config"]["auth_secret"]
18
+ @user_owner_id = config["config"]["user_owner_id"]
19
+ @server=config["config"]["server"]
20
+ #to remove - for debug
21
+ @user_login=config["config"]["user_login"]
22
+ @user_password=config["config"]["user_password"]
23
+ @device_platform= config["config"]["device_platform"]
24
+ @device_udid= config["config"]["device_udid"]
25
+
26
+ end
27
+
28
+
29
+ def initialize
30
+ configs
31
+ @auth_uri=URI("http://"+@server.to_s+'/auth.json')
32
+ @users_uri=URI("http://"+@server.to_s+'/users')
33
+ @geodata_uri=URI("http://"+@server.to_s+'/geodata')
34
+ @places_uri=URI("http://"+@server.to_s+'/places')
35
+ @files_uri=URI("http://"+@server.to_s+'/blobs')
36
+ @pushtokens_uri=URI("http://"+@server.to_s+'/push_tokens')
37
+ @gamemodes_uri=URI("http://"+@server.to_s+'/gamemodes')
38
+ @token=nil
39
+ @token_type=nil
40
+ @users_count = nil
41
+ @user_id = nil
42
+
43
+ end
44
+
45
+ def user_login=(value)
46
+ @user_login=value
47
+ end
48
+
49
+ def user_login
50
+ @user_login
51
+ end
52
+
53
+ def user_password=(value)
54
+ @user_password=value
55
+ end
56
+
57
+ def user_password
58
+ @user_password
59
+ end
60
+
61
+ def device_platform=(value)
62
+ @device_platform=value
63
+ end
64
+
65
+ def device_udid
66
+ @device_udid
67
+ end
68
+
69
+ #Generates token depending on a request. Application token is generated by default.
70
+ def get_token(type = 'app')
71
+ destroy_token if @token
72
+ timestamp=Time.now.to_i
73
+ nonce=rand(10000)
74
+
75
+ hash = {:application_id => @application_id, :nonce => nonce, :auth_key => @auth_key, :timestamp => timestamp}
76
+ hash.merge!({:user => {:login => @user_login, :password => @user_password, :owner_id => @user_owner_id}}) if type == 'user' || type == 'user_device'
77
+ hash.merge!({:device => {:platform => @device_platform, :udid => @device_udid}}) if type == 'device' || type == 'user_device'
78
+ normalized= normalize(hash)
79
+ signature = HMAC::SHA1.hexdigest(@auth_secret, normalized)
80
+ req = Net::HTTP::Post.new(@auth_uri.path)
81
+ req.body = "#{normalized}&signature=#{signature}"
82
+ ask_token = Net::HTTP.start(@auth_uri.host, @auth_uri.port) do |http|
83
+ http.request(req)
84
+ end
85
+
86
+ @token_type=type
87
+ @user_id=JSON.parse(ask_token.body)["session"]["user_id"]
88
+ @token=JSON.parse(ask_token.body)["session"]["token"]
89
+ end
90
+
91
+ #Destroys existing token
92
+ def destroy_token
93
+ http = Net::HTTP.new(@server)
94
+ delete_token = Net::HTTP::Delete.new("/auth_exit?token=#{@token}")
95
+ http.request(delete_token)
96
+ end
97
+
98
+ #Users API Requests
99
+
100
+ #Retrieve users count of the application
101
+ def get_users_count(at_uri=nil, at_param=nil)
102
+ @token = get_token unless @token
103
+ user_list = Net::HTTP.get_response(URI(@users_uri.to_s+"#{at_uri}.json")+"?token=#{@token}#{at_param}")
104
+ JSON.parse(user_list.body)["total_entries"]
105
+ end
106
+
107
+ #Retrieve all users of the application
108
+ def get_all_users_list
109
+ @token = get_token unless @token
110
+ @users_count = get_users_count unless @users_count
111
+ requests=(@users_count/100).to_i+1
112
+ users=[]
113
+ i=1
114
+ while i <= requests do
115
+ user_list=Net::HTTP.get_response(URI(@users_uri.to_s+".json")+"?token=#{@token}&per_page=100&page=#{i}")
116
+ i+=1
117
+ users.concat JSON.parse(user_list.body)["items"]
118
+ end
119
+ users
120
+ end
121
+
122
+ #Retrive users of the application with pagination
123
+ def get_users_list (page=nil, per_page=nil)
124
+ @token = get_token unless @token
125
+ page=1 unless page
126
+ per_page=10 unless per_page
127
+ user_list=Net::HTTP.get_response(URI(@users_uri.to_s+".json")+"?token=#{@token}&per_page=#{per_page}&page=#{page}")
128
+ JSON.parse(user_list.body)["items"]
129
+ end
130
+
131
+ #Create a new user
132
+ def signup_user(user_params)
133
+ @token = get_token unless @token_type=='app'
134
+ normalized= normalize(user_params)
135
+ req = Net::HTTP::Post.new(@users_uri.path)
136
+ req.body = "#{normalized}&token=#{@token}"
137
+ Net::HTTP.start(@users_uri.host) do |http|
138
+ http.request(req)
139
+ end
140
+
141
+ end
142
+
143
+ #Get usser info by ID
144
+ def get_user_by_id(id)
145
+ @token = get_token unless @token
146
+ get_users_by_id = Net::HTTP.get_response(URI(@users_uri.to_s+"/#{id}.json")+"?token=#{@token}")
147
+ JSON.parse(get_users_by_id.body)
148
+ end
149
+
150
+ #Get user info by login
151
+ def get_user_by_login(login)
152
+ @token = get_token unless @token
153
+ get_user_by_login= Net::HTTP.get_response(URI(@users_uri.to_s+"/by_login.json")+"?token=#{@token}&login=#{login}")
154
+ JSON.parse(get_user_by_login.body)
155
+ end
156
+
157
+ #Get all users by fullname
158
+ def get_all_users_by_fullname(fullname)
159
+ en_fullname = URI::encode(fullname)
160
+ @token = get_token unless @token
161
+ get_fullname_count= get_users_count("/by_full_name", "&full_name=#{en_fullname}")
162
+ requests=(get_fullname_count/100).to_i+1
163
+ users=[]
164
+ i=1
165
+ while i <= requests do
166
+ user_list=Net::HTTP.get_response(URI(@users_uri.to_s+"/by_full_name.json")+"?token=#{@token}&per_page=100&page=#{i}&full_name=#{en_fullname}")
167
+ i+=1
168
+ users.concat JSON.parse(user_list.body)["items"]
169
+ end
170
+ users
171
+ end
172
+
173
+ #Get users by full name with pagination
174
+ def get_users_by_fullname(fullname, page=nil, per_page=nil)
175
+ en_fullname = URI::encode(fullname)
176
+ page=1 unless page
177
+ per_page=10 unless per_page
178
+ @token = get_token unless @token
179
+ user_list=Net::HTTP.get_response(URI(@users_uri.to_s+"/by_full_name.json")+"?token=#{@token}&per_page=#{per_page}&page=#{page}&full_name=#{en_fullname}")
180
+ JSON.parse(user_list.body)["items"]
181
+ end
182
+
183
+ #Get user by facebook ID
184
+ def get_user_by_facebook_id(fbid)
185
+ @token = get_token unless @token
186
+ get_user_by_fb= Net::HTTP.get_response(URI(@users_uri.to_s+"/by_facebook_id.json")+"?token=#{@token}&facebook_id=#{fbid}")
187
+ JSON.parse(get_user_by_fb.body)
188
+ end
189
+
190
+ #Get user by twitter ID
191
+ def get_user_by_twitter_id(twid)
192
+ @token = get_token unless @token
193
+ get_user_by_tw= Net::HTTP.get_response(URI(@users_uri.to_s+"/by_twitter_id.json")+"?token=#{@token}&twitter_id=#{twid}")
194
+ JSON.parse(get_user_by_tw.body)
195
+ end
196
+
197
+ #Get user by email
198
+ def get_user_by_email(email)
199
+ @token = get_token unless @token
200
+ get_user_by_email= Net::HTTP.get_response(URI(@users_uri.to_s+"/by_email.json")+"?token=#{@token}&email=#{email}")
201
+ JSON.parse(get_user_by_email.body)
202
+ end
203
+
204
+ #Get all users by tags
205
+ def get_all_users_by_tags(tags)
206
+ en_tags = URI::encode(tags)
207
+ @token = get_token unless @token
208
+ get_tags_count= get_users_count("/by_tags", "&tags=#{en_tags}")
209
+ requests=(get_tags_count/100).to_i+1
210
+ users=[]
211
+ i=1
212
+ while i <= requests do
213
+ user_list=Net::HTTP.get_response(URI(@users_uri.to_s+"/by_tags.json")+"?token=#{@token}&per_page=100&page=#{i}&tags=#{en_tags}")
214
+ i+=1
215
+ users.concat JSON.parse(user_list.body)["items"]
216
+ end
217
+ users
218
+ end
219
+
220
+ #Get users by tags with pagination
221
+ def get_users_by_tags(tags, page=nil, per_page=nil)
222
+ en_tags = URI::encode(tags)
223
+ @token = get_token unless @token
224
+ page=1 unless page
225
+ per_page=10 unless per_page
226
+ user_list=Net::HTTP.get_response(URI(@users_uri.to_s+"/by_tags.json")+"?token=#{@token}&per_page=#{per_page}&page=#{page}&tags=#{en_tags}")
227
+ JSON.parse(user_list.body)["items"]
228
+ end
229
+
230
+ #Update user by ID
231
+ def update_user_by_id(user_params)
232
+ @token = get_token("user") unless @token_type=='user'
233
+ user_params.merge! "token" => @token
234
+ normalized= normalize(user_params)
235
+ req = Net::HTTP::Put.new(URI(@users_uri.to_s+"/"+@user_id.to_s).path)
236
+ req.body = "#{normalized}"
237
+ Net::HTTP.start(@users_uri.host) do |http|
238
+ http.request(req)
239
+ end
240
+
241
+ end
242
+
243
+ #Delete user
244
+ def delete_user
245
+ @token = get_token("user") unless @token_type=='user'
246
+ http = Net::HTTP.new(@server)
247
+ delete_user = Net::HTTP::Delete.new("/users/#{@user_id}?token=#{@token}")
248
+ http.request(delete_user)
249
+ end
250
+
251
+ #Get user info by external ID
252
+ def get_user_by_external_id (external_id)
253
+ @token = get_token unless @token
254
+ get_user_by_ext_id= Net::HTTP.get_response(URI(@users_uri.to_s+"/external/#{external_id}.json")+"?token=#{@token}")
255
+ JSON.parse(get_user_by_ext_id.body)
256
+ end
257
+
258
+ #Get external ID by user ID
259
+ def get_external_id_by_user_id(id)
260
+ get_user_by_id(id)["external_user_id"]
261
+ end
262
+
263
+ #Delete User by external ID
264
+ def delete_user_by_external_id
265
+ @token = get_token("user") unless @token_type=='user'
266
+ external_id = get_external_id_by_user_id(@user_id)
267
+ http = Net::HTTP.new(@server)
268
+ delete_user = Net::HTTP::Delete.new("/users/external/#{external_id}?token=#{@token}")
269
+ http.request(delete_user)
270
+ end
271
+
272
+ #Location API Requests
273
+
274
+ #Create Geodatum
275
+ def create_geodatum(geodata_params)
276
+ @token = get_token("user") unless @token_type=='user'
277
+ geodata_params.merge! "token" => @token
278
+ normalized= normalize(geodata_params)
279
+ req = Net::HTTP::Post.new(@geodata_uri.path)
280
+ req.body = "#{normalized}"
281
+ Net::HTTP.start(@geodata_uri.host) do |http|
282
+ http.request(req)
283
+ end
284
+ end
285
+
286
+ #Get geodata by ID
287
+ def get_geodata_by_id (geodata_id)
288
+ @token = get_token unless @token
289
+ get_geodata_by_id= Net::HTTP.get_response(URI(@geodata_uri.to_s+"/#{geodata_id}.json")+"?token=#{@token}")
290
+ JSON.parse(get_geodata_by_id.body)["geo_data"]
291
+ end
292
+
293
+ def get_geodata(geodata)
294
+ @token = get_token unless @token
295
+ normalized= normalize(geodata)
296
+ get_geodata_by_id= Net::HTTP.get_response(URI(@geodata_uri.to_s+"/find.json")+"?#{normalized}&token=#{@token}")
297
+ JSON.parse(get_geodata_by_id.body)
298
+ end
299
+
300
+ def delete_geodata(days)
301
+ @token = get_token("user") unless @token_type=='user'
302
+ http = Net::HTTP.new(@server)
303
+ delete_geodata = Net::HTTP::Delete.new("/geodata?token=#{@token}&days=#{days}")
304
+ http.request(delete_geodata)
305
+ end
306
+
307
+ def create_place(place_params)
308
+ @token = get_token("user") unless @token_type=='user'
309
+ place_params.merge! "token" => @token
310
+ normalized= normalize(place_params)
311
+ req = Net::HTTP::Post.new(@places_uri.path)
312
+ req.body = "#{normalized}"
313
+ Net::HTTP.start(@places_uri.host) do |http|
314
+ http.request(req)
315
+ end
316
+ end
317
+
318
+ def get_places(page=1, per_page=10)
319
+ @token = get_token unless @token
320
+ places_list=Net::HTTP.get_response(URI(@places_uri.to_s+".json")+"?token=#{@token}&per_page=#{per_page}&page=#{page}")
321
+ JSON.parse(places_list.body)["items"]
322
+ end
323
+
324
+ def get_places_by_id(id)
325
+ @token = get_token unless @token
326
+ places_list=Net::HTTP.get_response(URI(@places_uri.to_s+"/#{id}.json")+"?token=#{@token}")
327
+
328
+ JSON.parse(places_list.body)
329
+ end
330
+
331
+ def update_place_by_id(id, place_params)
332
+ @token = get_token("user") unless @token_type=='user'
333
+ place_params.merge! "token" => @token
334
+ normalized= normalize(lace_params)
335
+ req = Net::HTTP::Put.new(URI(@places_uri.to_s+"/#{id}").path)
336
+ req.body = "#{normalized}"
337
+ Net::HTTP.start(@places_uri.host) do |http|
338
+ http.request(req)
339
+ end
340
+ end
341
+
342
+ def delete_place_by_id(id)
343
+ @token = get_token("user") unless @token_type=='user'
344
+ http = Net::HTTP.new(@server)
345
+ delete_place = Net::HTTP::Delete.new("/places/#{id}?token=#{@token}")
346
+ http.request(delete_place)
347
+ end
348
+
349
+
350
+ #Content API Requests
351
+
352
+
353
+ def create_file(filename, file_params)
354
+ @token = get_token("user") unless @token_type=='user'
355
+ file_params.merge! "token" => @token, "blob[multipart]" => 0
356
+ normalized = normalize(file_params)
357
+ req = Net::HTTP::Post.new(URI(@files_uri.to_s+".json").path)
358
+ req.body = "#{normalized}"
359
+ create_file = Net::HTTP.start(URI(@files_uri.to_s+".json").host) do |http|
360
+ http.request(req)
361
+ end
362
+ link=JSON.parse(create_file.body)["blob"]["blob_object_access"]["params"]
363
+ uri=URI(link)
364
+ query = {}
365
+ CGI.parse(uri.query).map { |k, v| query.merge!({k => CGI.unescape(v[0])}) }
366
+ query.merge! "file" => File.read(filename)
367
+ req = Net::HTTP::Post::Multipart.new uri.path, query
368
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
369
+ http.request(req)
370
+ end
371
+ blobid = JSON.parse(create_file.body)["blob"]["id"]
372
+ filesize = File.size(filename)
373
+ req = Net::HTTP::Put.new(URI(@files_uri.to_s+"/#{blobid}/complete.json").path)
374
+ req.body = "blob[size]=#{filesize}&token=#{@token}"
375
+ p=Net::HTTP.start(@places_uri.host) do |http|
376
+ http.request(req)
377
+ end
378
+ JSON.parse(create_file.body)["blob"]
379
+ end
380
+
381
+ def get_file_info_by_id(id)
382
+ @token = get_token unless @token
383
+ file_info = Net::HTTP.get_response(URI(@files_uri.to_s+"/#{id}.json")+"?token=#{@token}")
384
+ JSON.parse(file_info.body)["blob"]
385
+ end
386
+
387
+ def get_file_link_by_uid(id)
388
+ @token = get_token unless @token
389
+ file_info = Net::HTTP.get_response(URI(@files_uri.to_s+"/#{id}"+"?token=#{@token}"))
390
+ CGI::unescapeHTML(file_info.body.gsub("<html><body>You are being <a href=\"", '').gsub("\">redirected</a>.</body></html>", ''))
391
+ end
392
+
393
+ def get_file_link_by_id(id)
394
+ @token = get_token unless @token
395
+ req = Net::HTTP::Post.new(URI(@files_uri.to_s+"/#{id}/getblobobjectbyid.json").path)
396
+ req.body = "token=#{@token}"
397
+ request=Net::HTTP.start(@files_uri.host) do |http|
398
+ http.request(req)
399
+ end
400
+ JSON.parse(request.body)["blob_object_access"]["params"]
401
+ end
402
+
403
+ def edit_file_by_id(id, params)
404
+ @token = get_token("user") unless @token_type=='user'
405
+ req = Net::HTTP::Put.new(URI(@files_uri.to_s+"/#{id}.json").path)
406
+ params.merge! "token" => @token
407
+ normalized= normalize(params)
408
+ req.body = "#{normalized}"
409
+ request=Net::HTTP.start(@files_uri.host) do |http|
410
+ http.request(req)
411
+ end
412
+ end
413
+
414
+ def delete_file(id)
415
+ @token = get_token("user") unless @token_type=='user'
416
+ http = Net::HTTP.new(@server)
417
+ delete_geodata = Net::HTTP::Delete.new("/blobs/#{id}/?token=#{@token}")
418
+ http.request(delete_geodata)
419
+ end
420
+
421
+ def increase_file_links(id)
422
+ @token = get_token("user") unless @token_type=='user'
423
+ req = Net::HTTP::Put.new(URI(@files_uri.to_s+"/#{id}/retain.json").path)
424
+ req.body = "token=#{@token}"
425
+ request=Net::HTTP.start(@files_uri.host) do |http|
426
+ http.request(req)
427
+ end
428
+ end
429
+
430
+
431
+ #Messages API request
432
+
433
+ def create_push_token(params)
434
+ @token = get_token("user_device") unless @token_type=='user_device'
435
+ params.merge! "token" => @token
436
+ normalized= normalize(params)
437
+ req = Net::HTTP::Post.new(URI(@pushtokens_uri.to_s+".json").path)
438
+ req.body = "#{normalized}"
439
+ create_token=Net::HTTP.start(@pushtokens_uri.host) do |http|
440
+ http.request(req)
441
+ end
442
+ JSON.parse(create_token.body)
443
+
444
+ end
445
+
446
+ def create_subscription (channels, url=nil)
447
+ @token = get_token("user_device") unless @token_type=='user_device'
448
+ hash={:notification_channels => "#{channels}", :url => "#{url}"}
449
+ hash.merge! "token" => @token
450
+ normalized= normalize(hash)
451
+ req = Net::HTTP::Post.new(URI("http://"+@server.to_s+"/subscriptions.json").path)
452
+ req.body = "#{normalized}"
453
+ subscribe=Net::HTTP.start(URI("http://"+@server.to_s).host) do |http|
454
+ http.request(req)
455
+ end
456
+ JSON.parse(subscribe.body)
457
+
458
+ end
459
+
460
+ def get_subscriptions
461
+ @token = get_token("user_device") unless @token_type=='user_device'
462
+ subscriptions = Net::HTTP.get_response(URI("http://"+@server.to_s+"/subscriptions.json?token=#{@token}"))
463
+ JSON.parse(subscriptions.body)
464
+ end
465
+
466
+ def delete_subscription(id)
467
+ @token = get_token("user_device") unless @token_type=='user_device'
468
+ http = Net::HTTP.new(@server)
469
+ delete_subscription = Net::HTTP::Delete.new("/subscriptions/#{id}/?token=#{@token}")
470
+ http.request(delete_subscription)
471
+ end
472
+
473
+ def create_event(params, message)
474
+ @token = get_token("user_device") unless @token_type=='user_device'
475
+ # message = hash
476
+ # message["platform"] could be mpns, ios or android
477
+ # if message[type] mpns
478
+ # EXAMPLE
479
+ # create_event({:event=>{:notification_type=>"push",:push_type=>"mpns",:user=>{:ids=>"616"}, :environment=>"production"}},{:type=>"toast",:body=>"message body",:head=>"message head",:return_path=>"/",:class=>2})
480
+
481
+ if params[:event][:notification_type]=="push"
482
+ if params[:event][:push_type]=="mpns"
483
+ if message[:type]=="toast"
484
+ mpns_body="<?xml version='1.0' encoding='utf-8'?>"+
485
+ "<wp:Notification xmlns:wp='WPNotification'>"+
486
+ "<wp:Toast>"+
487
+ "<wp:Text1>#{message[:head]}</wp:Text1>"+
488
+ "<wp:Text2>#{message[:body]}</wp:Text2>"+
489
+ "<wp:Param>#{message[:return_path]}</wp:Param>"+
490
+ "</wp:Toast>"+
491
+ "</wp:Notification>"
492
+
493
+ to_send=CGI::escape("mpns="+Base64.strict_encode64(mpns_body)+"&headers="+Base64.strict_encode64("Content-Type,text/xml,X-NotificationClass,#{message[:class]},X-WindowsPhone-Target,toast"))
494
+ end
495
+
496
+ # create_event({:event=>{:notification_type=>"push",:push_type=>"mpns",:user=>{:ids=>"616"}, :environment=>"production"}},{:type=>"tile",:background_image=>"Red.jpg",:count=>4,:title=>"FR TITLE",:back_background_image=>"Green.jpg", :back_content=>"312312", :back_title=>"BACK TITLE",:class=>1})
497
+
498
+ if message[:type]=="tile"
499
+ mpns_body="<?xml version='1.0' encoding='utf-8'?>" +
500
+ "<wp:Notification xmlns:wp='WPNotification'>" +
501
+ "<wp:Tile>" +
502
+ "<wp:BackgroundImage>#{message[:background_image]}></wp:BackgroundImage>" +
503
+ "<wp:Count>#{message[:count]}</wp:Count>" +
504
+ "<wp:Title>#{message[:title]}</wp:Title>" +
505
+ "<wp:BackBackgroundImage>#{message[:back_background_image]}</wp:BackBackgroundImage>"+
506
+ "<wp:BackTitle>#{message[:back_title]}</wp:BackTitle>"+
507
+ "<wp:BackContent>#{message[:back_content]}</wp:BackContent>"+
508
+ "</wp:Tile>" +
509
+ "</wp:Notification>"
510
+ puts mpns_body
511
+ to_send=CGI::escape("mpns="+Base64.strict_encode64(mpns_body)+"&headers="+Base64.strict_encode64("Content-Type,text/xml,X-NotificationClass,#{message[:class]},X-WindowsPhone-Target,token"))
512
+
513
+ end
514
+ # create_event({:event=>{:notification_type=>"push",:push_type=>"mpns",:user=>{:ids=>"616"}, :environment=>"production"}},{:type=>"raw",:body=>"<root><value1>fsdfsdf</value1></root>",:class=>3})
515
+
516
+ if message[:type]=="raw"
517
+ mpns_body="<?xml version='1.0' encoding='utf-8'?>" +
518
+ "#{message[:body]}"
519
+ to_send=CGI::escape("mpns="+Base64.strict_encode64(mpns_body)+"&headers="+Base64.strict_encode64("Content-Type,text/xml,X-NotificationClass,#{message[:class]}"))
520
+
521
+ end
522
+ end
523
+
524
+ # create_event({:event=>{:notification_type=>"push",:push_type=>"apns",:user=>{:ids=>"260"}, :environment=>"development"}},{:alert=>"lol",:badge_counter=>3, :sound=>"default"})
525
+ if params[:event][:push_type]=="apns"
526
+ to_send="payload=" + Base64.strict_encode64({:aps => {:alert => message[:alert], :badge => message[:badge_counter].to_i || 1, :sound => message[:sound] || 'default'}}.to_json).to_s
527
+ end
528
+ # create_event({:event=>{:notification_type=>"push",:push_type=>"c2dm",:user=>{:ids=>"260"}, :environment=>"development"}},{:body=>'message'})
529
+ if params[:event][:push_type]=="c2dm"
530
+ to_send="data.message=" + Base64.strict_encode64(message[:body]).to_s
531
+ end
532
+ # create_event({:event=>{:notification_type=>"push",:user=>{:ids=>"260"}, :environment=>"development"}},{:body=>'message body
533
+ if params[:event][:push_type]==nil
534
+ to_send=Base64.strict_encode64(message[:body]).to_s
535
+ end
536
+ end
537
+
538
+ # create_event({:event=>{:notification_type=>"email",:user=>{:ids=>"260"}, :environment=>"development"}},{:body=>'Message'})
539
+ # create_event({:event=>{:notification_type=>"pull",:date=>"1333974896",:end_date=>"1334060796",:user=>{:ids=>"260"}, :environment=>"development"}},{:body=>'Message'})
540
+
541
+
542
+ if params[:event][:notification_type]=="http_request" || params[:event][:notification_type]== "email" || params[:event][:notification_type]=="pull"
543
+ to_send=Base64.strict_encode64(message[:body]).to_s
544
+ end
545
+
546
+
547
+ params.merge! "token" => @token
548
+ normalized = normalize(params)
549
+ req = Net::HTTP::Post.new(URI("http://"+@server.to_s+"/events.json").path)
550
+ req.body = "#{normalized}&event[message]=#{to_send}"
551
+ subscribe=Net::HTTP.start(URI("http://"+@server.to_s).host) do |http|
552
+ http.request(req)
553
+ end
554
+
555
+ JSON.parse(subscribe.body)
556
+ end
557
+
558
+ def get_events(page=1, per_page=10)
559
+ @token = get_token("user_device") unless @token_type=='user_device'
560
+ file_info = Net::HTTP.get_response(URI("http://"+@server.to_s+"/events.json")+"?token=#{@token}&per_page=#{per_page}&page=#{page}")
561
+ JSON.parse(file_info.body)
562
+ end
563
+
564
+ def edit_event(id, params, message)
565
+ @token = get_token("user_device") unless @token_type=='user_device'
566
+ #edit_event (22,{:active=>true},{:push_type=>"mpns", :type=>"toast",:body=>"message body",:head=>"message head",:return_path=>"/",:class=>2})
567
+ if message[:push_type]=="mpns"
568
+ if message[:type]=="toast"
569
+ mpns_body="<?xml version='1.0' encoding='utf-8'?>"+
570
+ "<wp:Notification xmlns:wp='WPNotification'>"+
571
+ "<wp:Toast>"+
572
+ "<wp:Text1>#{message[:head]}</wp:Text1>"+
573
+ "<wp:Text2>#{message[:body]}</wp:Text2>"+
574
+ "<wp:Param>#{message[:return_path]}</wp:Param>"+
575
+ "</wp:Toast>"+
576
+ "</wp:Notification>"
577
+
578
+ to_send=CGI::escape("mpns="+Base64.strict_encode64(mpns_body)+"&headers="+Base64.strict_encode64("Content-Type,text/xml,X-NotificationClass,#{message[:class]},X-WindowsPhone-Target,toast"))
579
+ end
580
+
581
+ # edit_event(22,{:active=>true},{:push_type=>"mpns",:type=>"tile",:background_image=>"Red.jpg",:count=>4,:title=>"FR TITLE",:back_background_image=>"Green.jpg", :back_content=>"312312", :back_title=>"BACK TITLE",:class=>1})
582
+
583
+ if message[:type]=="tile"
584
+ mpns_body="<?xml version='1.0' encoding='utf-8'?>" +
585
+ "<wp:Notification xmlns:wp='WPNotification'>" +
586
+ "<wp:Tile>" +
587
+ "<wp:BackgroundImage>#{message[:background_image]}></wp:BackgroundImage>" +
588
+ "<wp:Count>#{message[:count]}</wp:Count>" +
589
+ "<wp:Title>#{message[:title]}</wp:Title>" +
590
+ "<wp:BackBackgroundImage>#{message[:back_background_image]}</wp:BackBackgroundImage>"+
591
+ "<wp:BackTitle>#{message[:back_title]}</wp:BackTitle>"+
592
+ "<wp:BackContent>#{message[:back_content]}</wp:BackContent>"+
593
+ "</wp:Tile>" +
594
+ "</wp:Notification>"
595
+ puts mpns_body
596
+ to_send=CGI::escape("mpns="+Base64.strict_encode64(mpns_body)+"&headers="+Base64.strict_encode64("Content-Type,text/xml,X-NotificationClass,#{message[:class]},X-WindowsPhone-Target,token"))
597
+
598
+ end
599
+ # create_event(22,{:active=>true},{:push_type=>"mpns",:type=>"raw",:body=>"<root><value1>fsdfsdf</value1></root>",:class=>3})
600
+
601
+ if message[:type]=="raw"
602
+ mpns_body="<?xml version='1.0' encoding='utf-8'?>" +
603
+ "#{message[:body]}"
604
+ to_send=CGI::escape("mpns="+Base64.strict_encode64(mpns_body)+"&headers="+Base64.strict_encode64("Content-Type,text/xml,X-NotificationClass,#{message[:class]}"))
605
+
606
+ end
607
+ end
608
+
609
+ # edit_event(640,{:event=>{:active=>true}},{:push_type=>"apns", :alert=>"lolishe", :badge_counter=>3, :sound=>"default"})
610
+
611
+ if message[:push_type]=="apns"
612
+ to_send="payload=" + Base64.strict_encode64({:aps => {:alert => message[:alert], :badge => message[:badge_counter].to_i || 1, :sound => message[:sound] || 'default'}}.to_json).to_s
613
+ end
614
+ # edit_event(640,{:event=>{:active=>true}},{:push_type=>"apns", :alert=>"lolishe", :badge_counter=>3, :sound=>"default"})
615
+ if message[:push_type]=="c2dm"
616
+ to_send="data.message=" + Base64.strict_encode64(message[:body]).to_s
617
+ end
618
+
619
+
620
+ if message[:type]=="http_request" || message[:type]== "email" || message[:type]=="pull"
621
+ to_send=Base64.strict_encode64(message[:body]).to_s
622
+ end
623
+
624
+ req = Net::HTTP::Put.new(URI("http://"+@server.to_s+"/events/#{id}.json").path)
625
+ params.merge! "token" => @token
626
+ normalized = normalize(params)
627
+ req.body = "#{normalized}&event[message]=#{to_send}"
628
+ puts req.body
629
+ Net::HTTP.start(URI("http://"+@server.to_s).host) do |http|
630
+ http.request(req)
631
+ end
632
+ end
633
+
634
+ def delete_event(id)
635
+ @token = get_token("user_device") unless @token_type=='user_device'
636
+ http = Net::HTTP.new(@server)
637
+ delete_event = Net::HTTP::Delete.new("/events/#{id}?token=#{@token}")
638
+ http.request(delete_event)
639
+ end
640
+
641
+ def get_event_by_id(id)
642
+ @token = get_token("user_device") unless @token_type=='user_device'
643
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/events/#{id}.json?token=#{@token}"))
644
+ JSON.parse(event.body)["event"]
645
+ end
646
+
647
+ def get_pull_request_list
648
+ @token = get_token("user_device") unless @token_type=='user_device'
649
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/pull_events.json?token=#{@token}"))
650
+ JSON.parse(event.body)
651
+ end
652
+
653
+ # Ratings API requests
654
+
655
+ def create_gamemode(title)
656
+ @token = get_token("user") unless @token_type=='user'
657
+ req = Net::HTTP::Post.new(URI(@gamemodes_uri.to_s+".json").path)
658
+ req.body = "token=#{@token}&gamemode[title]=#{title}"
659
+ create=Net::HTTP.start(@pushtokens_uri.host) do |http|
660
+ http.request(req)
661
+ end
662
+ JSON.parse(create.body)
663
+
664
+ end
665
+
666
+ def update_gamemode(id, title)
667
+ @token = get_token("user") unless @token_type=='user'
668
+ req = Net::HTTP::Put.new(URI(@gamemodes_uri.to_s+"/#{id}.json").path)
669
+ req.body = "token=#{@token}&gamemode[title]=#{title}"
670
+ create=Net::HTTP.start(@pushtokens_uri.host) do |http|
671
+ http.request(req)
672
+ end
673
+ JSON.parse(create.body)
674
+
675
+ end
676
+
677
+ def get_gamemode_by_id(id)
678
+ @token = get_token("user") unless @token_type=='user'
679
+ event = Net::HTTP.get_response(URI(@gamemodes_uri.to_s+"/#{id}.json?token=#{@token}"))
680
+ JSON.parse(event.body)
681
+ end
682
+
683
+ def get_gamemodes
684
+ @token = get_token("user") unless @token_type=='user'
685
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/application/gamemodes.json?token=#{@token}"))
686
+ JSON.parse(event.body)
687
+ end
688
+
689
+ def delete_gamemomde(id)
690
+ @token = get_token("user") unless @token_type=='user'
691
+ http = Net::HTTP.new(@server)
692
+ delete = Net::HTTP::Delete.new("/gamemodes/#{id}?token=#{@token}")
693
+ http.request(delete)
694
+ end
695
+
696
+ def create_score(params)
697
+ @token = get_token("user") unless @token_type=='user'
698
+ req = Net::HTTP::Post.new(URI("http://"+@server.to_s+"/scores.json").path)
699
+ params.merge! "token" => @token
700
+ normalized = normalize(params)
701
+ req.body = "#{normalized}"
702
+ create=Net::HTTP.start(URI("http://"+@server.to_s).host) do |http|
703
+ http.request(req)
704
+ end
705
+ JSON.parse(create.body)
706
+
707
+ end
708
+
709
+ # update_score(id,{:score=>{:game_mode_id=>"255",:value=>23})
710
+ def update_score(id, params)
711
+ @token = get_token("user") unless @token_type=='user'
712
+ req = Net::HTTP::Put.new(URI("http://"+@server.to_s+"/scores/#{id}.json").path)
713
+ params.merge! "token" => @token
714
+ normalized = normalize(params)
715
+ req.body = "#{normalized}"
716
+ update=Net::HTTP.start(URI("http://"+@server.to_s).host) do |http|
717
+ http.request(req)
718
+ end
719
+ JSON.parse(update.body)
720
+
721
+ end
722
+
723
+ def get_score_by_id(id)
724
+ @token = get_token("user") unless @token_type=='user'
725
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/scores/#{id}.json?token=#{@token}"))
726
+ JSON.parse(event.body)
727
+ end
728
+
729
+ def get_top_scores(id, count=10, page=1, per_page=10, filters=nil, sort=1)
730
+ @token = get_token("user") unless @token_type=='user'
731
+ event = Net::HTTP.get_response(URI(@gamemodes_uri.to_s+"/#{id}/top.#{count}.json?token=#{@token}&page=#{page}&per_page=#{per_page}&sort=#{sort}&filters=#{filters}"))
732
+ JSON.parse(event.body)
733
+ end
734
+
735
+ def get_scores_for_user(id, sort_by="value", filters=nil, sort=1)
736
+ @token = get_token("user") unless @token_type=='user'
737
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/users/#{id}/scores.json?token=#{@token}&sort=#{sort}&filters=#{filters}&sort_by=#{sort_by}"))
738
+ JSON.parse(event.body)
739
+ end
740
+
741
+ def get_average_scores(id)
742
+ @token = get_token("user") unless @token_type=='user'
743
+ event = Net::HTTP.get_response(URI(@gamemodes_uri.to_s+"/#{id}/average.json?token=#{@token}"))
744
+ JSON.parse(event.body)
745
+ end
746
+
747
+ def get_average_scores_by_application
748
+ @token = get_token("user") unless @token_type=='user'
749
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/application/averages.json?token=#{@token}"))
750
+ JSON.parse(event.body)
751
+ end
752
+
753
+ def delete_score(id)
754
+ @token = get_token("user") unless @token_type=='user'
755
+ http = Net::HTTP.new(@server)
756
+ delete = Net::HTTP::Delete.new("/scores/#{id}?token=#{@token}")
757
+ http.request(delete)
758
+ end
759
+
760
+ def create_gamemodeparameter(params)
761
+ @token = get_token("user") unless @token_type=='user'
762
+ req = Net::HTTP::Post.new(URI("http://"+@server.to_s+"/gamemodeparameters.json").path)
763
+ params.merge! "token" => @token
764
+ normalized = normalize(params)
765
+ req.body = "#{normalized}"
766
+ create=Net::HTTP.start(URI("http://"+@server.to_s).host) do |http|
767
+ http.request(req)
768
+ end
769
+ JSON.parse(create.body)
770
+
771
+ end
772
+
773
+ def update_gamemodeparameter(id, params)
774
+ @token = get_token("user") unless @token_type=='user'
775
+ req = Net::HTTP::Put.new(URI("http://"+@server.to_s+"/gamemodeparameters/#{id}.json").path)
776
+ params.merge! "token" => @token
777
+ normalized = normalize(params)
778
+ req.body = "#{normalized}"
779
+ update=Net::HTTP.start(URI("http://"+@server.to_s).host) do |http|
780
+ http.request(req)
781
+ end
782
+ JSON.parse(update.body)
783
+
784
+ end
785
+
786
+ def get_gamemodeparameter_by_id(id)
787
+ @token = get_token("user") unless @token_type=='user'
788
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/gamemodeparameters/#{id}.json?token=#{@token}"))
789
+ JSON.parse(event.body)
790
+ end
791
+
792
+ def get_gamemodeparameter_by_gamemode_id(id)
793
+ @token = get_token("user") unless @token_type=='user'
794
+ event = Net::HTTP.get_response(URI(@gamemodes_uri.to_s+"/#{id}/gamemodeparameters.json?token=#{@token}"))
795
+ JSON.parse(event.body)
796
+ end
797
+
798
+ def delete_gamemodeparameter(id)
799
+ @token = get_token("user") unless @token_type=='user'
800
+ http = Net::HTTP.new(@server)
801
+ delete = Net::HTTP::Delete.new("/gamemodeparameters/#{id}?token=#{@token}")
802
+ http.request(delete)
803
+ end
804
+
805
+ def create_gamemodeparametervalue(params)
806
+ @token = get_token("user") unless @token_type=='user'
807
+ req = Net::HTTP::Post.new(URI("http://"+@server.to_s+"/gamemodeparametervalues.json").path)
808
+ params.merge! "token" => @token
809
+ normalized = normalize(params)
810
+ req.body = "#{normalized}"
811
+ create=Net::HTTP.start(URI("http://"+@server.to_s).host) do |http|
812
+ http.request(req)
813
+ end
814
+ JSON.parse(create.body)
815
+
816
+ end
817
+
818
+ def update_gamemodeparametervalues(id, params)
819
+ @token = get_token("user") unless @token_type=='user'
820
+ req = Net::HTTP::Put.new(URI("http://"+@server.to_s+"/gamemodeparametervalues/#{id}.json").path)
821
+ params.merge! "token" => @token
822
+ normalized = normalize(params)
823
+ req.body = "#{normalized}"
824
+ update=Net::HTTP.start(URI("http://"+@server.to_s).host) do |http|
825
+ http.request(req)
826
+ end
827
+ JSON.parse(update.body)
828
+
829
+ end
830
+
831
+ def get_gamemodeparametervalue_by_id(id)
832
+ @token = get_token("user") unless @token_type=='user'
833
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/gamemodeparametervalues/#{id}.json?token=#{@token}"))
834
+ JSON.parse(event.body)
835
+ end
836
+
837
+ def get_gamemodeparametervalue_by_score_id(id)
838
+ @token = get_token("user") unless @token_type=='user'
839
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/scores/#{id}/gamemodeparametervalues.json?token=#{@token}"))
840
+ JSON.parse(event.body)
841
+ end
842
+
843
+ def get_api_gamemodeparametervalue_by_score_id(score_id, para_id)
844
+ @token = get_token("user") unless @token_type=='user'
845
+ event = Net::HTTP.get_response(URI("http://"+@server.to_s+"/scores/#{score_id}/gamemodeparameter/#{para_id}/values.json?token=#{@token}"))
846
+ JSON.parse(event.body)
847
+ end
848
+
849
+ def delete_gamemodeparametervalue(id)
850
+ @token = get_token("user") unless @token_type=='user'
851
+ http = Net::HTTP.new(@server)
852
+ delete = Net::HTTP::Delete.new("/gamemodeparametervalues/#{id}?token=#{@token}")
853
+ http.request(delete)
854
+ end
855
+
856
+
857
+ module HashConverter
858
+
859
+ def self.encode(value, key = nil, out_hash = {})
860
+ case value
861
+ when Hash then
862
+ value.each { |k, v| encode(v, append_key(key, k), out_hash) }
863
+ out_hash
864
+ when Array then
865
+ value.each { |v| encode(v, "#{key}[]", out_hash) }
866
+ out_hash
867
+ when nil then
868
+ ''
869
+ else
870
+ out_hash[key] = value
871
+ out_hash
872
+ end
873
+ end
874
+
875
+ private
876
+
877
+ def self.append_key(root_key, key)
878
+ root_key.nil? ? :"#{key}" : :"#{root_key}[#{key.to_s}]"
879
+ end
880
+
881
+ end
882
+
883
+ def normalize (var)
884
+ var = HashConverter.encode(var)
885
+ var.collect { |k, v|
886
+ if v.is_a? Hash
887
+ v.collect { |k1, v1| "#{k}[#{k1}]=#{v1}" }.sort.join('&')
888
+ else
889
+ "#{k}=#{v}"
890
+ end
891
+ }.sort.join('&')
892
+ end
893
+
894
+
895
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quickblox
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrey Kozhokaru
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: QuickBlox.com api
15
+ email: andrey.kozhokaru@quickblox.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - quickblox.rb
21
+ - config.yml.example
22
+ homepage: http://quickblox.com/
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.21
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: SuperPower for your apps
46
+ test_files: []