Class: GFClient
- Inherits:
-
Object
- Object
- GFClient
- Defined in:
- lib/gofile_ruby.rb
Overview
A wrapper for the GoFile API, containing methods for each available endpoint.
Instance Attribute Summary collapse
-
#account_details ⇒ Object
readonly
Returns the value of attribute account_details.
-
#is_guest ⇒ Object
readonly
Returns the value of attribute is_guest.
Instance Method Summary collapse
-
#authenticate ⇒ Hash
Calls the #get_account_details method and saves it to the @account_details instance variable.
-
#copy_content(contents_id:, destination_id:) ⇒ Hash
Copies one or multiple contents to destination folder.
-
#create_folder(parent_id: nil, folder_name:) ⇒ Hash
Creates a folder with the given folder name and parent ID.
-
#delete_content(contents_id:) ⇒ Hash
Delete one or multiple contents.
-
#get_account_details ⇒ Hash
Will return the details of the current account.
-
#get_children(parent_id: nil) ⇒ Hash
Gets the children of a specific folder.
-
#get_server ⇒ Hash
Retreives the best available server for uploading files.
-
#initialize(token: nil, guest: false) ⇒ GFClient
constructor
Creates a new instance of the GFClient class.
-
#is_guest? ⇒ Boolean
Alias for getter method.
-
#set_folder_option(folder_id:, option:, value:) ⇒ Hash
Sets the options for a specific folder.
-
#set_options_hash(folder_id:, options_hash:) {|Hash| ... } ⇒ Object
Helper function for #set_folder_option.
-
#upload_file(file:, folder_id: nil) ⇒ Hash
Uploads a file to the given destination folder.
Constructor Details
#initialize(token: nil, guest: false) ⇒ GFClient
Creates a new instance of the GFClient class.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gofile_ruby.rb', line 12 def initialize(token:nil, guest:false) # The GoFile API token, defaults to nil if none is provided. @token = token # A GFClient instance that is in guest mode (where no token is provided) will set @has_token to false, and @is_guest to true. # The @has_token instance variable will be set to true after the user acquires a guest account token by uploading a file in guest mode. @has_token = !@token.nil? # Instance variable for tracking if the client is using a guest account or not @is_guest = guest # Account details, initially set to `nil` until the user calls the `#authenticate` method @account_details = nil # The root folder returned by the `#upload_file` method when called in guest mode (without a token). # Gets set after a guest account uploads their first file. # Is used only when uploading files as a guest. @guest_upload_destination = nil # Final checks to ensure the user doesn't try anything odd. (Eg. passing in a token while also setting guest mode to true) validate_guest_mode end |
Instance Attribute Details
#account_details ⇒ Object (readonly)
Returns the value of attribute account_details.
8 9 10 |
# File 'lib/gofile_ruby.rb', line 8 def account_details @account_details end |
#is_guest ⇒ Object (readonly)
Returns the value of attribute is_guest.
8 9 10 |
# File 'lib/gofile_ruby.rb', line 8 def is_guest @is_guest end |
Instance Method Details
#authenticate ⇒ Hash
Calls the #get_account_details method and saves it to the @account_details instance variable
Response is same as #get_account_details method
300 301 302 303 |
# File 'lib/gofile_ruby.rb', line 300 def authenticate acc_deatils = get_account_details @account_details = acc_deatils end |
#copy_content(contents_id:, destination_id:) ⇒ Hash
This method will be tested at a later time due to it being a premium-only endpoint.
This method is premium only! You will not be able to use it unless you have a premium account!
Copies one or multiple contents to destination folder.
Destination ID: String
Contents ID: String A string of comma separated content ID’s. (“id1,id2,id3”) Response example:
"status": "ok",
"data": {}
239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/gofile_ruby.rb', line 239 def copy_content(contents_id:, destination_id:) copy_url = "https://api.gofile.io/copyContent" body = { "contentsId": contents_id, "folderIdDest": destination_id, "token": @token } ret = HTTPHelper.put(copy_url, body) ret end |
#create_folder(parent_id: nil, folder_name:) ⇒ Hash
Creates a folder with the given folder name and parent ID. If a parent ID is not provided, it default to the root folder.
When using guest mode, you cannot call this method until you’ve uploaded a file first, as the guest token and root folder ID won’t be available.
Example response:
"status": "ok",
"data": {
}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/gofile_ruby.rb', line 108 def create_folder(parent_id:nil, folder_name:) raise "Cannot create folders without a token! Please upload a file first!" unless @has_token raise "Cannot use root folder without authenticating first" unless @account_details post_folder_url = "https://api.gofile.io/createFolder" parent_id = @account_details["data"]["rootFolder"] unless parent_id folder_data = { "parentFolderId" => parent_id, "folderName" => folder_name, "token" => @token } ret = HTTPHelper.put(post_folder_url, folder_data) ret end |
#delete_content(contents_id:) ⇒ Hash
Delete one or multiple contents.
Response example:
"status": "ok",
"data": {}
260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/gofile_ruby.rb', line 260 def delete_content(contents_id:) delete_url = "https://api.gofile.io/deleteContent" body = { "contentsId": contents_id, "token": @token } ret = HTTPHelper.delete(delete_url, body) ret end |
#get_account_details ⇒ Hash
Will return the details of the current account.
Response example:
"status": "ok",
"data": {
"token": "ivlW1ZSGn2Y4AoADbCHUjllj2cO9m3WM",
"email": "[email protected]",
"tier": "standard",
"rootFolder": "2aecea58-84e6-420d-b2b9-68b4add8418d",
"foldersCount": 4,
"filesCount": 20,
"totalSize": 67653500,
"totalDownloadCount": 1
}
288 289 290 291 292 293 |
# File 'lib/gofile_ruby.rb', line 288 def get_account_details account_details_url = "https://api.gofile.io/getAccountDetails?token=#{@token}" details = HTTPHelper.get(account_details_url) details end |
#get_children(parent_id: nil) ⇒ Hash
This method will be tested at a later time due to it being a premium-only endpoint.
This method is premium only! You will not be able to use it unless you have a premium account!
Gets the children of a specific folder.
Defaults to root folder if a parent ID is not provided.
Response example:
"status": "ok",
"data": {
"isOwner": true,
"id": "3dbc2f87-4c1e-4a81-badc-af004e61a5b4",
"type": "folder",
"name": "Z19n9a",
"parentFolder": "3241d27a-f7e1-4158-bc75-73d057eff5fa",
"code": "Z19n9a",
"createTime": 1648229689,
"public": true,
"childs": [
"4991e6d7-5217-46ae-af3d-c9174adae924"
],
"totalDownloadCount": 0,
"totalSize": 9840497,
"contents": {
"4991e6d7-5217-46ae-af3d-c9174adae924": {
"id": "4991e6d7-5217-46ae-af3d-c9174adae924",
"type": "file",
"name": "example.mp4",
"parentFolder": "3dbc2f87-4c1e-4a81-badc-af004e61a5b4",
"createTime": 1648229689,
"size": 9840497,
"downloadCount": 0,
"md5": "10c918b1d01aea85864ee65d9e0c2305",
"mimetype": "video/mp4",
"serverChoosen": "store4",
"directLink": "https://store4.gofile.io/download/direct/4991e6d7-5217-46ae-af3d-c9174adae924/example.mp4",
"link": "https://store4.gofile.io/download/4991e6d7-5217-46ae-af3d-c9174adae924/example.mp4"
}
}
}
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/gofile_ruby.rb', line 167 def get_children(parent_id:nil) raise "Cannot use the #get_children method without a token!" if !@has_token parent = @account_details["data"]["rootFolder"] if !parent content_url = "https://api.gofile.io/getContent?contentId=#{parent}&token=#{@token}" ret = HTTPHelper.get(content_url) ret end |
#get_server ⇒ Hash
Retreives the best available server for uploading files
Example response:
"status": "ok",
"data": {
"server": "store1"
}
42 43 44 45 |
# File 'lib/gofile_ruby.rb', line 42 def get_server server_url = "https://api.gofile.io/getServer" HTTPHelper.get(server_url) end |
#is_guest? ⇒ Boolean
Alias for getter method.
308 309 310 |
# File 'lib/gofile_ruby.rb', line 308 def is_guest? @is_guest end |
#set_folder_option(folder_id:, option:, value:) ⇒ Hash
Sets the options for a specific folder.
The expected option and value types are listed below.
public: Boolean
password: String
description: String
expire: Unix Timestamp
tags: String (String of comma separated tags, Eg. “tag1,tag2,tag3”)
Response example:
"status": "ok",
"data": {}
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/gofile_ruby.rb', line 199 def set_folder_option(folder_id:, option:, value:) = "https://api.gofile.io/setFolderOption" body = { "option": option, "value": value, "folderId": folder_id, "token": @token } ret = HTTPHelper.put(, body) ret end |
#set_options_hash(folder_id:, options_hash:) {|Hash| ... } ⇒ Object
Helper function for #set_folder_option
218 219 220 221 222 223 |
# File 'lib/gofile_ruby.rb', line 218 def (folder_id:, options_hash:, &block) .each do |k, v| res = set_folder_option(folder_id: folder_id, option: k, value: v) yield res end end |
#upload_file(file:, folder_id: nil) ⇒ Hash
Uploads a file to the given destination folder.
If using guest mode, you cannot specify a folder ID when uploading your first file.
If you’re uploading multiple files, you have to use the parent ID along with the token from the response object in your subsequent uploads.
To get around this issue, gofile_ruby saves the newly returned token and parent ID after your first upload.
This means that you can call the #upload_file method multiple times without having to deal with authentication.
Example response:
"status": "ok",
"data": {
"guestToken": "a939kv5b43c03192imatoken2949" (If uploaded without a token)
"downloadPage": "https://gofile.io/d/Z19n9a",
"code": "Z19n9a",
"parentFolder": "3dbc2f87-4c1e-4a81-badc-af004e61a5b4",
"fileId": "4991e6d7-5217-46ae-af3d-c9174adae924",
"fileName": "example.mp4",
"md5": "10c918b1d01aea85864ee65d9e0c2305"
}
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gofile_ruby.rb', line 71 def upload_file(file:, folder_id: nil) raise "Guests cannot specify folder ID before their first upload!" if folder_id && @is_guest best_server = get_server()["data"]["server"] upload_url = "https://#{best_server}.gofile.io/uploadFile" body = [["file", file]] body << ["token", @token] if @has_token # If user inputs a folder_id while they have a token if folder_id && @has_token # add the ID to the body of the request body << ["folderId", folder_id] # If the user has uploaded a file as a guest, and a folder id hasn't been provided, use the folder ID returned from the first file upload elsif @guest_upload_destination && !folder_id body << ["folderId", @guest_upload_destination] end ret = HTTPHelper.post_multipart_data(upload_url, body) save_guest_acc_details(ret) if @has_token ret end |