Class: Putio::Client
- Inherits:
-
Object
- Object
- Putio::Client
- Defined in:
- lib/putio/client.rb
Overview
Our Ruby client.
Constant Summary collapse
- PUTIO_BASE_URL =
The base Put.io API URL.
"https://api.put.io/v2"
Instance Attribute Summary collapse
-
#access_token ⇒ Object
writeonly
Instance-based variables we’ll need to allocate memory for.
-
#application_secret ⇒ Object
writeonly
Instance-based variables we’ll need to allocate memory for.
-
#client_id ⇒ Object
writeonly
Instance-based variables we’ll need to allocate memory for.
-
#redirect_uri ⇒ Object
writeonly
Instance-based variables we’ll need to allocate memory for.
Instance Method Summary collapse
-
#all ⇒ Object
Putio::Client.all Shows all the files, including from subfolders.
-
#cancel_transfer(id) ⇒ Object
Putio::Client.cancel_transfer(id) Alias of Putio::Client.cancel_transfers.
-
#cancel_transfers(id) ⇒ Object
Putio::Client.cancel_transfers(id) Cancels any transfers that have not yet completed.
-
#clean_transfers(id) ⇒ Object
Putio::Client.clean_transfers(id) Removes any completed transfers from the list.
-
#convert_to_mp4(id) ⇒ Object
Putio::Client.convert_to_mp4(id) Put.io offer MP4 conversion - call this method to convert a video.
-
#create_folder(name, folder = 0) ⇒ Object
Putio::Client.create_folder(name, folder) Creates a new folder in your Put.io space.
-
#create_transfer(url, folder = 0, extract = true, callback_url = nil) ⇒ Object
Putio::Client.create_transfer(url, folder, extract, callback_url) Download an external file/torrent.
-
#delete(id) ⇒ Object
Putio::Client.delete(id) Removes a particular file.
-
#deny_friend_request(username) ⇒ Object
Putio::Client.deny_friend_requests(username) Rejects a friend request.
-
#download(id) ⇒ Object
Putio::Client.download(id) Provides a URL for anyone to download a file.
-
#file(id) ⇒ Object
Putio::Client.file(id) Returns the properties of a particular file.
-
#files(folder = 0) ⇒ Object
Putio::Client.files(folder) Shows all the users files in a particular folder.
-
#friend_requests ⇒ Object
Putio::Client.friend_requests Shows pending friend requests of the authenticated user.
-
#friends ⇒ Object
Putio::Client.friends Shows all the friends of the authenticated user.
-
#initialize(client_id, application_secret, redirect_uri, access_token = nil) ⇒ Client
constructor
Putio::Client.new Initialize the Putio::Client instance.
-
#make_friend_with(username) ⇒ Object
Putio::Client.make_friend_with(username) This sends a friend request to a particular user.
-
#me ⇒ Object
Putio::Client.me Shows information about the authenticated user.
-
#move(id, folder = 0) ⇒ Object
Putio::Client.move(id, folder) Move a file to another directory.
-
#mp4(id) ⇒ Object
Putio::Client.mp4(id) Put.io offer MP4 conversion - call this method to see the status of the MP4 video.
-
#oauth_complete(code) ⇒ Object
Putio::Client.oauth_complete(code) Provides an oauth_token used to authenticate API calls.
-
#oauth_url(response_type = 'code') ⇒ Object
Putio::Client.oauth_url(response_type) Provides a OAuth URL on Put.io for the user to grant access.
-
#putio_methods ⇒ Object
Putio::Client.putio_methods Similar to .methods - you can use this to see all possible Put.io-related methods.
-
#rename(id, name) ⇒ Object
Putio::Client.rename(id, name) Renames a particular file.
-
#search(query, page = 0) ⇒ Object
Putio::Client.search(query, page) Makes a file search against you and your shared files.
-
#settings ⇒ Object
Putio::Client.settings Shows preferences of the authenticated user.
-
#transfer(id) ⇒ Object
Putio::Client.transfer(id) Shows the status of a particular transfer.
-
#transfers ⇒ Object
Putio::Client.transfers Shows all the current transfers.
-
#transfers_count ⇒ Object
Putio::Client.transfers_count Shows how many downloads are currently in the queue.
-
#upload(file, folder = 0) ⇒ Object
Putio::Client.upload(file, folder) Upload a local file to Put.io.
-
#zip(id) ⇒ Object
Putio::Client.zip(id) Provides a URL of a ZIP of multiple files.
Constructor Details
#initialize(client_id, application_secret, redirect_uri, access_token = nil) ⇒ Client
Putio::Client.new Initialize the Putio::Client instance.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/putio/client.rb', line 54 def initialize(client_id, application_secret, redirect_uri, access_token = nil) # The client_id must be a Integer raise PutioError::ClientIDInvalid unless client_id.to_s.is_i? # Store arguments as instance variables @client_id = client_id @application_secret = application_secret @redirect_uri = redirect_uri @access_token = access_token || nil end |
Instance Attribute Details
#access_token=(value) ⇒ Object (writeonly)
Instance-based variables we’ll need to allocate memory for.
45 46 47 |
# File 'lib/putio/client.rb', line 45 def access_token=(value) @access_token = value end |
#application_secret=(value) ⇒ Object (writeonly)
Instance-based variables we’ll need to allocate memory for.
45 46 47 |
# File 'lib/putio/client.rb', line 45 def application_secret=(value) @application_secret = value end |
#client_id=(value) ⇒ Object (writeonly)
Instance-based variables we’ll need to allocate memory for.
45 46 47 |
# File 'lib/putio/client.rb', line 45 def client_id=(value) @client_id = value end |
#redirect_uri=(value) ⇒ Object (writeonly)
Instance-based variables we’ll need to allocate memory for.
45 46 47 |
# File 'lib/putio/client.rb', line 45 def redirect_uri=(value) @redirect_uri = value end |
Instance Method Details
#all ⇒ Object
Putio::Client.all Shows all the files, including from subfolders.
128 129 130 131 132 133 |
# File 'lib/putio/client.rb', line 128 def all # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! files(-1) end |
#cancel_transfer(id) ⇒ Object
Putio::Client.cancel_transfer(id) Alias of Putio::Client.cancel_transfers
340 341 342 |
# File 'lib/putio/client.rb', line 340 def cancel_transfer(id) cancel_transfers(id) end |
#cancel_transfers(id) ⇒ Object
Putio::Client.cancel_transfers(id) Cancels any transfers that have not yet completed. Use delete to remove downloaded files.
349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/putio/client.rb', line 349 def cancel_transfers(id) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! # This provides support for an Array of ids. if id.is_a? Array then id = id.join(',') end make_get_call('/transfers/cancel?transfer_ids=%s' % [id]).status == "OK" end |
#clean_transfers(id) ⇒ Object
Putio::Client.clean_transfers(id) Removes any completed transfers from the list.
366 367 368 369 370 371 |
# File 'lib/putio/client.rb', line 366 def clean_transfers(id) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/transfers/clean').status == "OK" end |
#convert_to_mp4(id) ⇒ Object
Putio::Client.convert_to_mp4(id) Put.io offer MP4 conversion - call this method to convert a video.
238 239 240 241 242 243 |
# File 'lib/putio/client.rb', line 238 def convert_to_mp4(id) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_post_call('/files/%i/mp4' % [id]).status == "OK" end |
#create_folder(name, folder = 0) ⇒ Object
Putio::Client.create_folder(name, folder) Creates a new folder in your Put.io space.
168 169 170 171 172 173 |
# File 'lib/putio/client.rb', line 168 def create_folder(name, folder = 0) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_post_call('/files/create-folder?name=%s&parent_id=%i' % [name, folder]) end |
#create_transfer(url, folder = 0, extract = true, callback_url = nil) ⇒ Object
Putio::Client.create_transfer(url, folder, extract, callback_url) Download an external file/torrent. Optionally choose to extract and have a HTTP callback URL on download completion.
316 317 318 319 320 321 |
# File 'lib/putio/client.rb', line 316 def create_transfer(url, folder = 0, extract = true, callback_url = nil) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_post_call('/transfers/add?url=%s&save_parent_id=%i&extract=%s&callback_url=%s' % [url, folder, extract.to_s.capitalize, callback_url]) end |
#delete(id) ⇒ Object
Putio::Client.delete(id) Removes a particular file. Parameter can be Integer or Array
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/putio/client.rb', line 195 def delete(id) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! if id.is_a? Array then id = id.join(',') end make_post_call('/files/delete?file_ids=%s' % [id]).status == "OK" end |
#deny_friend_request(username) ⇒ Object
Putio::Client.deny_friend_requests(username) Rejects a friend request.
426 427 428 429 430 431 |
# File 'lib/putio/client.rb', line 426 def deny_friend_request(username) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_post_call('/friends/%s/deny' % [username]).status == "OK" end |
#download(id) ⇒ Object
Putio::Client.download(id) Provides a URL for anyone to download a file.
262 263 264 265 266 267 |
# File 'lib/putio/client.rb', line 262 def download(id) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! PUTIO_BASE_URL + ("/files/%i/download?oauth_token=%s" % [id, @access_token]) end |
#file(id) ⇒ Object
Putio::Client.file(id) Returns the properties of a particular file.
180 181 182 183 184 185 186 187 188 |
# File 'lib/putio/client.rb', line 180 def file(id) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! response = make_get_call('/files/%i' % [id]) response.download = download(id) response end |
#files(folder = 0) ⇒ Object
Putio::Client.files(folder) Shows all the users files in a particular folder.
116 117 118 119 120 121 |
# File 'lib/putio/client.rb', line 116 def files(folder = 0) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/files/list?parent_id=%i' % [folder]).files end |
#friend_requests ⇒ Object
Putio::Client.friend_requests Shows pending friend requests of the authenticated user.
414 415 416 417 418 419 |
# File 'lib/putio/client.rb', line 414 def friend_requests # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/friends/waiting-requests').friends end |
#friends ⇒ Object
Putio::Client.friends Shows all the friends of the authenticated user.
402 403 404 405 406 407 |
# File 'lib/putio/client.rb', line 402 def friends # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/friends/list').friends end |
#make_friend_with(username) ⇒ Object
Putio::Client.make_friend_with(username) This sends a friend request to a particular user.
438 439 440 441 442 443 |
# File 'lib/putio/client.rb', line 438 def make_friend_with(username) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_post_call('/friends/%s/request' % [username]).status == "OK" end |
#me ⇒ Object
Putio::Client.me Shows information about the authenticated user.
378 379 380 381 382 383 |
# File 'lib/putio/client.rb', line 378 def me # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/account/info').info end |
#move(id, folder = 0) ⇒ Object
Putio::Client.move(id, folder) Move a file to another directory.
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/putio/client.rb', line 221 def move(id, folder = 0) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! # This provides support for an Array of ids. if id.is_a? Array then id = id.join(',') end make_post_call('/files/move?file_ids=%s&parent_id=%i' % [id, folder]).status == "OK" end |
#mp4(id) ⇒ Object
Putio::Client.mp4(id) Put.io offer MP4 conversion - call this method to see the status of the MP4 video.
250 251 252 253 254 255 |
# File 'lib/putio/client.rb', line 250 def mp4(id) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/files/%i/mp4' % [id]).mp4 end |
#oauth_complete(code) ⇒ Object
Putio::Client.oauth_complete(code) Provides an oauth_token used to authenticate API calls.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/putio/client.rb', line 89 def oauth_complete(code) # Let's compile the API URL we're calling. 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] # And call it. response = Curl::Easy.perform(url) do |req| req.headers['Accept'] = 'application/json' end # Use Crack to parse the JSON response = JSON.parse(response.body_str) # And use Hashie to present it. response = Hashie::Mash.new(response) # Save it locally. @access_token = response.access_token # Return it response end |
#oauth_url(response_type = 'code') ⇒ Object
Putio::Client.oauth_url(response_type) Provides a OAuth URL on Put.io for the user to grant access.
79 80 81 82 |
# File 'lib/putio/client.rb', line 79 def oauth_url(response_type = 'code') # The Redirect URI must be the same as registered with Put.io PUTIO_BASE_URL + "/oauth2/authenticate?client_id=%i&response_type=%s&redirect_uri=%s" % [@client_id, response_type, @redirect_uri] end |
#putio_methods ⇒ Object
Putio::Client.putio_methods Similar to .methods - you can use this to see all possible Put.io-related methods.
70 71 72 |
# File 'lib/putio/client.rb', line 70 def putio_methods [: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] end |
#rename(id, name) ⇒ Object
Putio::Client.rename(id, name) Renames a particular file.
210 211 212 213 214 215 |
# File 'lib/putio/client.rb', line 210 def rename(id, name) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_post_call('/files/rename?file_id=%i&name=%s' % [id, name]).status == "OK" end |
#search(query, page = 0) ⇒ Object
Putio::Client.search(query, page) Makes a file search against you and your shared files.
140 141 142 143 144 145 |
# File 'lib/putio/client.rb', line 140 def search(query, page = 0) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/files/list?parent_id=%i' % [folder]).files end |
#settings ⇒ Object
Putio::Client.settings Shows preferences of the authenticated user.
390 391 392 393 394 395 |
# File 'lib/putio/client.rb', line 390 def settings # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/account/settings').settings end |
#transfer(id) ⇒ Object
Putio::Client.transfer(id) Shows the status of a particular transfer.
328 329 330 331 332 333 |
# File 'lib/putio/client.rb', line 328 def transfer(id) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/transfers/%i' % [id]) end |
#transfers ⇒ Object
Putio::Client.transfers Shows all the current transfers.
292 293 294 295 296 297 |
# File 'lib/putio/client.rb', line 292 def transfers # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/transfers/list').transfers end |
#transfers_count ⇒ Object
Putio::Client.transfers_count Shows how many downloads are currently in the queue.
304 305 306 307 308 309 |
# File 'lib/putio/client.rb', line 304 def transfers_count # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! make_get_call('/transfers/count').count end |
#upload(file, folder = 0) ⇒ Object
Putio::Client.upload(file, folder) Upload a local file to Put.io
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/putio/client.rb', line 152 def upload(file, folder = 0) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! # Make the upload. response = make_upload_call('/files/upload?parent_id=%i' % [folder], file) # Return whatever. response.transfer || response.file end |
#zip(id) ⇒ Object
Putio::Client.zip(id) Provides a URL of a ZIP of multiple files.
274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/putio/client.rb', line 274 def zip(id) # Requires authorization raise PutioError::AuthorizationRequired if authentication_required! # This provides support for an Array of ids. if id.is_a? Array then id = id.join(',') end # Return zip download link PUTIO_BASE_URL + ("/files/zip?file_ids=%s&oauth_token=%s" % [id, @access_token]) end |