pcloud_api 0.1.0 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +29 -2
- data/README.md +67 -28
- data/Rakefile +1 -1
- data/lib/pcloud/client.rb +3 -3
- data/lib/pcloud/file.rb +41 -39
- data/lib/pcloud/folder.rb +27 -18
- data/lib/pcloud/time_helper.rb +28 -0
- data/lib/pcloud/version.rb +1 -1
- data/lib/pcloud_api.rb +2 -0
- data/pcloud_api.gemspec +4 -3
- metadata +25 -11
- data/.travis.yml +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fcef0e50f4deec3483d2ce4f23882a453ed3cea1fac57ef507ee1523824aba33
|
4
|
+
data.tar.gz: 287a6037d5f81652a936d611a69cd2b40dd4ce5cdfbde03093242e0bcc7e061a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9cc930698584cfebe1fbb501798cb15ccb8116e8d231f632c30813f37bc0912550cee1d3bcdfee66fd21add00e8d2874b8471173ca4c77b67a544b7efe2740a
|
7
|
+
data.tar.gz: ecdc92c74476eaed41515b5206d5c428f17fcddf870cd28f162fd382ba97abd11d095ef36ded8f155a188a36fe7a32afc4703df7275a779ec37628281de3da58
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,31 @@
|
|
1
|
+
## 0.2.3 2021-12-14
|
2
|
+
|
3
|
+
**Changes**
|
4
|
+
1. `Pcloud::File`'s `upload` method no longer requires a `:filename` param, since pCloud just reads it off of the file object and ignores the param anyway
|
5
|
+
2. Both `Pcloud::File` and `Pcloud::Folder`'s `update` and `update!` methods now allow either partial paths _(starting and ending with slashes)_ or full paths. This is a little more dangerous if you specify a full path and you meant partial, but it's a reasonable use case to support.
|
6
|
+
|
7
|
+
## 0.2.2 2021-10-09
|
8
|
+
|
9
|
+
**Changes**
|
10
|
+
1. After adding GitHub actions, I discovered that the gem did not in fact work on all the versions of Ruby that I had thought it supported. This update makes some small tweaks to bring support to Ruby 2.4 then updates the `.gemspec` to clarify that 2.3 is not in fact supported. All current users of the gem should see no behavior changes as a result of this update 👍🏻
|
11
|
+
|
12
|
+
## 0.2.1 2021-10-07
|
13
|
+
|
14
|
+
**Changes**
|
15
|
+
1. Simplifying the errors returned from `Pcloud::Folder` and `Pcloud::File`. This is purely a cleanup release, unless your client is explicitly checking error strings there should be no noticeable behavior change 👍🏻
|
16
|
+
|
17
|
+
## 0.2.0 2021-10-07
|
18
|
+
|
19
|
+
**New**
|
20
|
+
1. An `.exists?` method has been added to `Pcloud::Folder` and `Pcloud::File`
|
21
|
+
2. The `.find_by` method on `Pcloud::Folder` and `Pcloud::File` can now be called with either `:id` or `:path` parameters.
|
22
|
+
* NOTE: pCloud treats `:id` with precedence, so an error will be raised if the method is called with both parameters at once. _(Allowing precedence in a method like this would likely feel like unexpected behavior, so my intention was to make it immediately obvious to the end user.)_
|
23
|
+
|
24
|
+
**Changes**
|
25
|
+
1. The `created_at` and `modified_at` timestamps on `Pcloud::Folder` and `Pcloud::File` are now returned as Ruby `Time` objects _(specifically [`TZInfo::TimeWithOffset`](https://www.rubydoc.info/gems/tzinfo/TZInfo/TimeWithOffset))_.
|
26
|
+
* NOTE: if you were previously parsing the string timestamps out into time objects, you may see errors like `no implicit conversion of TZInfo::TimeWithOffset into String`
|
27
|
+
2. Some error class names and messages have been cleaned up for additional clarity and consistency.
|
28
|
+
|
1
29
|
## 0.1.0 2021-09-26
|
2
30
|
|
3
|
-
|
4
|
-
* Initial release
|
31
|
+
**Initial release** 🍰 🎉
|
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# pCloud API
|
2
2
|
|
3
|
+
[![CI](https://github.com/jhunschejones/pcloud_api/actions/workflows/ci.yml/badge.svg)](https://github.com/jhunschejones/pcloud_api/actions/workflows/ci.yml)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/pcloud_api.svg)](https://badge.fury.io/rb/pcloud_api)
|
5
|
+
[![Gem Downloads](https://badgen.net/rubygems/dt/pcloud_api)](https://rubygems.org/gems/pcloud_api)
|
6
|
+
|
3
7
|
The `pcloud_api` gem provides an intuitive Ruby interface for interacting with the [pCloud API](https://docs.pcloud.com/) using OAuth2. This gem does not attempt to replicate the entire functionality of the pCloud API but rather to provide quick and easy access for its most basic and common functionality. If you are looking for a lower-level pCloud API wrapper, [`pcloud`](https://github.com/7urkm3n/pcloud) might be a better fit for you.
|
4
8
|
|
5
9
|
## Installation
|
@@ -7,7 +11,7 @@ The `pcloud_api` gem provides an intuitive Ruby interface for interacting with t
|
|
7
11
|
Add this line to your application's Gemfile:
|
8
12
|
|
9
13
|
```ruby
|
10
|
-
gem
|
14
|
+
gem "pcloud_api"
|
11
15
|
```
|
12
16
|
|
13
17
|
And then execute:
|
@@ -18,15 +22,25 @@ Or install it yourself as:
|
|
18
22
|
|
19
23
|
$ gem install pcloud_api
|
20
24
|
|
25
|
+
## Requirements
|
26
|
+
|
27
|
+
* Ruby 2.4.0 or higher
|
28
|
+
* `httparty`
|
29
|
+
* `tzinfo`
|
30
|
+
|
21
31
|
## Usage
|
22
32
|
|
33
|
+
### Generating an access token
|
34
|
+
|
35
|
+
To use the `pcloud_api` client, you will need to first generate an access token. You may do this by cloning and running the script in [`./bin/generate_access_token`](https://github.com/jhunschejones/pcloud_api/blob/master/bin/generate_access_token). It will take you through the process of setting up a pCloud app and completing the OAuth2 flow in the browser.
|
36
|
+
|
23
37
|
### Configuration
|
24
38
|
|
25
39
|
The `Pcloud::Client` can be configured by directly calling the `Pcloud::Client.configure` method in an initializer or somewhere else in your code at startup:
|
26
40
|
```ruby
|
27
41
|
Pcloud::Client.configure(
|
28
|
-
|
29
|
-
|
42
|
+
access_token: "your-pcloud-app-access-token",
|
43
|
+
data_region: "EU"
|
30
44
|
)
|
31
45
|
```
|
32
46
|
|
@@ -38,38 +52,50 @@ There are two main objects represented in the library, `Pcloud::File` and `Pclou
|
|
38
52
|
|
39
53
|
The `Pcloud::File` API includes:
|
40
54
|
```ruby
|
41
|
-
# Find files by file id or path:
|
55
|
+
# Find files by file :id or :path:
|
42
56
|
Pcloud::File.find(1)
|
43
57
|
Pcloud::File.find_by(path: "/images/jack_the_cat.jpg")
|
58
|
+
# NOTE: `find_by` can also be used with :id, though this will take precedence
|
59
|
+
# over :path so just pick one or the other
|
60
|
+
|
61
|
+
# Check if a file exists by :id
|
62
|
+
Pcloud::File.exists?(1)
|
44
63
|
|
45
|
-
# Upload a new file, rename if already
|
64
|
+
# Upload a new file, rename if one already exists with this name:
|
46
65
|
Pcloud::File.upload(
|
47
|
-
|
48
|
-
|
49
|
-
file: File.open("/Users/joshua/Downloads/jack_goes_swimming.mp4")
|
66
|
+
path: "/Jack",
|
67
|
+
file: File.open("/Users/joshua/Downloads/jack_goes_swimming.mp4")
|
50
68
|
)
|
51
|
-
# NOTE: the upload method will allow you to specify either the `path` or the
|
52
|
-
# `folder_id`, or you can choose to pass neither paramenter and your files will
|
53
|
-
# be placed in your root pCloud directory.
|
54
69
|
|
55
|
-
# Upload a file, force overwrite
|
70
|
+
# Upload a file, force overwrite if a file already exists with this name:
|
56
71
|
Pcloud::File.upload!(
|
57
|
-
|
58
|
-
|
59
|
-
file: File.open("/Users/joshua/Downloads/jack_goes_swimming.mp4")
|
72
|
+
path: "/Jack",
|
73
|
+
file: File.open("/Users/joshua/Downloads/jack_goes_swimming.mp4")
|
60
74
|
)
|
61
75
|
|
76
|
+
# NOTE:
|
77
|
+
# The `upload` and `upload!` methods will allow you to specify either the :path
|
78
|
+
# or the :folder_id of the target parent directory, or you can choose to pass
|
79
|
+
# neither paramenter and your files will be placed in your root pCloud
|
80
|
+
# directory by default.
|
81
|
+
|
62
82
|
# Rename a file:
|
63
83
|
jack_goes_swimming.update(name: "That's one wet cat.mp4")
|
64
84
|
|
65
|
-
# Move a file by updating the parent_folder_id
|
85
|
+
# Move a file by updating the :parent_folder_id:
|
66
86
|
jack_goes_swimming.update(parent_folder_id: 9000)
|
67
87
|
|
88
|
+
# Move a file by specifying the parent folder part of the path:
|
89
|
+
jack_goes_swimming.update(path: "/photos/") # NOTE: needs to start and end with slashes
|
90
|
+
|
91
|
+
# Move a file by specifying the entire new file path:
|
92
|
+
jack_goes_swimming.update(path: "/photos/jack_goes_swimming.mp4")
|
93
|
+
|
68
94
|
# Delete a file:
|
69
95
|
jack_goes_swimming.delete
|
70
96
|
|
71
97
|
# Get a link to download a file:
|
72
|
-
jack_goes_swimming.
|
98
|
+
jack_goes_swimming.download_url
|
73
99
|
|
74
100
|
# Access the parent folder of a file:
|
75
101
|
jack_goes_swimming.parent_folder
|
@@ -77,22 +103,37 @@ jack_goes_swimming.parent_folder
|
|
77
103
|
|
78
104
|
The `Pcloud::Folder` API is very similar:
|
79
105
|
```ruby
|
80
|
-
# Find folders by folder id or path:
|
106
|
+
# Find folders by folder :id or :path:
|
81
107
|
Pcloud::Folder.find(1)
|
82
108
|
Pcloud::Folder.find_by(path: "/images")
|
109
|
+
# NOTE: `find_by` can also be used with :id, though this will take precedence
|
110
|
+
# over :path so just pick one or the other
|
111
|
+
|
112
|
+
# Check if a folder exists by id
|
113
|
+
Pcloud::Folder.exists?(1)
|
83
114
|
|
84
|
-
# Create a new folder by parent_folder_id and name:
|
115
|
+
# Create a new folder by :parent_folder_id and :name:
|
85
116
|
Pcloud::Folder.first_or_create(parent_folder_id: 9000, name: "jack")
|
86
117
|
|
87
|
-
# Create a new folder by path:
|
118
|
+
# Create a new folder by :path (parent directory must already exist):
|
88
119
|
Pcloud::Folder.first_or_create(path: "/images/jack")
|
89
120
|
|
90
121
|
# Rename a folder:
|
91
122
|
jack_images.update(name: "Jack's Photo Library")
|
92
123
|
|
93
|
-
# Move a folder by updating the parent_folder_id
|
124
|
+
# Move a folder by updating the :parent_folder_id:
|
94
125
|
jack_images.update(parent_folder_id: 9000)
|
95
126
|
|
127
|
+
# Move a folder by specifying the parent folder part of the path:
|
128
|
+
jack_images.update(path: "/photos/")
|
129
|
+
# NOTES:
|
130
|
+
# - Partial paths must start and end with slashes
|
131
|
+
# - All refrenced parent directories in the path must already exist
|
132
|
+
|
133
|
+
# Move a folder by specifying the entire new file path:
|
134
|
+
jack_images.update(path: "/photos/images/jack")
|
135
|
+
# NOTE: All refrenced parent directories in the path must already exist
|
136
|
+
|
96
137
|
# Delete an empty folder:
|
97
138
|
jack_images.delete
|
98
139
|
|
@@ -106,20 +147,18 @@ jack_images.parent_folder
|
|
106
147
|
jack_images.contents
|
107
148
|
```
|
108
149
|
|
109
|
-
**Aside: path vs id
|
150
|
+
**Aside: path vs id**
|
151
|
+
|
110
152
|
pCloud recommends using the `folder_id`, `parent_folder_id` or `file_id` params for API calls whenever possible, rather using an exact path. Folder and file ids are static, so this will make your code less brittle to changes in the file/folder tree. You can simply look up the id for a folder in the console ahead of time and then set it in your code, similar to how you would specify an AWS S3 bucket.
|
111
153
|
|
112
154
|
|
113
|
-
**Aside: off-label client use
|
155
|
+
**Aside: off-label client use**
|
156
|
+
|
114
157
|
The `Pcloud::File` and `Pcloud::Folder` APIs cover the most important, common functionality developers will want to access in a way that is easy to use without much onboarding. If you find that you still need to access other parts of the pCloud API that are not included in this gem yet, you can try calling other methods specified in [the pCloud API docs](https://docs.pcloud.com/) by interacting directly with the `Pcloud::Client`:
|
115
158
|
```ruby
|
116
159
|
Pcloud::Client.execute("listrevisions", query: { fileid: 90000 })
|
117
160
|
```
|
118
|
-
_(There are a few methods on the raw pCloud API that require manual login, which this gem does not yet support. If you find that you need access to these methods you may wish to look at using the [`pcloud`](https://github.com/7urkm3n/pcloud) gem instead.)
|
119
|
-
|
120
|
-
### Generating an access token
|
121
|
-
|
122
|
-
To use the `pcloud_api` client, you will need to first generate an access token. You may do this by cloning and running the script in `./bin/generate_access_token`. It will take you through the process of setting up a pCloud app and completing the OAuth2 flow in the browser.
|
161
|
+
_(There are a few methods on the raw pCloud API that require manual login, which this gem does not yet support. If you find that you need access to these methods you may wish to look at using the [`pcloud`](https://github.com/7urkm3n/pcloud) gem instead.)_
|
123
162
|
|
124
163
|
## Development
|
125
164
|
|
data/Rakefile
CHANGED
data/lib/pcloud/client.rb
CHANGED
@@ -24,8 +24,8 @@ module Pcloud
|
|
24
24
|
headers: { "Authorization" => "Bearer #{access_token}" },
|
25
25
|
timeout: TIMEOUT_SECONDS # this sets both the open and read timeouts to the same value
|
26
26
|
}
|
27
|
-
options
|
28
|
-
options
|
27
|
+
options[:query] = query unless query.empty?
|
28
|
+
options[:body] = body unless body.empty?
|
29
29
|
response = HTTParty.public_send(verb, "https://#{closest_server}/#{method}", options)
|
30
30
|
json_response = JSON.parse(response.body)
|
31
31
|
raise ErrorResponse.new(json_response["error"]) if json_response["error"]
|
@@ -44,7 +44,7 @@ module Pcloud
|
|
44
44
|
def access_token
|
45
45
|
@@access_token ||= ENV["PCLOUD_API_ACCESS_TOKEN"]
|
46
46
|
return @@access_token unless @@access_token.nil?
|
47
|
-
raise ConfigurationError.new("Missing
|
47
|
+
raise ConfigurationError.new("Missing pCloud API access token")
|
48
48
|
end
|
49
49
|
|
50
50
|
# You can manually hit "https://<default_server_for_your_region>/getapiserver"
|
data/lib/pcloud/file.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
module Pcloud
|
2
2
|
class File
|
3
|
-
class UnsuportedUpdateParams < StandardError; end
|
4
|
-
class ManformedUpdateParams < StandardError; end
|
5
3
|
class InvalidParameter < StandardError; end
|
4
|
+
class InvalidParameters < StandardError; end
|
5
|
+
class MissingParameter < StandardError; end
|
6
6
|
class UploadFailed < StandardError; end
|
7
|
+
|
7
8
|
include Parser
|
9
|
+
include Pcloud::TimeHelper
|
8
10
|
|
9
11
|
SUPPORTED_UPDATE_PARAMS = [:name, :parent_folder_id, :path].freeze
|
12
|
+
SUPPORTED_FIND_BY_PARAMS = [:id, :path].freeze
|
10
13
|
FILE_CATAGORIES = {
|
11
14
|
"0" => "uncategorized",
|
12
15
|
"1" => "image",
|
@@ -28,16 +31,16 @@ module Pcloud
|
|
28
31
|
@size = params.fetch(:size) # bytes
|
29
32
|
@parent_folder_id = params.fetch(:parent_folder_id)
|
30
33
|
@is_deleted = params.fetch(:is_deleted) || false
|
31
|
-
@created_at = params.fetch(:created_at)
|
32
|
-
@modified_at = params.fetch(:modified_at)
|
34
|
+
@created_at = time_from(params.fetch(:created_at))
|
35
|
+
@modified_at = time_from(params.fetch(:modified_at))
|
33
36
|
end
|
34
37
|
|
35
38
|
def update(params)
|
36
39
|
unless (params.keys - SUPPORTED_UPDATE_PARAMS).empty?
|
37
|
-
raise
|
40
|
+
raise InvalidParameters.new("Must be one of #{SUPPORTED_UPDATE_PARAMS}")
|
38
41
|
end
|
39
|
-
if params[:path] &&
|
40
|
-
raise
|
42
|
+
if params[:path] && params[:path][0] != "/"
|
43
|
+
raise InvalidParameter.new(":path param must start with `/`")
|
41
44
|
end
|
42
45
|
query = {
|
43
46
|
fileid: id,
|
@@ -70,45 +73,41 @@ module Pcloud
|
|
70
73
|
"#{@download_url}/#{URI.encode_www_form_component(name)}"
|
71
74
|
end
|
72
75
|
|
73
|
-
private
|
74
|
-
|
75
|
-
def is_invalid_path_param?(path_param)
|
76
|
-
# Path params have to start and end with `/``
|
77
|
-
[path_param[0], path_param[-1]] != ["/", "/"]
|
78
|
-
end
|
79
|
-
|
80
76
|
class << self
|
77
|
+
def exists?(id)
|
78
|
+
find(id)
|
79
|
+
true
|
80
|
+
rescue Pcloud::Client::ErrorResponse => e
|
81
|
+
return false if e.message == "File not found."
|
82
|
+
raise e
|
83
|
+
end
|
84
|
+
|
81
85
|
def find(id)
|
82
86
|
parse_one(Client.execute("stat", query: { fileid: id }))
|
83
87
|
end
|
84
88
|
|
85
|
-
def find_by(
|
86
|
-
|
89
|
+
def find_by(params)
|
90
|
+
unless (params.keys - SUPPORTED_FIND_BY_PARAMS).empty?
|
91
|
+
raise InvalidParameters.new("Must be one of #{SUPPORTED_FIND_BY_PARAMS}")
|
92
|
+
end
|
93
|
+
raise InvalidParameters.new(":id takes precedent over :path, please only use one or the other") if params[:path] && params[:id]
|
94
|
+
query = { path: params[:path], fileid: params[:id] }.compact
|
95
|
+
parse_one(Client.execute("stat", query: query))
|
87
96
|
end
|
88
97
|
|
89
|
-
def upload(
|
90
|
-
process_upload(
|
91
|
-
filename: filename,
|
92
|
-
file: file,
|
93
|
-
path: path,
|
94
|
-
folder_id: folder_id
|
95
|
-
)
|
98
|
+
def upload(params)
|
99
|
+
process_upload(params)
|
96
100
|
end
|
97
101
|
|
98
|
-
def upload!(
|
99
|
-
process_upload(
|
100
|
-
filename: filename,
|
101
|
-
file: file,
|
102
|
-
path: path,
|
103
|
-
folder_id: folder_id,
|
104
|
-
overwrite: true
|
105
|
-
)
|
102
|
+
def upload!(params)
|
103
|
+
process_upload(params.merge({ overwrite: true }))
|
106
104
|
end
|
107
105
|
|
108
106
|
private
|
109
107
|
|
110
|
-
def process_upload(
|
111
|
-
|
108
|
+
def process_upload(params)
|
109
|
+
file = params.fetch(:file)
|
110
|
+
raise InvalidParameter.new("The :file parameter must be an instance of Ruby `File`") unless file.is_a?(::File)
|
112
111
|
|
113
112
|
# === pCloud API behavior notes: ===
|
114
113
|
# 1. If neither `path` nor `folder_id` is provided, the file will be
|
@@ -118,11 +117,11 @@ module Pcloud
|
|
118
117
|
response = Client.execute(
|
119
118
|
"uploadfile",
|
120
119
|
body: {
|
121
|
-
renameifexists: overwrite ? 0 : 1,
|
122
|
-
path: path,
|
123
|
-
folderid: folder_id,
|
124
|
-
filename: filename,
|
125
|
-
file: file
|
120
|
+
renameifexists: params[:overwrite] ? 0 : 1,
|
121
|
+
path: params[:path],
|
122
|
+
folderid: params[:folder_id],
|
123
|
+
filename: params[:filename],
|
124
|
+
file: file,
|
126
125
|
}.compact,
|
127
126
|
)
|
128
127
|
# This method on the pCloud API can accept multiple uploads at once.
|
@@ -130,7 +129,10 @@ module Pcloud
|
|
130
129
|
# so we return just one file out of this method.
|
131
130
|
uploaded_file = parse_many(response).first
|
132
131
|
raise UploadFailed if uploaded_file.nil?
|
133
|
-
|
132
|
+
uploaded_file
|
133
|
+
rescue KeyError => e
|
134
|
+
missing_param = e.message.gsub("key not found: ", "")
|
135
|
+
raise MissingParameter.new("#{missing_param} is required")
|
134
136
|
end
|
135
137
|
end
|
136
138
|
end
|
data/lib/pcloud/folder.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
module Pcloud
|
2
2
|
class Folder
|
3
|
-
class
|
4
|
-
class
|
5
|
-
class
|
3
|
+
class InvalidParameter < StandardError; end
|
4
|
+
class InvalidParameters < StandardError; end
|
5
|
+
class MissingParameter < StandardError; end
|
6
|
+
|
6
7
|
include Parser
|
8
|
+
include Pcloud::TimeHelper
|
7
9
|
|
8
10
|
SUPPORTED_UPDATE_PARAMS = [:name, :parent_folder_id, :path].freeze
|
11
|
+
SUPPORTED_FIND_BY_PARAMS = [:id, :path].freeze
|
9
12
|
|
10
13
|
attr_reader :id, :path, :name, :parent_folder_id, :is_deleted, :created_at,
|
11
14
|
:modified_at
|
@@ -23,16 +26,16 @@ module Pcloud
|
|
23
26
|
# have any contents set yet.
|
24
27
|
@contents_are_confirmed = @contents && @contents.size > 0
|
25
28
|
@is_deleted = params.fetch(:is_deleted) || false
|
26
|
-
@created_at = params.fetch(:created_at)
|
27
|
-
@modified_at = params.fetch(:modified_at)
|
29
|
+
@created_at = time_from(params.fetch(:created_at))
|
30
|
+
@modified_at = time_from(params.fetch(:modified_at))
|
28
31
|
end
|
29
32
|
|
30
33
|
def update(params)
|
31
34
|
unless (params.keys - SUPPORTED_UPDATE_PARAMS).empty?
|
32
|
-
raise
|
35
|
+
raise InvalidParameters.new("Must be one of #{SUPPORTED_UPDATE_PARAMS}")
|
33
36
|
end
|
34
|
-
if params[:path] &&
|
35
|
-
raise
|
37
|
+
if params[:path] && params[:path][0] != "/"
|
38
|
+
raise InvalidParameter.new(":path parameter must start with `/`")
|
36
39
|
end
|
37
40
|
query = {
|
38
41
|
folderid: id,
|
@@ -69,13 +72,6 @@ module Pcloud
|
|
69
72
|
@contents
|
70
73
|
end
|
71
74
|
|
72
|
-
private
|
73
|
-
|
74
|
-
def is_invalid_path_param?(path_param)
|
75
|
-
# Path params have to start and end with `/``
|
76
|
-
[path_param[0], path_param[-1]] != ["/", "/"]
|
77
|
-
end
|
78
|
-
|
79
75
|
class << self
|
80
76
|
def first_or_create(params)
|
81
77
|
if params[:parent_folder_id] && params[:name]
|
@@ -83,16 +79,29 @@ module Pcloud
|
|
83
79
|
elsif params[:path]
|
84
80
|
parse_one(Client.execute("createfolderifnotexists", query: { path: params[:path] }))
|
85
81
|
else
|
86
|
-
raise
|
82
|
+
raise InvalidParameters.new("either :path or a combination of :parent_folder_id and :name params are required")
|
87
83
|
end
|
88
84
|
end
|
89
85
|
|
86
|
+
def exists?(id)
|
87
|
+
find(id)
|
88
|
+
true
|
89
|
+
rescue Pcloud::Client::ErrorResponse => e
|
90
|
+
return false if e.message == "Directory does not exist."
|
91
|
+
raise e
|
92
|
+
end
|
93
|
+
|
90
94
|
def find(id)
|
91
95
|
parse_one(Client.execute("listfolder", query: { folderid: id }))
|
92
96
|
end
|
93
97
|
|
94
|
-
def find_by(
|
95
|
-
|
98
|
+
def find_by(params)
|
99
|
+
unless (params.keys - SUPPORTED_FIND_BY_PARAMS).empty?
|
100
|
+
raise InvalidParameters.new("Must be one of #{SUPPORTED_FIND_BY_PARAMS}")
|
101
|
+
end
|
102
|
+
raise InvalidParameters.new(":id takes precedent over :path, please only use one or the other") if params[:path] && params[:id]
|
103
|
+
query = { path: params[:path], folderid: params[:id] }.compact
|
104
|
+
parse_one(Client.execute("listfolder", query: query))
|
96
105
|
end
|
97
106
|
end
|
98
107
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Pcloud
|
2
|
+
module TimeHelper
|
3
|
+
class UnrecognizedTimeFormat < StandardError; end
|
4
|
+
|
5
|
+
TIMEZONE = TZInfo::Timezone.get(ENV.fetch("TZ", "UTC")).freeze
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def time_from(time)
|
10
|
+
time_object =
|
11
|
+
if time.is_a?(String)
|
12
|
+
Time.parse(time)
|
13
|
+
elsif time.is_a?(Integer)
|
14
|
+
return Time.at(time) if time.digits.size < 13
|
15
|
+
milliseconds = time.to_s[-3..-1].to_i
|
16
|
+
seconds = time.to_s[0..-4].to_i
|
17
|
+
# Older Ruby versions only support microseconds as the second
|
18
|
+
# argument to Time.at/2
|
19
|
+
Time.at(seconds, milliseconds * 1000)
|
20
|
+
elsif time.is_a?(Time)
|
21
|
+
time
|
22
|
+
else
|
23
|
+
raise Pcloud::TimeHelper::UnrecognizedTimeFormat.new(time.inspect)
|
24
|
+
end
|
25
|
+
TIMEZONE.to_local(time_object)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/pcloud/version.rb
CHANGED
data/lib/pcloud_api.rb
CHANGED
data/pcloud_api.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.summary = "A Ruby library for interacting with the pCloud API"
|
10
10
|
spec.homepage = "https://github.com/jhunschejones/pcloud_api"
|
11
11
|
spec.license = "MIT"
|
12
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
13
13
|
|
14
14
|
spec.metadata["homepage_uri"] = spec.homepage
|
15
15
|
spec.metadata["source_code_uri"] = "https://github.com/jhunschejones/pcloud_api"
|
@@ -18,16 +18,17 @@ Gem::Specification.new do |spec|
|
|
18
18
|
# Specify which files should be added to the gem when it is released.
|
19
19
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
20
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|.github)/}) }
|
22
22
|
end
|
23
23
|
spec.bindir = "exe"
|
24
24
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
25
|
spec.require_paths = ["lib"]
|
26
26
|
|
27
27
|
spec.add_development_dependency "bundler", ">= 1.17", "< 3.0"
|
28
|
-
spec.add_development_dependency "rake", "~>
|
28
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
29
29
|
spec.add_development_dependency "rspec", "~> 3.0"
|
30
30
|
spec.add_development_dependency "pry", "~> 0.13"
|
31
31
|
|
32
32
|
spec.add_dependency "httparty", "~> 0.16"
|
33
|
+
spec.add_dependency "tzinfo"
|
33
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pcloud_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Hunsche Jones
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -36,14 +36,14 @@ dependencies:
|
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
39
|
+
version: '13.0'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
46
|
+
version: '13.0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,7 +86,21 @@ dependencies:
|
|
86
86
|
- - "~>"
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0.16'
|
89
|
-
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: tzinfo
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :runtime
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
description:
|
90
104
|
email:
|
91
105
|
- joshua@hunschejones.com
|
92
106
|
executables: []
|
@@ -95,7 +109,6 @@ extra_rdoc_files: []
|
|
95
109
|
files:
|
96
110
|
- ".gitignore"
|
97
111
|
- ".rspec"
|
98
|
-
- ".travis.yml"
|
99
112
|
- CHANGELOG.md
|
100
113
|
- Gemfile
|
101
114
|
- LICENSE.txt
|
@@ -110,6 +123,7 @@ files:
|
|
110
123
|
- lib/pcloud/file/parser.rb
|
111
124
|
- lib/pcloud/folder.rb
|
112
125
|
- lib/pcloud/folder/parser.rb
|
126
|
+
- lib/pcloud/time_helper.rb
|
113
127
|
- lib/pcloud/version.rb
|
114
128
|
- lib/pcloud_api.rb
|
115
129
|
- pcloud_api.gemspec
|
@@ -120,7 +134,7 @@ metadata:
|
|
120
134
|
homepage_uri: https://github.com/jhunschejones/pcloud_api
|
121
135
|
source_code_uri: https://github.com/jhunschejones/pcloud_api
|
122
136
|
changelog_uri: https://github.com/jhunschejones/pcloud_api/blob/master/CHANGELOG.md
|
123
|
-
post_install_message:
|
137
|
+
post_install_message:
|
124
138
|
rdoc_options: []
|
125
139
|
require_paths:
|
126
140
|
- lib
|
@@ -128,15 +142,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
142
|
requirements:
|
129
143
|
- - ">="
|
130
144
|
- !ruby/object:Gem::Version
|
131
|
-
version: 2.
|
145
|
+
version: 2.4.0
|
132
146
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
147
|
requirements:
|
134
148
|
- - ">="
|
135
149
|
- !ruby/object:Gem::Version
|
136
150
|
version: '0'
|
137
151
|
requirements: []
|
138
|
-
rubygems_version: 3.2.
|
139
|
-
signing_key:
|
152
|
+
rubygems_version: 3.2.22
|
153
|
+
signing_key:
|
140
154
|
specification_version: 4
|
141
155
|
summary: A Ruby library for interacting with the pCloud API
|
142
156
|
test_files: []
|