-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hristo
committed
Mar 26, 2017
1 parent
89a1853
commit d5ba165
Showing
13 changed files
with
233 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
auth_type: AUTHENTICATION_TYPE | ||
username: YOUR_USERNAME | ||
password: YOUR_PASSWORD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,58 @@ | ||
module Pcloud | ||
class Authentication | ||
|
||
def self.username | ||
@username ||= yaml_settings['username'] | ||
|
||
PLAIN = 'plain'.freeze | ||
OAUTH = 'oauth'.freeze | ||
TYPES = [PLAIN, OAUTH].freeze | ||
|
||
attr_accessor :auth_type, :username, :password, :access_token | ||
|
||
def initialize | ||
@auth_type = yaml_settings['auth_type'] | ||
|
||
raise_invalid_auth_type unless valid_auth_type? | ||
|
||
assign_auth_params | ||
end | ||
|
||
def credentials | ||
return { auth: access_token } unless plain_auth? | ||
|
||
{ | ||
username: username, | ||
password: password | ||
} | ||
end | ||
|
||
private | ||
|
||
def plain_auth? | ||
self.auth_type == PLAIN | ||
end | ||
|
||
def raise_invalid_auth_type | ||
raise ArgumentError, 'invalid authentication type' | ||
end | ||
|
||
def valid_auth_type? | ||
TYPES.include?(auth_type) | ||
end | ||
|
||
def assign_auth_params | ||
send("#{auth_type}_params") | ||
end | ||
|
||
def plain_params | ||
@username = yaml_settings['username'] | ||
@password = yaml_settings['password'] | ||
end | ||
|
||
def self.password | ||
@password ||= yaml_settings['password'] | ||
def oauth_params | ||
@access_token = yaml_settings['access_token'] | ||
end | ||
|
||
def self.yaml_settings | ||
file_path = File.join(Rails.root, 'config', 'pcloud.yml') | ||
YAML.load_file(file_path) | ||
def yaml_settings | ||
@yaml_settings ||= YAML.load_file(File.join(Rails.root, 'config', 'pcloud.yml')) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Pcloud | ||
class DeleteFile | ||
|
||
DELETE_FILE_URL = "#{Pcloud::Constants::BASE_URL}/deletefile".freeze | ||
|
||
attr_reader :destination_file_id, :auth | ||
|
||
def initialize(destination_file_id) | ||
@destination_file_id = destination_file_id | ||
@auth = Pcloud::Authentication.new | ||
end | ||
|
||
def delete_file | ||
RestClient.post(DELETE_FILE_URL, | ||
{ | ||
fileid: destination_file_id | ||
}.merge(auth.credentials) | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Pcloud | ||
class DeleteFolder | ||
|
||
DELETE_FOLDER_URL = "#{Pcloud::Constants::BASE_URL}/deletefolderrecursive".freeze | ||
|
||
attr_reader :destination_folder_id, :auth | ||
|
||
def initialize(destination_folder_id) | ||
@destination_folder_id = destination_folder_id | ||
@auth = Pcloud::Authentication.new | ||
end | ||
|
||
def delete_folder | ||
RestClient.post(DELETE_FOLDER_URL, | ||
{ | ||
folderid: destination_folder_id | ||
}.merge(auth.credentials) | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Pcloud | ||
class DownloadFile | ||
|
||
attr_reader :source_file_link, :destination_file_path, :auth | ||
|
||
def initialize(source_file_link, destination_file_path) | ||
@source_file_link = source_file_link | ||
@destination_file_path = destination_file_path | ||
@auth = Pcloud::Authentication.new | ||
end | ||
|
||
def download_file | ||
uri = URI(source_file_link) | ||
response = Net::HTTP.get_response(uri) | ||
|
||
case response | ||
when Net::HTTPSuccess | ||
file = "#{destination_file_path}/#{source_file_link.split('/').last}" | ||
File.open(file, 'wb') { |content| content.write(response.body) } | ||
else | ||
raise StandardError, 'Something went wrong :(' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Pcloud | ||
class GetFileLink | ||
|
||
GET_FILE_URL = "#{Pcloud::Constants::BASE_URL}/getfilelink".freeze | ||
|
||
attr_reader :source_file_id, :auth | ||
|
||
def initialize(source_file_id) | ||
@source_file_id = source_file_id | ||
@auth = Pcloud::Authentication.new | ||
end | ||
|
||
def get_file_link | ||
RestClient.post(GET_FILE_URL, | ||
{ | ||
fileid: source_file_id | ||
}.merge(auth.credentials) | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.