uploadcare-ruby 1.0.6 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/LICENSE +1 -1
- data/README.md +343 -169
- data/lib/uploadcare.rb +1 -0
- data/lib/uploadcare/api.rb +2 -1
- data/lib/uploadcare/resources/file.rb +27 -3
- data/lib/uploadcare/rest/auth/auth.rb +31 -0
- data/lib/uploadcare/rest/auth/secure.rb +43 -0
- data/lib/uploadcare/rest/auth/simple.rb +16 -0
- data/lib/uploadcare/rest/connections/api_connection.rb +19 -7
- data/lib/uploadcare/rest/connections/upload_connection.rb +2 -2
- data/lib/uploadcare/rest/middlewares/auth_middleware.rb +24 -0
- data/lib/uploadcare/rest/middlewares/parse_json_middleware.rb +1 -1
- data/lib/uploadcare/rest/middlewares/raise_error_middleware.rb +2 -2
- data/lib/uploadcare/version.rb +1 -1
- data/spec/resources/file_spec.rb +107 -7
- data/spec/rest/api_connection_spec.rb +58 -9
- data/spec/rest/auth/secure_spec.rb +66 -0
- data/spec/rest/auth/simple_spec.rb +31 -0
- data/spec/spec_helper.rb +18 -1
- metadata +33 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a64d207f7ef4f89a13f68834cea937420cc9f3a8
|
4
|
+
data.tar.gz: 4dfa8a074d60bc1a37651fb5c3f138799fe38154
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b23740f26de3e0eeffdb832e431f0a4f739c089afb23f6e9d60a880e1ea2cb497e8288684c8abf96ee4a3daeaaeaea571723e18970fe979dbb1fea77dde4c07f
|
7
|
+
data.tar.gz: 4552d01fa016da894a2ed61e02f4ead93723b65faa76014ba61ef8af673fa20a61203831efe89e1aa552dfc3cdd39c3abe83622ea9ef93ce6f70e274a261f70f
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
#Changelog
|
2
2
|
|
3
|
+
### 1.1.0, 21.03.2017
|
4
|
+
|
5
|
+
- Deprecated `Uploadcare::Api::File#copy` in favor of `#internal_copy` and `#external_copy`.
|
6
|
+
- Added to new methods to Uploadcare::Api::File, #internal_copy and #external_copy.
|
7
|
+
- Added support of [secure authorization](https://uploadcare.com/documentation/rest/#request) for REST API. It is now used by default (can be overriden in config)
|
8
|
+
- Fixed middleware names that could break other gems ([#13](https://github.com/uploadcare/uploadcare-ruby/issues/13}).
|
9
|
+
|
3
10
|
### 1.0.6, 30.01.2017
|
4
11
|
|
5
12
|
- Fixed incorrect dependencies
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,42 +1,42 @@
|
|
1
1
|
[](http://travis-ci.org/uploadcare/uploadcare-ruby)
|
2
2
|
|
3
|
-
A ruby wrapper for [Uploadcare
|
4
|
-
|
3
|
+
A [Ruby](https://www.ruby-lang.org/en/) wrapper for [Uploadcare](https://uploadcare.com).
|
5
4
|
|
6
5
|
## Installation
|
7
6
|
|
8
|
-
|
7
|
+
Installing `uploadcare-ruby` is quite simple and takes a couple of steps.
|
8
|
+
First of, add the following line to your app's Gemfile:
|
9
9
|
|
10
10
|
```ruby
|
11
11
|
gem 'uploadcare-ruby'
|
12
12
|
```
|
13
13
|
|
14
|
-
|
14
|
+
Once you've added the line, execute this:
|
15
15
|
|
16
16
|
```shell
|
17
17
|
$ bundle install
|
18
18
|
```
|
19
19
|
|
20
|
-
Or
|
20
|
+
Or that (for manual install):
|
21
21
|
|
22
22
|
```shell
|
23
23
|
$ gem install uploadcare-ruby
|
24
24
|
```
|
25
25
|
|
26
|
-
|
26
|
+
## Initialization
|
27
27
|
|
28
|
-
|
29
|
-
Just create an API object - and you're good to go.
|
28
|
+
Init is simply done through creating an API object.
|
30
29
|
|
31
30
|
```ruby
|
32
31
|
require 'uploadcare'
|
33
32
|
|
34
|
-
@api = Uploadcare::Api.new #
|
33
|
+
@api = Uploadcare::Api.new # default settings are used
|
35
34
|
|
36
|
-
@api = Uploadcare::Api.new(settings)
|
35
|
+
@api = Uploadcare::Api.new(settings) # using user-defined settings
|
37
36
|
```
|
38
37
|
|
39
|
-
|
38
|
+
Here's how the default settings look like:
|
39
|
+
|
40
40
|
``` ruby
|
41
41
|
{
|
42
42
|
public_key: 'demopublickey', # you need to override this
|
@@ -46,51 +46,42 @@ require 'uploadcare'
|
|
46
46
|
static_url_base: 'https://ucarecdn.com',
|
47
47
|
api_version: '0.3',
|
48
48
|
cache_files: true,
|
49
|
+
auth_scheme: :secure
|
49
50
|
}
|
50
51
|
```
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
--
|
60
|
-
|
61
|
-
## Raw API
|
62
|
-
Raw API is a simple interface that allows you to make custom requests to Uploadcare REST API.
|
63
|
-
Just in case you want some low-level control over your app.
|
64
|
-
|
65
|
-
```ruby
|
66
|
-
# any request
|
67
|
-
@api.request :get, "/files/", {page: 2}
|
68
|
-
|
69
|
-
# you also have the shortcuts for methods
|
70
|
-
@api.get '/files', {page: 2}
|
71
|
-
|
72
|
-
@api.post ...
|
53
|
+
You're free to use both `demopublickey` and `demoprivatekey`
|
54
|
+
for initial testing purposes. We wipe out files loaded to our
|
55
|
+
demo account periodically. For a better experience,
|
56
|
+
consider creating an Uploadcare account. Check out
|
57
|
+
[this](http://kb.uploadcare.com/article/234-uc-project-and-account)
|
58
|
+
article to get up an running in minutes.
|
73
59
|
|
74
|
-
|
60
|
+
Please note, in order to use [Upload API](https://uploadcare.com/documentation/upload/)
|
61
|
+
you will only need the public key alone. However, using
|
62
|
+
[REST API](https://uploadcare.com/documentation/rest/) requires you to
|
63
|
+
use both public and private keys for authentication.
|
64
|
+
While “private key” is a common way to name a key from an
|
65
|
+
authentication key pair, the actual thing for our `auth-param` is `secret_key`.
|
75
66
|
|
76
|
-
|
67
|
+
## Usage
|
77
68
|
|
78
|
-
|
79
|
-
|
69
|
+
This section contains practical usage examples. Please note,
|
70
|
+
everything that follows gets way more clear once you've looked
|
71
|
+
through our docs [intro](https://uploadcare.com/documentation/).
|
80
72
|
|
81
|
-
|
73
|
+
### Basic usage: uploading a single file, manipulations
|
82
74
|
|
83
|
-
|
84
|
-
Using Uploadcare is pretty easy (which is the essence of the service itself).
|
75
|
+
Using Uploadcare is simple, and here are the basics of handling files.
|
85
76
|
|
86
|
-
|
77
|
+
First of, create the API object:
|
87
78
|
|
88
79
|
```ruby
|
89
80
|
@api = Uploadcare::Api.new(CONFIG)
|
90
81
|
|
91
82
|
```
|
92
83
|
|
93
|
-
|
84
|
+
And yeah, now you can upload a file:
|
94
85
|
|
95
86
|
```ruby
|
96
87
|
@file_to_upload = File.open("your-file.png")
|
@@ -99,62 +90,56 @@ Upload file
|
|
99
90
|
# => #<Uploadcare::Api::File ...
|
100
91
|
```
|
101
92
|
|
102
|
-
|
93
|
+
Then, let's check out UUID and URL of the
|
94
|
+
file you've just uploaded:
|
103
95
|
|
104
96
|
```ruby
|
105
|
-
# file uuid (you probably want to store
|
97
|
+
# file uuid (you'd probably want to store those somewhere)
|
106
98
|
@uc_file.uuid
|
107
|
-
# => "
|
99
|
+
# => "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
|
108
100
|
|
109
|
-
# url for the file
|
101
|
+
# url for the file, can be used with your website or app right away
|
110
102
|
@uc_file.cdn_url
|
111
|
-
# => "https://ucarecdn.com/
|
103
|
+
# => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/"
|
112
104
|
```
|
113
105
|
|
114
|
-
|
106
|
+
Your might then want to store or delete the uploaded file.
|
107
|
+
Storing files could be crucial if you aren't using the
|
108
|
+
“Automatic file storing” option for your Uploadcare project.
|
109
|
+
If not stored manually or automatically, files get deleted
|
110
|
+
within a 24-hour period.
|
115
111
|
|
116
112
|
```ruby
|
117
|
-
#
|
113
|
+
# that's how you store a file
|
118
114
|
@uc_file.store
|
119
115
|
# => #<Uploadcare::Api::File ...
|
120
116
|
|
121
|
-
# and
|
117
|
+
# and that works for deleting it
|
122
118
|
@uc_file.delete
|
123
119
|
# => #<Uploadcare::Api::File ...
|
124
120
|
```
|
125
|
-
## Uploading
|
126
|
-
You can upload either File object (array of files will also cut it) or custom URL.
|
127
121
|
|
128
|
-
### Uploading from URL
|
129
|
-
|
122
|
+
### Uploading a file from URL
|
123
|
+
|
124
|
+
Now, this one is also quick. Just pass your URL into our API
|
125
|
+
and you're good to go.
|
130
126
|
|
131
127
|
```ruby
|
132
|
-
# smart upload
|
128
|
+
# the smart upload
|
133
129
|
@file = @api.upload "http://your.awesome/avatar.jpg"
|
134
130
|
# => #<Uploadcare::Api::File ...
|
135
131
|
|
136
|
-
# explicitly upload from URL
|
132
|
+
# use this one if you want to explicitly upload from URL
|
137
133
|
@file = @api.upload_from_url "http://your.awesome/avatar.jpg"
|
138
134
|
# => #<Uploadcare::Api::File ...
|
139
135
|
```
|
140
|
-
Keep in mind that invalid URL
|
141
|
-
|
142
|
-
### Uploading a single file
|
143
|
-
Like with URL - just start throwing your file into API
|
144
|
-
|
145
|
-
```ruby
|
136
|
+
Keep in mind that providing invalid URL
|
137
|
+
will raise `ArgumentError`.
|
146
138
|
|
147
|
-
|
148
|
-
|
149
|
-
@uc_file = @api.upload file
|
150
|
-
# => #<Uploadcare::Api::File ...
|
151
|
-
|
152
|
-
```
|
153
|
-
And that's it.
|
139
|
+
### Uploading multiple files
|
154
140
|
|
155
|
-
|
156
|
-
|
157
|
-
Note, that every object in array must be an instance of `File`.
|
141
|
+
Uploading multiple files is as simple as passing an array
|
142
|
+
of `File` instances into our API.
|
158
143
|
|
159
144
|
```ruby
|
160
145
|
file1 = File.open("path/to/your/file.png")
|
@@ -162,22 +147,30 @@ file2 = File.open("path/to/your/another-file.png")
|
|
162
147
|
files = [file1, file2]
|
163
148
|
|
164
149
|
@uc_files = @api.upload files
|
165
|
-
# => [#<Uploadcare::Api::File uuid="
|
166
|
-
# #<Uploadcare::Api::File uuid="
|
150
|
+
# => [#<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40">,
|
151
|
+
# #<Uploadcare::Api::File uuid="96cdc400-adc3-435b-9c94-04cd87633fbb">]
|
167
152
|
```
|
168
|
-
|
153
|
+
|
154
|
+
In case of multiple input, the respective output would also be an array.
|
155
|
+
You can iterate through the array to address to single files.
|
156
|
+
You might also want to request more info about a file using `load_data`.
|
169
157
|
|
170
158
|
```ruby
|
171
159
|
@uc_files[0]
|
172
|
-
# => #<Uploadcare::Api::File uuid="
|
160
|
+
# => #<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40">
|
173
161
|
|
174
|
-
@uc_files[
|
175
|
-
# => #<Uploadcare::Api::File uuid="
|
162
|
+
@uc_files[1].load_data
|
163
|
+
# => #<Uploadcare::Api::File uuid="96cdc400-adc3-435b-9c94-04cd87633fbb", original_file_url="https://ucarecdn.com/96cdc400-adc3-435b-9c94-04cd87633fbb/samuelzeller118195.jpg", image_info={"width"=>4896, "geo_location"=>nil, "datetime_original"=>nil, "height"=>3264}, ....>
|
176
164
|
```
|
177
165
|
|
178
|
-
|
179
|
-
|
180
|
-
|
166
|
+
### `File` object
|
167
|
+
|
168
|
+
Now that we've already outlined using arrays of `File` instances
|
169
|
+
to upload multiple files, let's fix on the `File` itself.
|
170
|
+
It's the the primary object for Uploadcare API.
|
171
|
+
Basically, it's an avatar for a file you uploaded.
|
172
|
+
And all the further operations are performed using this avatar,
|
173
|
+
the `File` object.
|
181
174
|
|
182
175
|
```ruby
|
183
176
|
@file_to_upload = File.open("your-file.png")
|
@@ -186,28 +179,32 @@ So all the operations you do - you do it with the file object.
|
|
186
179
|
# => #<Uploadcare::Api::File ...
|
187
180
|
|
188
181
|
@uc_file.uuid
|
189
|
-
# => "
|
182
|
+
# => "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
|
190
183
|
|
191
184
|
@uc_file.cdn_url
|
192
|
-
# => "https://ucarecdn.com/
|
185
|
+
# => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/"
|
193
186
|
```
|
194
187
|
|
195
|
-
|
196
|
-
|
188
|
+
Please note, all the data associated with files is only accessible
|
189
|
+
through separate HTTP requests only. So if you don't specifically
|
190
|
+
need file data (filenames, image dimensions, etc.), you'll be just
|
191
|
+
fine with using `:uuid` and `:cdn_url` methods for file output:
|
197
192
|
|
198
193
|
```erb
|
199
194
|
<img src="#{@file.cdn_url}"/>
|
200
195
|
```
|
201
196
|
|
202
|
-
|
203
|
-
|
204
|
-
|
197
|
+
Great, we've just lowered a precious loading time.
|
198
|
+
However, if you do need the data, you can always request
|
199
|
+
it manually:
|
205
200
|
|
206
201
|
```ruby
|
207
202
|
@uc_file.load_data
|
208
203
|
```
|
209
204
|
|
210
|
-
|
205
|
+
That way your file object will respond to any method described
|
206
|
+
in [API docs](https://uploadcare.com/documentation/rest/#file).
|
207
|
+
Basically, that's an an OpenStruct, so you know what to do:
|
211
208
|
|
212
209
|
```ruby
|
213
210
|
@uc_file.original_filename
|
@@ -217,80 +214,253 @@ Then your file object will respond to any method, described in API documentation
|
|
217
214
|
# => {"width"=>397, "geo_location"=>nil, "datetime_original"=>nil, "height"=>81}
|
218
215
|
```
|
219
216
|
|
220
|
-
|
217
|
+
### `File` object from UUID or CDN URL
|
221
218
|
|
222
|
-
|
223
|
-
|
224
|
-
|
219
|
+
`File` objects are needed to manipulate files on our CDN.
|
220
|
+
The usual case would be you as a client storing file UUIDs
|
221
|
+
or CDN URLs somewhere on your side, e.g. in a database.
|
222
|
+
This is how you can use those to create `File` objects:
|
225
223
|
|
226
224
|
```ruby
|
227
|
-
# file
|
228
|
-
@file = @api.file "
|
229
|
-
# => #<Uploadcare::Api::File uuid="
|
225
|
+
# file object from UUID
|
226
|
+
@file = @api.file "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
|
227
|
+
# => #<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
|
230
228
|
|
231
|
-
# file
|
232
|
-
@file = @api.file "https://ucarecdn.com/
|
233
|
-
# => #<Uploadcare::Api::File uuid="
|
229
|
+
# file object from CDN URL
|
230
|
+
@file = @api.file "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/"
|
231
|
+
# => #<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
|
234
232
|
|
235
|
-
#
|
233
|
+
# note, files you generate won't be loaded on init,
|
234
|
+
# you'll need to load those manually
|
236
235
|
@file.is_loaded?
|
237
236
|
# => false
|
238
237
|
```
|
239
238
|
|
240
239
|
### Operations
|
241
|
-
|
242
|
-
|
243
|
-
|
240
|
+
|
241
|
+
Another way to mainpulate files on CDN is through operations.
|
242
|
+
This is particularly useful for images.
|
243
|
+
We've got on-the-fly crop, resize, rotation, format conversions, and
|
244
|
+
[more](https://uploadcare.com/documentation/cdn/).
|
245
|
+
Image operations are there to help you build responsive designs,
|
246
|
+
generate thumbnails and galleries, change formats, etc.
|
247
|
+
Currently, this gem has no specific methods for image operations,
|
248
|
+
we're planning to implement those in further versions.
|
249
|
+
However, we do support applying image operations through
|
250
|
+
adding them to CDN URLs. That's an Uploadcare CDN-native
|
251
|
+
way described in our [docs](https://uploadcare.com/documentation/cdn/).
|
244
252
|
|
245
253
|
```ruby
|
246
|
-
@file = @api.file "https://ucarecdn.com/
|
247
|
-
# => #<Uploadcare::Api::File uuid="
|
254
|
+
@file = @api.file "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/-/crop/150x150/center/-/format/png/"
|
255
|
+
# => #<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
|
248
256
|
|
249
257
|
@file.operations
|
250
258
|
# => ["crop/150x150/center", "format/png"]
|
251
259
|
|
252
|
-
# note that by default :cdn_url method
|
260
|
+
# note that by default :cdn_url method returns URLs with no operations:
|
253
261
|
@file.cdn_url
|
254
|
-
# => "https://ucarecdn.com/
|
262
|
+
# => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/""
|
255
263
|
|
256
|
-
# you can pass true
|
264
|
+
# you can pass "true" into the :cdn_url method to get URL including operations:
|
257
265
|
@file.cdn_url(true)
|
258
266
|
# => "https://ucarecdn.com/a8775cf7-0c2c-44fa-b071-4dd48637ecac/-/crop/150x150/center/-/format/png/"
|
259
267
|
|
260
|
-
#
|
268
|
+
# there also are specific methods to either dump or include image operations
|
269
|
+
# in the output URL:
|
261
270
|
@file.cdn_url_with_operations
|
262
271
|
@file.cdn_url_without_operations
|
263
272
|
```
|
264
273
|
|
265
|
-
|
274
|
+
While there's no operation wrapper, the best way of handling operations
|
275
|
+
is through adding them to URLs as strings:
|
266
276
|
|
267
277
|
```ruby
|
268
278
|
<img src="#{file.cdn_url}-/crop/#{width}x#{height}/center/"/>
|
269
279
|
# or something like that
|
270
280
|
```
|
271
281
|
|
272
|
-
|
273
|
-
|
274
|
-
|
282
|
+
### Copying files
|
283
|
+
|
284
|
+
You can also create file copies using our API.
|
285
|
+
There are multiple ways of creating those.
|
286
|
+
Also, copying is important for image files because
|
287
|
+
it allows you to “apply” all the CDN operations
|
288
|
+
specified in the source URL to a separate static image.
|
289
|
+
|
290
|
+
First of all, a copy of your file can be put in the Uploadcare storage.
|
291
|
+
This is called “internal copy”, and here's how it works:
|
275
292
|
|
276
293
|
```ruby
|
277
|
-
@
|
294
|
+
@uc_file.internal_copy
|
295
|
+
|
296
|
+
# =>
|
297
|
+
{
|
298
|
+
"type"=>"file",
|
299
|
+
"result"=> {
|
300
|
+
"uuid"=>"a191a3df-2c43-4939-9590-784aa371ad6d",
|
301
|
+
"original_file_url"=>"https://ucarecdn.com/a191a3df-2c43-4939-9590-784aa371ad6d/19xldj.jpg",
|
302
|
+
"image_info"=>nil,
|
303
|
+
"datetime_stored"=>nil,
|
304
|
+
"mime_type"=>"application/octet-stream",
|
305
|
+
"is_ready"=>true,
|
306
|
+
"url"=>"https://api.uploadcare.com/files/a191a3df-2c43-4939-9590-784aa371ad6d/",
|
307
|
+
"original_filename"=>"19xldj.jpg",
|
308
|
+
"datetime_uploaded"=>"2017-02-10T14:14:18.690581Z",
|
309
|
+
"size"=>0,
|
310
|
+
"is_image"=>nil,
|
311
|
+
"datetime_removed"=>nil,
|
312
|
+
"source"=>"/4ea293d5-153f-422f-a24e-350237109606/"
|
313
|
+
}
|
314
|
+
}
|
315
|
+
```
|
316
|
+
|
317
|
+
Once the procedure is complete, a copy would be a separate file
|
318
|
+
with its own UUID and attributes.
|
319
|
+
|
320
|
+
`#internal_copy` can optionally be used with the options hash argument.
|
321
|
+
The available options are:
|
322
|
+
|
323
|
+
- *store*
|
324
|
+
|
325
|
+
By default a copy is created without “storing”.
|
326
|
+
Which means it will be deleted within a 24-hour period.
|
327
|
+
You can make your output copy permanent by passing the
|
328
|
+
`store: true` option to the `#internal_copy` method.
|
329
|
+
|
330
|
+
Example:
|
331
|
+
|
332
|
+
```ruby
|
333
|
+
@uc_file.internal_copy(store: true)
|
334
|
+
```
|
335
|
+
|
336
|
+
- *strip_operations*
|
337
|
+
|
338
|
+
If your file is an image and you applied some operations to it,
|
339
|
+
then by default the same set of operations is also applied to a copy.
|
340
|
+
You can override this by passing `strip_operations: true` to the
|
341
|
+
`#internal_copy` method.
|
342
|
+
|
343
|
+
Example:
|
344
|
+
|
345
|
+
```ruby
|
346
|
+
file = @api.file "https://ucarecdn.com/24626d2f-3f23-4464-b190-37115ce7742a/-/resize/50x50/"
|
347
|
+
file.internal_copy
|
348
|
+
# => This will trigger POST /files/ with {"source": "https://ucarecdn.com/24626d2f-3f23-4464-b190-37115ce7742a/-/resize/50x50/"} in the body
|
349
|
+
file.internal_copy(strip_operations: true)
|
350
|
+
# => This will trigger POST /files/ with {"source": "https://ucarecdn.com/24626d2f-3f23-4464-b190-37115ce7742a/"} in the body
|
351
|
+
```
|
352
|
+
|
353
|
+
Another option is copying your file to a custom storage.
|
354
|
+
We call it “external copy” and here's the usage example:
|
355
|
+
|
356
|
+
```ruby
|
357
|
+
@uc_file.external_copy('my_custom_storage_name')
|
358
|
+
|
359
|
+
# =>
|
360
|
+
{
|
361
|
+
"type"=>"url",
|
362
|
+
"result"=>"s3://my_bucket_name/c969be02-9925-4a7e-aa6d-b0730368791c/view.png"
|
363
|
+
}
|
364
|
+
```
|
365
|
+
|
366
|
+
First argument of the `#external_copy` method is a name of
|
367
|
+
a custom destination storage for your file.
|
368
|
+
|
369
|
+
There's also an optional second argument — options hash. The available options are:
|
370
|
+
|
371
|
+
- *make_public*
|
372
|
+
|
373
|
+
Make a copy available via public links. Can be either `true` or `false`.
|
374
|
+
|
375
|
+
- *pattern*
|
376
|
+
|
377
|
+
Name pattern for a copy. If the parameter is omitted, custom storage pattern is used.
|
378
|
+
|
379
|
+
- *strip_operations*
|
380
|
+
|
381
|
+
Same as for `#internal_copy`
|
382
|
+
|
383
|
+
You might want to learn more about
|
384
|
+
[storage options](https://uploadcare.com/documentation/storages/) or
|
385
|
+
[copying files](https://uploadcare.com/documentation/rest/#files-post)
|
386
|
+
with Uploadcare.
|
387
|
+
|
388
|
+
### `Group` object
|
389
|
+
|
390
|
+
Groups are structures intended to organize sets of separate files.
|
391
|
+
Each group is assigned UUID.
|
392
|
+
Note, group UUIDs include a `~#{files_count}` part at the end.
|
393
|
+
That's a requirement of our API.
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
# group can be created from an array of Uploadcare files
|
397
|
+
@files_ary = [@file, @file2]
|
398
|
+
@files = @api.upload @files_ary
|
399
|
+
@group = @api.create_group @files
|
400
|
+
# => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
|
401
|
+
|
402
|
+
# another way to from a group is via an array of strings holding UUIDs
|
403
|
+
@uuids_ary = ["c969be02-9925-4a7e-aa6d-b0730368791c", "c969be02-9925-4a7e-aa6d-b0730368791c"]
|
404
|
+
@group = @api.create_group @uuids_ary
|
405
|
+
# => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
|
406
|
+
|
407
|
+
# also, you can create a group object via group UUID
|
408
|
+
@group_uloaded = @api.group "#{uuid}"
|
409
|
+
```
|
410
|
+
|
411
|
+
As with files, groups created via UUIDs are not loaded by default.
|
412
|
+
You need to load the data manually, as it requires a separate
|
413
|
+
HTTP GET request. New groups created with the `:create_group` method
|
414
|
+
are loaded by default.
|
415
|
+
|
416
|
+
```ruby
|
417
|
+
@group = @api.group "#{uuid}"
|
418
|
+
|
419
|
+
@group.is_loaded?
|
420
|
+
# => false
|
421
|
+
|
422
|
+
@group.load_data
|
423
|
+
# => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
|
424
|
+
|
425
|
+
# once a group is loaded, you can use any methods described in our API docs
|
426
|
+
# the files within a loaded group are loaded by default
|
427
|
+
@group.files
|
428
|
+
# => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
|
429
|
+
# ... #{files_count} of them ...
|
430
|
+
# #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
|
431
|
+
```
|
432
|
+
|
433
|
+
Check out our docs to learn more about
|
434
|
+
[groups](https://uploadcare.com/documentation/rest/#group).
|
435
|
+
|
436
|
+
### File lists and pagination
|
437
|
+
|
438
|
+
File list is a paginated collection of files. Such lists are created
|
439
|
+
to better represent the contents of your project.
|
440
|
+
For this gem, a file list would be a single page containing
|
441
|
+
20 files (you can override the number).
|
442
|
+
There also are methods for navigating through pages.
|
443
|
+
You can find more info about pagination
|
444
|
+
[here](https://uploadcare.com/documentation/rest/#pagination).
|
445
|
+
|
446
|
+
```ruby
|
447
|
+
@list = @api.file_list 1 # page number, 1 is the default
|
278
448
|
# => #<Uploadcare::Api::FileList ....
|
279
449
|
|
280
450
|
|
281
|
-
# method :
|
451
|
+
# method :results returns an array of files
|
282
452
|
@list.results
|
283
453
|
# => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
|
284
454
|
# ... 20 of them ...
|
285
455
|
# #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
|
286
456
|
|
287
457
|
|
288
|
-
# note
|
458
|
+
# note, every file is already loaded
|
289
459
|
@list.results[1].is_loaded?
|
290
460
|
# => true
|
291
461
|
|
292
462
|
|
293
|
-
#
|
463
|
+
# we've also added some shortcuts
|
294
464
|
@list.to_a
|
295
465
|
# => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
|
296
466
|
# ... 20 of them ...
|
@@ -300,7 +470,7 @@ In our gem file list is a single page containing 20 (by default, value may chang
|
|
300
470
|
# => #<Uploadcare::Api::File ....
|
301
471
|
```
|
302
472
|
|
303
|
-
|
473
|
+
Here's how we handle navigating through pages:
|
304
474
|
|
305
475
|
```ruby
|
306
476
|
@list = @api.files_list 3
|
@@ -314,9 +484,8 @@ And don't forget that you can navigate throught pages:
|
|
314
484
|
@list.go_to 5
|
315
485
|
# => #<Uploadcare::Api::FileList page=5 ....
|
316
486
|
|
317
|
-
|
318
|
-
|
319
|
-
# there is also methods described in API docs avaliable for you:
|
487
|
+
# of course, you can go with any of the methods
|
488
|
+
# described in our API docs
|
320
489
|
# total pages
|
321
490
|
@list.pages
|
322
491
|
# => 16
|
@@ -329,16 +498,16 @@ And don't forget that you can navigate throught pages:
|
|
329
498
|
@list.per_page
|
330
499
|
# => 20
|
331
500
|
|
332
|
-
# total files in project
|
501
|
+
# total files in a project
|
333
502
|
@list.total
|
334
503
|
# => 308
|
335
504
|
```
|
336
505
|
|
337
|
-
|
338
|
-
|
339
|
-
Project
|
340
|
-
|
341
|
-
|
506
|
+
### `Project` object
|
507
|
+
|
508
|
+
`Project` provides basic info about the connected Uploadcare project.
|
509
|
+
That object is also an OpenStruct, so every methods out of
|
510
|
+
[these](https://uploadcare.com/documentation/rest/#project) will work.
|
342
511
|
|
343
512
|
```ruby
|
344
513
|
project = @api.project
|
@@ -349,56 +518,42 @@ project.name
|
|
349
518
|
|
350
519
|
p.collaborators
|
351
520
|
# => []
|
352
|
-
#
|
521
|
+
# while that one was empty, it usually goes like this:
|
353
522
|
# [{"email": collaborator@gmail.com, "name": "Collaborator"}, {"email": collaborator@gmail.com, "name": "Collaborator"}]
|
354
523
|
```
|
355
524
|
|
525
|
+
### Raw API
|
356
526
|
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
527
|
+
Raw API is a simple interface allowing you to make
|
528
|
+
custom requests to Uploadcare REST API.
|
529
|
+
It's mainly used when you want a low-level control
|
530
|
+
over your app.
|
361
531
|
|
362
532
|
```ruby
|
363
|
-
#
|
364
|
-
@
|
365
|
-
@files = @api.upload @files_ary
|
366
|
-
@group = @api.create_group @files
|
367
|
-
# => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
|
533
|
+
# here's how you make any requests
|
534
|
+
@api.request :get, "/files/", {page: 2}
|
368
535
|
|
369
|
-
#
|
370
|
-
@
|
371
|
-
@group = @api.create_group @uuids_ary
|
372
|
-
# => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
|
536
|
+
# and there also are shortcuts for methods
|
537
|
+
@api.get '/files', {page: 2}
|
373
538
|
|
374
|
-
|
375
|
-
@group_uloaded = @api.group "#{uuid}"
|
376
|
-
```
|
539
|
+
@api.post ...
|
377
540
|
|
378
|
-
|
379
|
-
New groups created by :create_group method is loaded by default.
|
541
|
+
@api.put ...
|
380
542
|
|
381
|
-
|
382
|
-
@group = @api.group "#{uuid}"
|
543
|
+
@api.delete ...
|
383
544
|
|
384
|
-
|
385
|
-
# => false
|
545
|
+
```
|
386
546
|
|
387
|
-
|
388
|
-
|
547
|
+
All of the raw API methods return a parsed JSON response
|
548
|
+
or raise an error (handling those is done on your side in the case).
|
389
549
|
|
390
|
-
|
391
|
-
# this files are loaded by default.
|
392
|
-
@group.files
|
393
|
-
# => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
|
394
|
-
# ... #{files_count} of them ...
|
395
|
-
# #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
|
396
|
-
```
|
550
|
+
### Error handling
|
397
551
|
|
398
|
-
|
399
|
-
|
552
|
+
Starting from the version 1.0.2, we've got have custom exceptions
|
553
|
+
that will be raised in case the Uploadcare service returns
|
554
|
+
something with 4xx or 5xx HTTP status.
|
400
555
|
|
401
|
-
|
556
|
+
Check out the list of custom errors:
|
402
557
|
|
403
558
|
```ruby
|
404
559
|
400 => Uploadcare::Error::RequestError::BadRequest,
|
@@ -415,7 +570,8 @@ List of custom errors:
|
|
415
570
|
504 => Uploadcare::Error::ServerError::GatewayTimeout
|
416
571
|
```
|
417
572
|
|
418
|
-
|
573
|
+
That's how you handle a particular error
|
574
|
+
(in this case, a “404: Not Found” error):
|
419
575
|
|
420
576
|
```ruby
|
421
577
|
begin
|
@@ -425,7 +581,7 @@ rescue Uploadcare::Error::RequestError::NotFound => e
|
|
425
581
|
end
|
426
582
|
```
|
427
583
|
|
428
|
-
|
584
|
+
Handling any request error (covers all 4xx status codes):
|
429
585
|
|
430
586
|
```ruby
|
431
587
|
begin
|
@@ -435,7 +591,7 @@ rescue Uploadcare::Error::RequestError => e
|
|
435
591
|
end
|
436
592
|
```
|
437
593
|
|
438
|
-
|
594
|
+
Handling any Uploadcare service error:
|
439
595
|
|
440
596
|
```ruby
|
441
597
|
begin
|
@@ -445,19 +601,37 @@ rescue Uploadcare::Error => e
|
|
445
601
|
end
|
446
602
|
```
|
447
603
|
|
448
|
-
|
604
|
+
Since many of the above listed things depend on Uploadcare servers,
|
605
|
+
errors might occasionally occur. Be prepared to handle those.
|
449
606
|
|
450
607
|
## Testing
|
451
608
|
|
452
|
-
|
609
|
+
For testing purposes, run `bundle exec rspec`.
|
453
610
|
|
454
|
-
|
611
|
+
Please note, if you're willing to run tests using your own keys,
|
612
|
+
make a `spec/config.yml` file containing the following:
|
455
613
|
|
456
614
|
```yaml
|
457
615
|
public_key: 'PUBLIC KEY'
|
458
616
|
private_key: 'PRIVATE KEY'
|
459
617
|
```
|
460
618
|
|
461
|
-
##
|
619
|
+
## Contributors
|
620
|
+
|
621
|
+
This is open source so fork, hack, request a pull — get a discount.
|
622
|
+
|
623
|
+
- [@romanonthego](https://github.com/romanonthego)
|
624
|
+
- [@vizvamitra](https://github.com/vizvamitra)
|
625
|
+
- [@dmitry-mukhin](https://github.com/dmitry-mukhin)
|
626
|
+
- [@zenati](https://github.com/zenati)
|
627
|
+
- [@renius](https://github.com/renius)
|
628
|
+
|
629
|
+
## Security issues
|
630
|
+
|
631
|
+
If you think you ran into something in Uploadcare libraries
|
632
|
+
which might have security implications, please hit us up at
|
633
|
+
[bugbounty@uploadcare.com](mailto:bugbounty@uploadcare.com)
|
634
|
+
or Hackerone.
|
462
635
|
|
463
|
-
|
636
|
+
We'll contact you personally in a short time to fix an issue
|
637
|
+
through co-op and prior to any public disclosure.
|