Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Hristo committed Mar 26, 2017
1 parent 89a1853 commit d5ba165
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 47 deletions.
41 changes: 33 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,61 @@ Then **bundle install** to install it
rails generate pcloud
```

which will generate config/pcloud.yml file to set user name and password
which will generate config/pcloud.yml file to set auth_type, user name and password or OAuth token
```
auth_type: plain/oauth
username: YOUR_USERNAME
password: YOUR_PASSWORD
access_token: YOUR_OAUTH_TOEKB
```

## Upload file to pcloud

```
Pcloud.upload(source_file_name, source_file_path, destination_path, destination_folder_id)
Pcloud.upload(source_file_name, source_file_path, destination_folder_id)
```

One of **destination_path** or **destination_folder_id** should present

## List folder from pcloud

```
Pcloud.list_folder(destination_path = nil, destination_folder_id = nil)
Pcloud.list_folder(destination_folder_id)
```

One of **destination_path** or **destination_folder_id** should present

## Create folder on pcloud

```
Pcloud.create_folder(destination_folder_id, folder_name)
```

Currently it works only with folder_id
## Create delete folder on pcloud

```
Pcloud.delete_folder(destination_folder_id)
```

## Create delete file on pcloud

```
Pcloud.delete_file(destination_file_id)
```

## Get file link on pcloud

```
Pcloud.get_file_link(destination_file_id)
```

## Download file by link

```
Pcloud.download_file(file_url, destination_path)
```

## Download file by id on pcloud

```
Pcloud.download_file_by_id(file_id, destination_path)
```
## License

Config is released under the [MIT License.](https://opensource.org/licenses/MIT)
1 change: 1 addition & 0 deletions lib/generators/pcloud.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
auth_type: AUTHENTICATION_TYPE
username: YOUR_USERNAME
password: YOUR_PASSWORD
48 changes: 44 additions & 4 deletions lib/pcloud.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
require 'rest-client'
require 'net/http'

require File.dirname(__FILE__) + '/pcloud/version'
require File.dirname(__FILE__) + '/pcloud/constants'
require File.dirname(__FILE__) + '/pcloud/authentication'
require File.dirname(__FILE__) + '/pcloud/upload'
require File.dirname(__FILE__) + '/pcloud/list_folder'
require File.dirname(__FILE__) + '/pcloud/delete_folder'
require File.dirname(__FILE__) + '/pcloud/delete_file'
require File.dirname(__FILE__) + '/pcloud/get_file_link'
require File.dirname(__FILE__) + '/pcloud/download_file'
require File.dirname(__FILE__) + '/pcloud/create_folder'

module Pcloud

def self.upload(source_file_name, source_file_path, destination_path = nil, destination_folder_id = nil)
response = Pcloud::Upload.new(destination_path, destination_folder_id, source_file_name, source_file_path).upload
def self.upload(source_file_name, source_file_path, destination_folder_id = nil)
response = Pcloud::Upload.new(destination_folder_id, source_file_name, source_file_path).upload

JSON.parse(response, { symbolize_names: true })
end

def self.list_folder(destination_path = nil, destination_folder_id = nil)
response = Pcloud::ListFolder.new(destination_path, destination_folder_id).list_folder
def self.list_folder(destination_folder_id)
response = Pcloud::ListFolder.new(destination_folder_id).list_folder

JSON.parse(response, { symbolize_names: true })
end
Expand All @@ -26,4 +31,39 @@ def self.create_folder(destination_folder_id, folder_name)

JSON.parse(response, { symbolize_names: true })
end

def self.delete_folder(destination_folder_id)
response = Pcloud::DeleteFolder.new(destination_folder_id).delete_folder

JSON.parse(response, { symbolize_names: true })
end

def self.delete_file(destination_file_id)
response = Pcloud::DeleteFile.new(destination_file_id).delete_file

JSON.parse(response, { symbolize_names: true })
end

def self.get_file_link(source_file_id)
response = Pcloud::GetFileLink.new(source_file_id).get_file_link

JSON.parse(response, { symbolize_names: true })
end

def self.download_file(source_file_url, destination_file_path)
response = Pcloud::DownloadFile.new(source_file_url, destination_file_path).download_file
end

def self.download_file_by_id(source_file_id, destination_file_path)
link_response = Pcloud::GetFileLink.new(source_file_id).get_file_link

file_response = JSON.parse(link_response, { symbolize_names: true })

if file_response[:result] == 0
file_url = "https://#{file_response[:hosts].first}#{file_response[:path]}"
Pcloud::DownloadFile.new(file_url, destination_file_path).download_file
else
raise StandardError, 'File link is invalid!'
end
end
end
57 changes: 49 additions & 8 deletions lib/pcloud/authentication.rb
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
9 changes: 4 additions & 5 deletions lib/pcloud/create_folder.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
module Pcloud
class CreateFolder

CREATE_FOLDER_URL = "#{Pcloud::Constants::BASE_URL}/createfolder"
CREATE_FOLDER_URL = "#{Pcloud::Constants::BASE_URL}/createfolder".freeze

attr_accessor :destination_folder_id, :folder_name
attr_reader :destination_folder_id, :folder_name, :auth

def initialize(destination_folder_id, folder_name)
@destination_folder_id = destination_folder_id
@folder_name = folder_name
@auth = Pcloud::Authentication.new
end

def create_folder
RestClient.post(CREATE_FOLDER_URL,
{
username: Pcloud::Authentication.username,
password: Pcloud::Authentication.password,
folderid: destination_folder_id,
name: folder_name
}
}.merge(auth.credentials)
)
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/pcloud/delete_file.rb
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
21 changes: 21 additions & 0 deletions lib/pcloud/delete_folder.rb
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
25 changes: 25 additions & 0 deletions lib/pcloud/download_file.rb
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
21 changes: 21 additions & 0 deletions lib/pcloud/get_file_link.rb
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
14 changes: 5 additions & 9 deletions lib/pcloud/list_folder.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
module Pcloud
class ListFolder
LIST_FOLDER_URL = "#{Pcloud::Constants::BASE_URL}/listfolder"

attr_accessor :destination_path, :destination_folder_id
LIST_FOLDER_URL = "#{Pcloud::Constants::BASE_URL}/listfolder".freeze

def initialize(destination_path = nil, destination_folder_id = nil)
raise ArgumentError, 'can not both parameters be nil' if destination_path.nil? && destination_folder_id.nil?
attr_reader :destination_folder_id, :auth

@destination_path = destination_path
def initialize(destination_folder_id)
@destination_folder_id = destination_folder_id
@auth = Pcloud::Authentication.new
end

def list_folder
RestClient.post(LIST_FOLDER_URL,
{
username: Pcloud::Authentication.username,
password: Pcloud::Authentication.password,
path: destination_path,
folderid: destination_folder_id
}
}.merge(auth.credentials)
)
end
end
Expand Down
Loading

0 comments on commit d5ba165

Please sign in to comment.