putio-cli 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZjIwNDM1MDcxMDQyNDJmMmRjZWUwZGZmOWQ5ZmUxMTlmYjU2NTY1YQ==
5
+ data.tar.gz: !binary |-
6
+ MzU1ODY4OGNjNTBhNmI4NTUzNGU1NmU1NmM2MWZmYjQ0MWQ3ZmNhZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MzYzZGUxZWI1YTkwMGIxNWZiNjExZjQ0ZTEyYWRhYTJhNmI5MWExMmU4ODVk
10
+ YmFiYWI1MDM1NTc1YmE0NmU0MDI3MjIwYmUwYTNmYjFiMzEyNTAyYjVjYjlh
11
+ NjZlZTIwZWMxZGY2Nzc1NTZmMjNhZTM3YmQyMDZmOTQ0Njg2NTI=
12
+ data.tar.gz: !binary |-
13
+ ZWQzNjAzYTkxZmJhNWNkOTZiOTJhNWQ5N2ZlNDJhNmYxNmZkNTA0OTFhZDk2
14
+ OWJhYzkwNGQ0ZDlhYjc1YmQ5ZmE5NzA1YmNiZmQ0NTg1MmEwMTk2MjAyNmJm
15
+ MzVjNjYzODE2OGY2ODJhNjFiZmUxYzBlMWRmOGQ0ZjQyNDZlNWM=
@@ -0,0 +1,4 @@
1
+ .bundle/config
2
+ Gemfile.lock
3
+ putio*.gem
4
+ putio-cli.gemspec
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'fakeweb', '1.3.0'
6
+ gem 'shoulda', '2.11.3'
7
+
@@ -0,0 +1,131 @@
1
+ # Put.io V2
2
+
3
+ This is a Ruby client for the [Put.io REST API](https://api.put.io/v2/docs/#jsonp) with full support for files, transfers, friends and settings. It utilises the new Put.io V2 with OAuth integration so you can build fully featured applications.
4
+
5
+ ### Installation
6
+ **This is a fork of [rodreegez/putio](http://github.com/rodreegez/putio) and I'm awaiting the update of the 'putio' gem. Until then it can be installed with 'putio-cli'**
7
+
8
+ ```ruby
9
+ gem install putio
10
+ ```
11
+
12
+ ### Usage
13
+
14
+ ```ruby
15
+ require 'putio'
16
+ ```
17
+
18
+ ```ruby
19
+ p = Putio.new(000, 'abcdefghijklmn012345', 'http://localhost/callback') # Create an application at https://put.io/v2/oauth2/register
20
+ ```
21
+
22
+ ```ruby
23
+ p.me # Show my information
24
+ ```
25
+
26
+ ## Options
27
+
28
+ ```ruby
29
+ Putio::Client.putio_methods # Get all methods like .methods
30
+
31
+ # Generate a OAuth HTTP URL
32
+ p.oauth_url
33
+
34
+ # Once returned with code, complete authorisation
35
+ p.oauth_complete(params[:code])
36
+
37
+ # Or, if you already have a access token - set it and ignore the previous two steps.
38
+ p.access_token = ""
39
+
40
+ ## Files
41
+
42
+ # Show files in a particular folder.
43
+ p.files(folder_id = 0)
44
+
45
+ # Or just show them all, including those from subfolders.
46
+ p.all
47
+
48
+ # Search for a file
49
+ p.search(query, page = 0)
50
+
51
+ # Upload a local file
52
+ p.upload(local_file, folder_id = 0)
53
+
54
+ # Create a folder
55
+ p.create_folder(name, folder_id = 0)
56
+
57
+ # File properties
58
+ p.file(file_id)
59
+
60
+ # Remove a file
61
+ p.delete(file_id)
62
+
63
+ # Rename a file
64
+ p.rename(file_id)
65
+
66
+ # Move a file
67
+ p.move(file_id, folder_id = 0)
68
+
69
+ # Convert a video file on Put.io to MP4
70
+ p.convert_to_mp4(file_id)
71
+
72
+ # Status of MP4 video
73
+ p.mp4(file_id)
74
+
75
+ # Universal download link of a file
76
+ p.download(file_id)
77
+
78
+ # Universal download link of a ZIP of multiple files
79
+ p.zip(file_ids) # Example: p.zip([300, 59412, 9313])
80
+
81
+ ## Transfers
82
+
83
+ # Show all current transfers
84
+ p.transfers
85
+
86
+ # Show count of transfer queue
87
+ p.transfers_count
88
+
89
+ # Create a transfer
90
+ # Note: Callback URL pings a HTTP URL when a file/torrent has been downloaded to Put.io
91
+ p.create_transfer(http_url, folder_id = 0, extract_if_possible = true, callback_url = nil)
92
+
93
+ # Show status on transfer
94
+ p.transfer(transfer_id)
95
+
96
+ # Cancel Transfer
97
+ p.cancel_transfers(ids) # Example: p.cancel_transfers([412421, 4812984, 19334])
98
+
99
+ # Clean transfers queue (i.e. remove completed transfers)
100
+ p.clean_transfers
101
+
102
+ # Show authenticated user
103
+ p.me
104
+
105
+ # Show preferences of authenticated user
106
+ p.settings
107
+
108
+ # My Put.io friends
109
+ p.friends
110
+
111
+ # My Put.io incoming friend requests
112
+ p.friend_requests
113
+
114
+ # Deny friend request
115
+ p.deny_friend_request(username)
116
+
117
+ # Send friend request
118
+ p.make_friends_with(username)
119
+ ```
120
+
121
+ ## Todo
122
+ - Make Putio::Client object syntax 'chain' better (i.e. `Putio::File.new(391239123).delete`)
123
+ - Update test units to reflect V2 API.
124
+
125
+ ## Changelog
126
+
127
+ 23/6/2013
128
+ - Added support for V2 API
129
+ - Added OAuth Support
130
+ - Removed development folder.
131
+ - Removed :rubygems and replaced with 'http://rubygems.org'
@@ -0,0 +1,21 @@
1
+ require 'rake/testtask'
2
+ Rake::TestTask.new(:test) do |test|
3
+ test.libs << 'lib' << 'test'
4
+ test.pattern = 'test/**/*_test.rb'
5
+ test.verbose = true
6
+ end
7
+ task :default => :test
8
+
9
+ task :install do
10
+ %x|gem install putio|
11
+ end
12
+
13
+ task :uninstall do
14
+ %x|gem uninstall putio|
15
+ end
16
+
17
+ task :build do
18
+ %x|gem build putio.gemspec|
19
+ end
20
+
21
+ task :reinstall => [:uninstall, :build, :install]
@@ -0,0 +1,4 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'putio/putio'
4
+ require 'putio/client'
@@ -0,0 +1,540 @@
1
+ ##
2
+ ## Let's include our dependencies.
3
+ ##
4
+ require 'curb'
5
+ require 'rubygems'
6
+ require 'crack/json'
7
+ require 'hashie'
8
+ require 'json'
9
+ require 'uri'
10
+ require 'cgi'
11
+
12
+
13
+ ##
14
+ ## Let's extend :String to identify an Integer.
15
+ ##
16
+ class String
17
+ def is_i?
18
+ !!(self =~ /^[-+]?[0-9]+$/)
19
+ end
20
+ end
21
+
22
+
23
+ ##
24
+ ## This serves custom errors that people can begin-rescue.
25
+ ##
26
+ module PutioError
27
+ class ClientIDInvalid < Exception; end
28
+ class FileNotExist < Exception; end
29
+ end
30
+
31
+
32
+ ##
33
+ ## This contains the entire Putio Ruby interface.
34
+ ##
35
+ module Putio
36
+
37
+ # This is used to make reading code easier later. Ignore this.
38
+ class HTTP
39
+ attr_reader :GET, :POST
40
+ GET, POST = false, true
41
+ end
42
+
43
+ # Our Ruby client.
44
+ class Client
45
+ # Instance-based variables we'll need to allocate memory for.
46
+ attr_writer :client_id, :application_secret, :redirect_uri, :access_token
47
+ # The base Put.io API URL.
48
+ PUTIO_BASE_URL = "https://api.put.io/v2"
49
+
50
+
51
+ ##
52
+ ## Putio::Client.new
53
+ ## Initialize the Putio::Client instance.
54
+ ##
55
+ def initialize(client_id, application_secret, redirect_uri, access_token = nil)
56
+ # The client_id must be a Integer
57
+ raise PutioError::ClientIDInvalid unless client_id.to_s.is_i?
58
+
59
+ # Store arguments as instance variables
60
+ @client_id = client_id
61
+ @application_secret = application_secret
62
+ @redirect_uri = redirect_uri
63
+ @access_token = access_token || nil
64
+ end
65
+
66
+
67
+ ##
68
+ ## Putio::Client.putio_methods
69
+ ## Similar to .methods - you can use this to see all possible Put.io-related methods.
70
+ ##
71
+ def putio_methods
72
+ [:oauth_url, :oauth_complete, :files, :all, :search, :upload, :create_folder, :file, :delete, :rename, :move, :convert_to_mp4, :mp4, :download, :zip, :transfers, :transfers_count, :create_transfer, :cancel_transfer, :cancel_transfers, :clean_transfers, :me, :settings, :friends, :friend_requests, :deny_friend_requests, :make_friend_with]
73
+ end
74
+
75
+
76
+ ##
77
+ ## Putio::Client.oauth_url(response_type)
78
+ ## Provides a OAuth URL on Put.io for the user to grant access.
79
+ ##
80
+ def oauth_url(response_type = 'code')
81
+ # The Redirect URI must be the same as registered with Put.io
82
+ PUTIO_BASE_URL + "/oauth2/authenticate?client_id=%i&response_type=%s&redirect_uri=%s" % [@client_id, response_type, @redirect_uri]
83
+ end
84
+
85
+
86
+ ##
87
+ ## Putio::Client.oauth_complete(code)
88
+ ## Provides an oauth_token used to authenticate API calls.
89
+ ##
90
+ def oauth_complete(code)
91
+ # Let's compile the API URL we're calling.
92
+ url = PUTIO_BASE_URL + "/oauth2/access_token?client_id=%i&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s" % [@client_id, @application_secret, @redirect_uri, code]
93
+
94
+ # And call it.
95
+ response = Curl::Easy.perform(url) do |req|
96
+ req.headers['Accept'] = 'application/json'
97
+ end
98
+
99
+ # Use Crack to parse the JSON
100
+ response = Crack::JSON.parse(response.body_str)
101
+
102
+ # And use Hashie to present it.
103
+ response = Hashie::Mash.new(response)
104
+
105
+ # Save it locally.
106
+ @access_token = response.access_token
107
+
108
+ # Return it
109
+ response
110
+ end
111
+
112
+
113
+ ##
114
+ ## Putio::Client.files(folder)
115
+ ## Shows all the users files in a particular folder.
116
+ ##
117
+ def files(folder = 0)
118
+ # Requires authorization
119
+ raise PutioError::AuthorizationRequired if authentication_required!
120
+
121
+ make_get_call('/files/list?parent_id=%i' % [folder]).files
122
+ end
123
+
124
+
125
+ ##
126
+ ## Putio::Client.all
127
+ ## Shows all the files, including from subfolders.
128
+ ##
129
+ def all
130
+ # Requires authorization
131
+ raise PutioError::AuthorizationRequired if authentication_required!
132
+
133
+ files(-1)
134
+ end
135
+
136
+
137
+ ##
138
+ ## Putio::Client.search(query, page)
139
+ ## Makes a file search against you and your shared files.
140
+ ##
141
+ def search(query, page = 0)
142
+ # Requires authorization
143
+ raise PutioError::AuthorizationRequired if authentication_required!
144
+
145
+ make_get_call('/files/list?parent_id=%i' % [folder]).files
146
+ end
147
+
148
+
149
+ ##
150
+ ## Putio::Client.upload(file, folder)
151
+ ## Upload a local file to Put.io
152
+ ##
153
+ def upload(file, folder = 0)
154
+ # Requires authorization
155
+ raise PutioError::AuthorizationRequired if authentication_required!
156
+
157
+ # Make the upload.
158
+ response = make_upload_call('/files/upload?parent_id=%i' % [folder], file)
159
+
160
+ # Return whatever.
161
+ response.transfer || response.file
162
+ end
163
+
164
+
165
+ ##
166
+ ## Putio::Client.create_folder(name, folder)
167
+ ## Creates a new folder in your Put.io space.
168
+ ##
169
+ def create_folder(name, folder = 0)
170
+ # Requires authorization
171
+ raise PutioError::AuthorizationRequired if authentication_required!
172
+
173
+ make_post_call('/files/create-folder?name=%s&parent_id=%i' % [name, folder])
174
+ end
175
+
176
+
177
+ ##
178
+ ## Putio::Client.file(id)
179
+ ## Returns the properties of a particular file.
180
+ ##
181
+ def file(id)
182
+ # Requires authorization
183
+ raise PutioError::AuthorizationRequired if authentication_required!
184
+
185
+ response = make_get_call('/files/%i' % [id])
186
+ response.download = download(id)
187
+
188
+ response
189
+ end
190
+
191
+
192
+ ##
193
+ ## Putio::Client.delete(id)
194
+ ## Removes a particular file. Parameter can be Integer or Array
195
+ ##
196
+ def delete(id)
197
+ # Requires authorization
198
+ raise PutioError::AuthorizationRequired if authentication_required!
199
+
200
+ if id.is_a? Array then
201
+ id = id.join(',')
202
+ end
203
+
204
+ make_post_call('/files/delete?file_ids=%s' % [id]).status == "OK"
205
+ end
206
+
207
+ ##
208
+ ## Putio::Client.rename(id, name)
209
+ ## Renames a particular file.
210
+ ##
211
+ def rename(id, name)
212
+ # Requires authorization
213
+ raise PutioError::AuthorizationRequired if authentication_required!
214
+
215
+ make_post_call('/files/rename?file_id=%i&name=%s' % [id, name]).status == "OK"
216
+ end
217
+
218
+ ##
219
+ ## Putio::Client.move(id, folder)
220
+ ## Move a file to another directory.
221
+ ##
222
+ def move(id, folder = 0)
223
+ # Requires authorization
224
+ raise PutioError::AuthorizationRequired if authentication_required!
225
+
226
+ # This provides support for an Array of ids.
227
+ if id.is_a? Array then
228
+ id = id.join(',')
229
+ end
230
+
231
+ make_post_call('/files/move?file_ids=%s&parent_id=%i' % [id, folder]).status == "OK"
232
+ end
233
+
234
+
235
+ ##
236
+ ## Putio::Client.convert_to_mp4(id)
237
+ ## Put.io offer MP4 conversion - call this method to convert a video.
238
+ ##
239
+ def convert_to_mp4(id)
240
+ # Requires authorization
241
+ raise PutioError::AuthorizationRequired if authentication_required!
242
+
243
+ make_post_call('/files/%i/mp4' % [id]).status == "OK"
244
+ end
245
+
246
+
247
+ ##
248
+ ## Putio::Client.mp4(id)
249
+ ## Put.io offer MP4 conversion - call this method to see the status of the MP4 video.
250
+ ##
251
+ def mp4(id)
252
+ # Requires authorization
253
+ raise PutioError::AuthorizationRequired if authentication_required!
254
+
255
+ make_get_call('/files/%i/mp4' % [id]).mp4
256
+ end
257
+
258
+
259
+ ##
260
+ ## Putio::Client.download(id)
261
+ ## Provides a URL for anyone to download a file.
262
+ ##
263
+ def download(id)
264
+ # Requires authorization
265
+ raise PutioError::AuthorizationRequired if authentication_required!
266
+
267
+ PUTIO_BASE_URL + ("/files/%i/download?oauth_token=%s" % [id, @access_token])
268
+ end
269
+
270
+
271
+ ##
272
+ ## Putio::Client.zip(id)
273
+ ## Provides a URL of a ZIP of multiple files.
274
+ ##
275
+ def zip(id)
276
+ # Requires authorization
277
+ raise PutioError::AuthorizationRequired if authentication_required!
278
+
279
+ # This provides support for an Array of ids.
280
+ if id.is_a? Array then
281
+ id = id.join(',')
282
+ end
283
+
284
+ # Return zip download link
285
+ PUTIO_BASE_URL + ("/files/zip?file_ids=%s&oauth_token=%s" % [id, @access_token])
286
+ end
287
+
288
+
289
+ ##
290
+ ## Putio::Client.transfers
291
+ ## Shows all the current transfers.
292
+ ##
293
+ def transfers
294
+ # Requires authorization
295
+ raise PutioError::AuthorizationRequired if authentication_required!
296
+
297
+ make_get_call('/transfers/list').transfers
298
+ end
299
+
300
+
301
+ ##
302
+ ## Putio::Client.transfers_count
303
+ ## Shows how many downloads are currently in the queue.
304
+ ##
305
+ def transfers_count
306
+ # Requires authorization
307
+ raise PutioError::AuthorizationRequired if authentication_required!
308
+
309
+ make_get_call('/transfers/count').count
310
+ end
311
+
312
+
313
+ ##
314
+ ## Putio::Client.create_transfer(url, folder, extract, callback_url)
315
+ ## Download an external file/torrent. Optionally choose to extract and have a HTTP callback URL on download completion.
316
+ ##
317
+ def create_transfer(url, folder = 0, extract = true, callback_url = nil)
318
+ # Requires authorization
319
+ raise PutioError::AuthorizationRequired if authentication_required!
320
+
321
+ make_post_call('/transfers/add?url=%s&save_parent_id=%i&extract=%s&callback_url=%s' % [url, folder, extract.to_s.capitalize, callback_url])
322
+ end
323
+
324
+
325
+ ##
326
+ ## Putio::Client.transfer(id)
327
+ ## Shows the status of a particular transfer.
328
+ ##
329
+ def transfer(id)
330
+ # Requires authorization
331
+ raise PutioError::AuthorizationRequired if authentication_required!
332
+
333
+ make_get_call('/transfers/%i' % [id])
334
+ end
335
+
336
+
337
+ ##
338
+ ## Putio::Client.cancel_transfer(id)
339
+ ## Alias of Putio::Client.cancel_transfers
340
+ ##
341
+ def cancel_transfer(id)
342
+ cancel_transfers(id)
343
+ end
344
+
345
+
346
+ ##
347
+ ## Putio::Client.cancel_transfers(id)
348
+ ## Cancels any transfers that have not yet completed. Use delete to remove downloaded files.
349
+ ##
350
+ def cancel_transfers(id)
351
+ # Requires authorization
352
+ raise PutioError::AuthorizationRequired if authentication_required!
353
+
354
+ # This provides support for an Array of ids.
355
+ if id.is_a? Array then
356
+ id = id.join(',')
357
+ end
358
+
359
+ make_get_call('/transfers/cancel?transfer_ids=%s' % [id]).status == "OK"
360
+ end
361
+
362
+
363
+ ##
364
+ ## Putio::Client.clean_transfers(id)
365
+ ## Removes any completed transfers from the list.
366
+ ##
367
+ def clean_transfers(id)
368
+ # Requires authorization
369
+ raise PutioError::AuthorizationRequired if authentication_required!
370
+
371
+ make_get_call('/transfers/clean').status == "OK"
372
+ end
373
+
374
+
375
+ ##
376
+ ## Putio::Client.me
377
+ ## Shows information about the authenticated user.
378
+ ##
379
+ def me
380
+ # Requires authorization
381
+ raise PutioError::AuthorizationRequired if authentication_required!
382
+
383
+ make_get_call('/account/info').info
384
+ end
385
+
386
+
387
+ ##
388
+ ## Putio::Client.settings
389
+ ## Shows preferences of the authenticated user.
390
+ ##
391
+ def settings
392
+ # Requires authorization
393
+ raise PutioError::AuthorizationRequired if authentication_required!
394
+
395
+ make_get_call('/account/settings').settings
396
+ end
397
+
398
+
399
+ ##
400
+ ## Putio::Client.friends
401
+ ## Shows all the friends of the authenticated user.
402
+ ##
403
+ def friends
404
+ # Requires authorization
405
+ raise PutioError::AuthorizationRequired if authentication_required!
406
+
407
+ make_get_call('/friends/list').friends
408
+ end
409
+
410
+
411
+ ##
412
+ ## Putio::Client.friend_requests
413
+ ## Shows pending friend requests of the authenticated user.
414
+ ##
415
+ def friend_requests
416
+ # Requires authorization
417
+ raise PutioError::AuthorizationRequired if authentication_required!
418
+
419
+ make_get_call('/friends/waiting-requests').friends
420
+ end
421
+
422
+
423
+ ##
424
+ ## Putio::Client.deny_friend_requests(username)
425
+ ## Rejects a friend request.
426
+ ##
427
+ def deny_friend_request(username)
428
+ # Requires authorization
429
+ raise PutioError::AuthorizationRequired if authentication_required!
430
+
431
+ make_post_call('/friends/%s/deny' % [username]).status == "OK"
432
+ end
433
+
434
+
435
+ ##
436
+ ## Putio::Client.make_friend_with(username)
437
+ ## This sends a friend request to a particular user.
438
+ ##
439
+ def make_friend_with(username)
440
+ # Requires authorization
441
+ raise PutioError::AuthorizationRequired if authentication_required!
442
+
443
+ make_post_call('/friends/%s/request' % [username]).status == "OK"
444
+ end
445
+
446
+
447
+ private
448
+
449
+ ##
450
+ ## Putio::Client.authentication_required!
451
+ ## A private function used to tell if the user has granted access.
452
+ ##
453
+ def authentication_required!
454
+ @access_token.nil?
455
+ end
456
+
457
+ ##
458
+ ## Putio::Client.make_call(endpoint)
459
+ ## This is the underlying code that makes the HTTP request.
460
+ ##
461
+ def make_call(endpoint, is_post = false)
462
+ # Before anything.. is it a POST request?
463
+ postdata = Hash.new
464
+
465
+ # It's a HTTP POST request.
466
+ if is_post && endpoint.include?('?') then
467
+ endpoint, postdata = endpoint.split('?')
468
+ postdata = CGI::parse(postdata)
469
+ end
470
+
471
+ # Let's compile the API URL we're calling.
472
+ url = PUTIO_BASE_URL + endpoint
473
+ url += url.include?("?") ? "&" : "?"
474
+ url += "oauth_token=%s" % [@access_token]
475
+
476
+ # And call it. POST or GET ;)
477
+ if is_post then
478
+ response = Curl.post(url, postdata) { |req| req.headers['Accept'] = 'application/json' }
479
+ else
480
+ response = Curl.get(url) { |req| req.headers['Accept'] = 'application/json' }
481
+ end
482
+
483
+ # Use Crack to parse the JSON
484
+ begin
485
+ response = Crack::JSON.parse(response.body_str)
486
+ rescue Psych::SyntaxError
487
+ response = JSON::parse(response.body_str)
488
+ end
489
+
490
+ # And use Hashie to present it.
491
+ response = Hashie::Mash.new(response)
492
+ end
493
+
494
+ ##
495
+ ## Putio::Client.make_get_call(endpoint)
496
+ ## This is the underlying code that makes the HTTP GET request.
497
+ ##
498
+ def make_get_call(endpoint)
499
+ make_call(endpoint, Putio::HTTP::GET)
500
+ end
501
+
502
+ ##
503
+ ## Putio::Client.make_post_call(endpoint)
504
+ ## This is the underlying code that makes the HTTP POST request.
505
+ ##
506
+ def make_post_call(endpoint)
507
+ make_call(endpoint, Putio::HTTP::POST)
508
+ end
509
+
510
+ ##
511
+ ## Putio::Client.make_upload_call(endpoint, file)
512
+ ## This is the underlying code that makes the HTTP multipart POST request.
513
+ ##
514
+ def make_upload_call(endpoint, file)
515
+ raise PutioError::FileNotExist unless File.exists?(file)
516
+
517
+ # Let's compile the API URL we're calling.
518
+ url = PUTIO_BASE_URL + endpoint
519
+ url += url.include?("?") ? "&" : "?"
520
+ url += "oauth_token=%s" % [@access_token]
521
+
522
+ # And call it.
523
+ response = Curl::Easy.new(url)
524
+ response.multipart_form_post = true
525
+ response.headers['Accept'] = 'application/json'
526
+ response.http_post(Curl::PostField.file('file', file))
527
+
528
+ # Use Crack to parse the JSON
529
+ begin
530
+ response = Crack::JSON.parse(response.body_str)
531
+ rescue Psych::SyntaxError
532
+ response = JSON::parse(response.body_str)
533
+ end
534
+
535
+ # And use Hashie to present it.
536
+ response = Hashie::Mash.new(response)
537
+ end
538
+
539
+ end
540
+ end
@@ -0,0 +1,6 @@
1
+ module Putio
2
+ def self.new(application_id, application_secret, redirect_uri, access_token = nil)
3
+ # Acts as an alias for Putio::Client.new
4
+ Putio::Client.new(application_id, application_secret, redirect_uri, access_token)
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "putio"
3
+ s.version = "0.0.2"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.authors = ["Adam Rogers", "Bilawal Hameed"]
6
+ s.email = ["adam@rodreegez.com", "bilawal@games.com"]
7
+ s.homepage = "http://github.com/rodreegez/putio"
8
+ s.summary = %q{Ruby wrapper for Put.io API}
9
+ s.description = %q{A lightweight and simple Ruby interface to the Put.io cloud service API available at http://api.put.io}
10
+
11
+ s.rubyforge_project = "putio"
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test}/*`.split("\n")
15
+ s.require_paths = ["lib"]
16
+
17
+ s.add_dependency 'curb', '0.8.4'
18
+ s.add_dependency 'json', '1.8.0'
19
+ s.add_dependency 'crack', '0.4.0'
20
+ s.add_dependency 'hashie', '2.0.5'
21
+ end
@@ -0,0 +1 @@
1
+ {"response": {"total":1, "results":[{"dir_id":"7731413","id":"8664","name":"Dean Strelau"}]},"error":false,"user_name":"rodreegez","id":2183}
@@ -0,0 +1 @@
1
+ {"response": {"total": 1, "results": [{"name": "rodreegez", "friends_count": 1, "bw_avail_last_month": "462774811875", "shared_space": 0, "shared_items": 0, "bw_quota_available": "496108387615", "disk_quota": "53687091200", "disk_quota_available": "13259450609", "bw_quota": "53687091200"}]}, "error": false, "user_name": "rodreegez", "id": 2183}
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ Bundler.require
5
+
6
+ require 'lib/putio'
7
+
8
+ FakeWeb.allow_net_connect = false
9
+
10
+ def fixture_file(filename)
11
+ return '' if filename == ''
12
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
13
+ File.read(file_path)
14
+ end
15
+
16
+ def stub(http, path, file)
17
+ response = { :body => fixture_file(file), :content_type => 'text/json' }
18
+ FakeWeb.register_uri(http, path, response)
19
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class ClientTest < Test::Unit::TestCase
4
+ context 'creating a new client' do
5
+ should 'succeed with api_key and api_secret' do
6
+ assert_nothing_raised do
7
+ putio = Putio::Client.new('api_key', 'api_secret')
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class PutioTest < Test::Unit::TestCase
4
+ context 'Putio' do
5
+ should 'initilize a Client with api_key and api_secret' do
6
+ putio = Putio.new('api_key', 'api_secret')
7
+
8
+ assert_instance_of Putio::Client, putio
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < Test::Unit::TestCase
4
+ context 'A Putio client with valid credentials' do
5
+ setup { @putio = Putio.new('abc', '123') }
6
+
7
+ should %q|return user's info via GET| do
8
+ stub(:get, %r|http://api.put.io/v1/user\?method=info|,
9
+ 'user_info.json')
10
+ user_info = @putio.get_user_info
11
+
12
+ assert_equal "rodreegez", user_info.first.name
13
+ end
14
+
15
+ should %q|return user's info via POST| do
16
+ stub(:post, %r|http:\/\/api.put.io\/v1\/user\?method=info|,
17
+ 'user_info.json')
18
+ user_info = @putio.post_user_info
19
+
20
+ assert_equal "rodreegez", user_info.first.name
21
+ end
22
+
23
+ should %q|return user's friends via GET| do
24
+ stub(:get, %r|http:\/\/api.put.io\/v1\/user\?method=friends|,
25
+ 'user_friends.json')
26
+ user_friends = @putio.get_user_friends
27
+
28
+ assert_equal "Dean Strelau", user_friends.first.name
29
+ end
30
+
31
+ should %q|return user's friends via POST| do
32
+ stub(:post, %r|http:\/\/api.put.io\/v1\/user\?method=friends|,
33
+ 'user_friends.json')
34
+ user_friends = @putio.post_user_friends
35
+
36
+ assert_equal "Dean Strelau", user_friends.first.name
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: putio-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Adam Rogers
8
+ - Bilawal Hameed
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: curb
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '='
19
+ - !ruby/object:Gem::Version
20
+ version: 0.8.4
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '='
26
+ - !ruby/object:Gem::Version
27
+ version: 0.8.4
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '='
33
+ - !ruby/object:Gem::Version
34
+ version: 1.8.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '='
40
+ - !ruby/object:Gem::Version
41
+ version: 1.8.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: crack
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '='
47
+ - !ruby/object:Gem::Version
48
+ version: 0.4.0
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '='
54
+ - !ruby/object:Gem::Version
55
+ version: 0.4.0
56
+ - !ruby/object:Gem::Dependency
57
+ name: hashie
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '='
61
+ - !ruby/object:Gem::Version
62
+ version: 2.0.5
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.0.5
70
+ description: A lightweight and simple Ruby interface to the Put.io cloud service API
71
+ available at http://api.put.io
72
+ email:
73
+ - adam@rodreegez.com
74
+ - bilawal@games.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - .gitignore
80
+ - Gemfile
81
+ - README.markdown
82
+ - Rakefile
83
+ - lib/putio.rb
84
+ - lib/putio/client.rb
85
+ - lib/putio/putio.rb
86
+ - putio.gemspec
87
+ - test/fixtures/user_friends.json
88
+ - test/fixtures/user_info.json
89
+ - test/test_helper.rb
90
+ - test/units/client_test.rb
91
+ - test/units/putio_test.rb
92
+ - test/units/user_test.rb
93
+ homepage: http://github.com/rodreegez/putio
94
+ licenses: []
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project: putio
112
+ rubygems_version: 2.0.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Ruby wrapper for Put.io API
116
+ test_files: []