uploadcare-ruby 1.2.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/.rspec +1 -0
  4. data/.travis.yml +19 -5
  5. data/CHANGELOG.md +12 -30
  6. data/README.md +149 -72
  7. data/Rakefile +1 -1
  8. data/UPGRADE_NOTES.md +36 -0
  9. data/lib/uploadcare.rb +16 -22
  10. data/lib/uploadcare/api.rb +3 -1
  11. data/lib/uploadcare/api/file_list_api.rb +15 -4
  12. data/lib/uploadcare/api/file_storage_api.rb +34 -0
  13. data/lib/uploadcare/api/group_list_api.rb +13 -4
  14. data/lib/uploadcare/api/raw_api.rb +10 -16
  15. data/lib/uploadcare/api/uploading_api.rb +45 -84
  16. data/lib/uploadcare/api/uploading_api/upload_params.rb +72 -0
  17. data/lib/uploadcare/api/validators/file_list_options_validator.rb +73 -0
  18. data/lib/uploadcare/api/validators/group_list_options_validator.rb +49 -0
  19. data/lib/uploadcare/resources/file_list.rb +6 -33
  20. data/lib/uploadcare/resources/group_list.rb +6 -23
  21. data/lib/uploadcare/resources/resource_list.rb +83 -0
  22. data/lib/uploadcare/rest/connections/api_connection.rb +25 -3
  23. data/lib/uploadcare/rest/connections/upload_connection.rb +3 -2
  24. data/lib/uploadcare/version.rb +1 -1
  25. data/spec/api/file_list_api_spec.rb +95 -0
  26. data/spec/api/file_storage_api_spec.rb +88 -0
  27. data/spec/api/group_list_api_spec.rb +59 -0
  28. data/spec/api/raw_api_spec.rb +12 -12
  29. data/spec/api/uploading_api/upload_params_spec.rb +99 -0
  30. data/spec/api/uploading_api_spec.rb +59 -0
  31. data/spec/resources/file_list_spec.rb +13 -52
  32. data/spec/resources/file_spec.rb +6 -3
  33. data/spec/resources/group_list_spec.rb +15 -20
  34. data/spec/rest/api_connection_spec.rb +1 -1
  35. data/spec/shared/resource_list.rb +188 -0
  36. data/spec/spec_helper.rb +11 -1
  37. data/spec/uploadcare_spec.rb +9 -32
  38. data/spec/utils/parser_spec.rb +34 -36
  39. data/uploadcare-ruby.gemspec +7 -6
  40. metadata +52 -37
  41. data/lib/uploadcare/utils/user_agent.rb +0 -44
  42. data/spec/uploading/uploading_multiple_spec.rb +0 -43
  43. data/spec/uploading/uploading_spec.rb +0 -40
  44. data/spec/utils/user_agent_spec.rb +0 -46
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 9363abb3c073881cc706fb59bc0b3d3b537a5db0fc53f4a186a726873bf622a6
4
- data.tar.gz: ebdda4fafed6775f6904a9b4e1737241234d244ed42bed8c00a9fe1368d32027
2
+ SHA1:
3
+ metadata.gz: 2951151a5b47adb4e4dab2c8b3dcd225aacd9f1c
4
+ data.tar.gz: 0c24efc39d8da2550ae26a3f129faf31326f1c6f
5
5
  SHA512:
6
- metadata.gz: 9065fb7d524d457237d6ed507c405e4c83c8fb0de0f9a194dd259b35726353c6ea3dd96c23fd26f4a968784108cc6d8b2c6c593fb487345465e77c6b7960d9fb
7
- data.tar.gz: f265a7b3fbcd3fcf716019edd99668b9b763463feaf46aeecc69accbe81bd5585b24ab6a31592fc8badac78c109228271ab95862ff811044beb4585d7c75e9c7
6
+ metadata.gz: b26a5b79dec7109fdb14c80fe9335d29aff594fa98942ce99e361936fba03a535a3b055667ac91cd49e7eb6068440634472f24ef5a9cc53608ba0195c2bae906
7
+ data.tar.gz: 65ee759c45a3122b551ff6fcdc237751a8b1252663d3cd360d54041e68fcc5a6a95d873c956ce351a4d24cd20634dcc9056e72da97f2e2831b4ade6a9abd6984
data/.gitignore CHANGED
@@ -3,4 +3,4 @@ Gemfile.lock
3
3
  .ruby-version
4
4
 
5
5
  *.DS_Store
6
- *.gem
6
+ *.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -2,11 +2,25 @@ language: ruby
2
2
 
3
3
  rvm:
4
4
  - 1.9.3
5
- - 2.0.0
6
- - 2.1.10
7
- - 2.2.6
8
- - 2.3.3
9
- - 2.4.0
5
+ - 2.0
6
+ - 2.1
7
+ - 2.2
8
+ - 2.3
9
+ - 2.4
10
+ - ruby-head
11
+
12
+ jobs:
13
+ include:
14
+ - stage: style checks
15
+ rvm: 2.4
16
+ before_install:
17
+ - gem install rubocop
18
+ script:
19
+ - rubocop
20
+
21
+ allow_failures:
22
+ - stage: style checks
23
+ - rvm: ruby-head
10
24
 
11
25
  before_install:
12
26
  - gem update bundler
@@ -1,41 +1,23 @@
1
1
  # Changelog
2
- All notable changes to this project will be documented in this file.
3
2
 
4
- The format is based now on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
- and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
3
+ ### 2.0.0, 26.09.2017
6
4
 
7
- ### [1.2.2] - 2018-06-11
5
+ - There are **breaking** changes in this release, please read [upgrade notes](UPGRADE_NOTES.md#v1---v2)
6
+ - Added support for `store` flag in [Upload API](https://uploadcare.com/documentation/upload/) methods
7
+ - Upgraded to REST API v0.5
8
+ - All POST/PUT/DELETE params are now being sent as JSON instead of being form-encoded
9
+ - Added methods to store/delete multiple files at once: `Uploadcare::Api#store_files` & `Uploadcare::Api#delete_files`
10
+ - Changed pagination implementation for files and groups
8
11
 
9
- ### Fixed
10
- - Show Uploadcare::USER_AGENT deprecation warning only when the constant is being used
12
+ ### 1.1.0, 21.03.2017
11
13
 
12
- ### [1.2.1] - 2018-05-24
13
-
14
- ### Changed
15
- - Allow user to override User-Agent header
16
- - User-Agent format reports gem name, version and environment
17
-
18
-
19
- ## 1.1.0 - 2017-03-21
20
-
21
- ### Added
14
+ - Deprecated `Uploadcare::Api::File#copy` in favor of `#internal_copy` and `#external_copy`.
22
15
  - Added to new methods to `Uploadcare::Api::File`, `#internal_copy` and `#external_copy`.
23
16
  - 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)
24
-
25
- ### Fixed
26
17
  - Fixed middleware names that could break other gems ([#13](https://github.com/uploadcare/uploadcare-ruby/issues/13)).
27
18
 
28
- ### Deprecated
29
- - `Uploadcare::Api::File#copy` in favor of `#internal_copy` and `#external_copy`.
30
-
31
-
32
- ## 1.0.6 - 2017-01-30
33
-
34
- ### Added
35
- - Ruby version and public API key sent via User-Agent header (can be overriden in config)
36
-
37
- ### Fixed
38
- - Incorrect dependencies
39
19
 
20
+ ### 1.0.6, 30.01.2017
40
21
 
41
- [1.2.1]: https://github.com/uploadcare/uploadcare-ruby/compare/6dde...v1.2.1
22
+ - Fixed incorrect dependencies
23
+ - Added ruby version and public API key to User-Agent header (can be overriden in config)
data/README.md CHANGED
@@ -44,8 +44,9 @@ Here's how the default settings look like:
44
44
  upload_url_base: 'https://upload.uploadcare.com',
45
45
  api_url_base: 'https://api.uploadcare.com',
46
46
  static_url_base: 'https://ucarecdn.com',
47
- api_version: '0.3',
47
+ api_version: '0.5',
48
48
  cache_files: true,
49
+ autostore: :auto,
49
50
  auth_scheme: :secure
50
51
  }
51
52
  ```
@@ -58,12 +59,19 @@ consider creating an Uploadcare account. Check out
58
59
  article to get up an running in minutes.
59
60
 
60
61
  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
+ you will only need the public key alone. However, using
62
63
  [REST API](https://uploadcare.com/documentation/rest/) requires you to
63
- use both public and private keys for authentication.
64
+ use both public and private keys for authentication.
64
65
  While “private key” is a common way to name a key from an
65
66
  authentication key pair, the actual thing for our `auth-param` is `secret_key`.
66
67
 
68
+ `:autostore` option allows you to set the default storage
69
+ behaviour upon uploads. For more info see [`store` flag][uploads from url] for
70
+ uploads via URL and [`UPLOADCARE_STORE` flag][in-body file uploads] for file uploads
71
+
72
+ [in-body file uploads]: https://uploadcare.com/documentation/upload/#upload-body
73
+ [uploads from url]: https://uploadcare.com/documentation/upload/#from-url
74
+
67
75
  ## Usage
68
76
 
69
77
  This section contains practical usage examples. Please note,
@@ -98,7 +106,7 @@ file you've just uploaded:
98
106
  @uc_file.uuid
99
107
  # => "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
100
108
 
101
- # url for the file, can be used with your website or app right away
109
+ # URL for the file, can be used with your website or app right away
102
110
  @uc_file.cdn_url
103
111
  # => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/"
104
112
  ```
@@ -126,11 +134,11 @@ and you're good to go.
126
134
 
127
135
  ```ruby
128
136
  # the smart upload
129
- @file = @api.upload "http://your.awesome/avatar.jpg"
137
+ @file = @api.upload "http://your.awesome/avatar.jpg"
130
138
  # => #<Uploadcare::Api::File ...
131
139
 
132
140
  # use this one if you want to explicitly upload from URL
133
- @file = @api.upload_from_url "http://your.awesome/avatar.jpg"
141
+ @file = @api.upload_from_url "http://your.awesome/avatar.jpg"
134
142
  # => #<Uploadcare::Api::File ...
135
143
  ```
136
144
  Keep in mind that providing invalid URL
@@ -163,6 +171,15 @@ You might also want to request more info about a file using `load_data`.
163
171
  # => #<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}, ....>
164
172
  ```
165
173
 
174
+ ### Upload options
175
+
176
+ You can override global [`:autostore`](#initialization) option for each upload request:
177
+
178
+ ```ruby
179
+ @api.upload(files, store: true)
180
+ @api.upload_from_url(url, store: :auto)
181
+ ```
182
+
166
183
  ### `File` object
167
184
 
168
185
  Now that we've already outlined using arrays of `File` instances
@@ -238,7 +255,7 @@ This is how you can use those to create `File` objects:
238
255
 
239
256
  ### Operations
240
257
 
241
- Another way to mainpulate files on CDN is through operations.
258
+ Another way to manipulate files on CDN is through operations.
242
259
  This is particularly useful for images.
243
260
  We've got on-the-fly crop, resize, rotation, format conversions, and
244
261
  [more](https://uploadcare.com/documentation/cdn/).
@@ -356,7 +373,7 @@ We call it “external copy” and here's the usage example:
356
373
  ```ruby
357
374
  @uc_file.external_copy('my_custom_storage_name')
358
375
 
359
- # =>
376
+ # =>
360
377
  {
361
378
  "type"=>"url",
362
379
  "result"=>"s3://my_bucket_name/c969be02-9925-4a7e-aa6d-b0730368791c/view.png"
@@ -383,7 +400,120 @@ There's also an optional second argument — options hash. The available options
383
400
  You might want to learn more about
384
401
  [storage options](https://uploadcare.com/documentation/storages/) or
385
402
  [copying files](https://uploadcare.com/documentation/rest/#files-post)
386
- with Uploadcare.
403
+ with Uploadcare.
404
+
405
+
406
+ ### File lists
407
+
408
+ `Uploadcare::Api::FileList` represents the whole collection of files (or it's subset) and privides a way to iterate through it, making pagination transparent. FileList objects can be created using `Uploadcare::Api#file_list` method.
409
+
410
+ ```ruby
411
+ @list = @api.file_list # => instance of Uploadcare::Api::FileList
412
+ ```
413
+
414
+ This method accepts some options to controll which files should be fetched and how they should be fetched:
415
+
416
+ - **:limit** - Controls page size. Accepts values from 1 to 1000, defaults to 100.
417
+ - **:stored** - Can be either `true` or `false`. When true, file list will contain only stored files. When false - only not stored.
418
+ - **:removed** - Can be either `true` or `false`. When true, file list will contain only removed files. When false - all except removed. Defaults to false.
419
+ - **:ordering** - Controls the order of returned files. Available values: `datetime_updated`, `-datetime_updated`, `size`, `-size`. Defaults to `datetime_uploaded`. More info can be found [here](https://uploadcare.com/documentation/rest/#file-files)
420
+ - **:from** - Specifies the starting point for a collection. Resulting collection will contain files from the given value and to the end in a direction set by an **ordering** option. When files are ordered by datetime_updated in any direction, accepts either a `DateTime` object or an ISO 8601 string. When files are ordered by size, acepts non-negative integers (size in bytes). More info can be found [here](https://uploadcare.com/documentation/rest/#file-files)
421
+
422
+ Options used to create a file list can be accessed through `#options` method. Note that, once set, they don't affect file fetching process anymore and are stored just for your convenience. That is why they are frozen.
423
+
424
+ ```ruby
425
+ options = {
426
+ limit: 10,
427
+ stored: true,
428
+ ordering: '-datetime_uploaded',
429
+ from: "2017-01-01T00:00:00",
430
+ }
431
+ @list = @api.file_list(options)
432
+ @list.options # => same as options hash above, but frozen
433
+ ```
434
+
435
+ `Uploadcare::Api::FileList` implements Enumerable interface and holds a collection of `Uploadcare::Api::File` objects, as well as some meta information.
436
+
437
+ ```ruby
438
+ @list = @api.file_list
439
+ @list.total # => 1977
440
+ @list.meta # => {
441
+ # "next"=> "https://api.uploadcare.com/files/?from=2017-03-09T10%3A30%3A01.877590%2B00%3A00&offset=0",
442
+ # "previous"=>nil,
443
+ # "total"=>1977,
444
+ # "per_page"=>100
445
+ # }
446
+
447
+ # Enumerable interface
448
+ @list.first(5) # => array of 5 x Uploadcare::Api::File
449
+ @list.each{|file| puts file.original_filename}
450
+ @list.map{|file| file.uuid}
451
+ @list.reduce(0){|overall_size, file| overall_size += file.size}
452
+ # ...
453
+ ```
454
+
455
+ On the inside, `FileList` loada files page by page. First page is loaded when you call `Uploadcare::Api#file_list` and subsequent pages are being loaded when needed. The size of pages is controlled by a `:limit` option.
456
+
457
+ Currently loaded files are available through `FileList#objects`. `FileList#loaded` method returns the number of currently loaded files.
458
+
459
+ ```ruby
460
+ @list = @api.file_list(limit: 5) # will load first 5 files
461
+ @list.loaded # => 5
462
+ @list.fully_loaded? # => false
463
+
464
+ @list.objects # => array of 5 x Uploadcare::Api::File
465
+ @list.objects[4] # => #<Uploadcare::Api::File ...>
466
+ @list.objects[5] # => nil (since 6th file is not yet loaded)
467
+
468
+ @list[4] # won't load anything, because 5 files are already loaded
469
+ @list[5] # will load the next page
470
+ @list.loaded # => 10
471
+
472
+ # Note that the example below will load all the files left, page by page,
473
+ # and return the count only when they all will be loaded
474
+ @list.count # => 132
475
+ @list.fully_loaded? # => true
476
+ ```
477
+
478
+ Loaded files are preserved when `break` statement or an exception occures inside a block provided to #each, #map, etc.
479
+
480
+ ```ruby
481
+ @list = @api.file_list(limit: 10)
482
+ @list.loaded # => 10
483
+
484
+ i = 0
485
+ @list.map do |file|
486
+ raise if i >= 19
487
+ i += 1
488
+ file.uuid
489
+ end # => RuntimeError
490
+
491
+ @list.loaded # => 20
492
+ ```
493
+
494
+
495
+ ### Store / delete multiple files at once
496
+
497
+ There are two methods to do so: `Uploadcare::Api#store_files` and
498
+ `Uploadcare::Api#delete_files`. Both of them accept a list of either
499
+ UUIDs or `Uploadcare::Api::File`s:
500
+
501
+ ```ruby
502
+ uuids_to_store = ['f5c477e0-22af-469d-859a-712e14e14361', 'ec72c6eb-5ea8-4057-a009-52ffffb27c94']
503
+ @api.store_files(uuids_to_store)
504
+ # => {'status' => 'ok', 'problems' => {}, 'result' => [{...}, {...}]}
505
+
506
+ old_files = @api.file_list(stored: true, ordering: '-datetime_uploaded', from: "2015-01-01T00:00:00")
507
+ old_files.each_slice(100) do |batch|
508
+ response = @api.delete_files(batch)
509
+ # Do something with response if you need to
510
+ end
511
+ ```
512
+
513
+ Our API supports up to 100 files per request. Calling `#store_files` or
514
+ `#delete_files` with more than 100 files at once will cause an ArgumentError.
515
+
516
+ For more details see our [documentation on batch file storage/deletion](https://uploadcare.com/documentation/rest/#files-storage)
387
517
 
388
518
  ### `Group` object
389
519
 
@@ -433,75 +563,22 @@ are loaded by default.
433
563
  Check out our docs to learn more about
434
564
  [groups](https://uploadcare.com/documentation/rest/#group).
435
565
 
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
448
- # => #<Uploadcare::Api::FileList ....
449
-
450
-
451
- # method :results returns an array of files
452
- @list.results
453
- # => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
454
- # ... 20 of them ...
455
- # #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
456
-
457
566
 
458
- # note, every file is already loaded
459
- @list.results[1].is_loaded?
460
- # => true
567
+ ### Group lists
461
568
 
462
-
463
- # we've also added some shortcuts
464
- @list.to_a
465
- # => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
466
- # ... 20 of them ...
467
- # #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
468
-
469
- @list[3]
470
- # => #<Uploadcare::Api::File ....
471
- ```
472
-
473
- Here's how we handle navigating through pages:
569
+ `Uploadcare::Api::GroupList` represents a group collection. It works in a same way as `Uploadcare::Api::FileList` does, but with groups.
474
570
 
475
571
  ```ruby
476
- @list = @api.files_list 3
477
-
478
- @list.next_page
479
- # => #<Uploadcare::Api::FileList page=4 ....
480
-
481
- @list.previous_page
482
- # => #<Uploadcare::Api::FileList page=2 ....
483
-
484
- @list.go_to 5
485
- # => #<Uploadcare::Api::FileList page=5 ....
486
-
487
- # of course, you can go with any of the methods
488
- # described in our API docs
489
- # total pages
490
- @list.pages
491
- # => 16
572
+ @list = @api.group_list(limit: 10) # => instance of Uploadcare::Api::GroupList
573
+ @list[0] # => instance of Uploadcare::Api::Group
574
+ ```
492
575
 
493
- # current page
494
- @list.page
495
- # => 3
576
+ The only thing that differs is an available options list:
496
577
 
497
- # files per page
498
- @list.per_page
499
- # => 20
578
+ - **:limit** - Controls page size. Accepts values from 1 to 1000, defaults to 100.
579
+ - **:ordering** - Controls the order of returned files. Available values: `datetime_created`, `-datetime_created`. Defaults to `datetime_created`. More info can be found [here](https://uploadcare.com/documentation/rest/#group-groups)
580
+ - **:from** - Specifies the starting point for a collection. Resulting collection will contain files from the given value and to the end in a direction set by an **ordering** option. Accepts either a `DateTime` object or an ISO 8601 string. More info can be found [here](https://uploadcare.com/documentation/rest/#group-groups)
500
581
 
501
- # total files in a project
502
- @list.total
503
- # => 308
504
- ```
505
582
 
506
583
  ### `Project` object
507
584
 
@@ -581,7 +658,7 @@ rescue Uploadcare::Error::RequestError::NotFound => e
581
658
  end
582
659
  ```
583
660
 
584
- Handling any request error (covers all 4xx status codes):
661
+ Handling any request error (covers all 4xx status codes):
585
662
 
586
663
  ```ruby
587
664
  begin
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
2
+ require 'bundler/gem_tasks'
3
3
  require 'rspec/core/rake_task'
4
4
 
5
5
  task :default => :spec
@@ -0,0 +1,36 @@
1
+ # Upgrade notes
2
+
3
+ ## v1.* -> v2.*
4
+
5
+ In 2.* release we've moved to [REST API v0.5][uploadcare-changelog-rest-api-v05] which introduces a new pagination for `/files/` and `/groups/` endpoints, so `Uploadcare::Api::FileList` and `Uploadcare::Api::GroupList` were completely reimplemented.
6
+
7
+ Previously, the file/group list API was:
8
+
9
+ ```ruby
10
+ # creating
11
+ list = api.file_list # => #<Uploadcare::Api::FileList page=1 ...>
12
+
13
+ # accessing files/groups
14
+ list.results # => [#<Uploadcare::Api::File>, ...]
15
+
16
+ # pagination
17
+ list.next_page # => #<Uploadcare::Api::FileList page=2 ...>
18
+ list.previous_page # => #<Uploadcare::Api::FileList page=1 ...>
19
+ list.go_to 5 # => #<Uploadcare::Api::FileList page=5 ...>
20
+
21
+ # metadata
22
+ list.pages # => 15
23
+ list.page # => 5
24
+ list.total # => 308 (files in a project)
25
+ ```
26
+
27
+ It **won't work anymore**. For the details on the new file/group lists interface see [readme][readme]
28
+
29
+ The core features of the new file/group list API are:
30
+
31
+ - transparent pagination via enumerable interface
32
+ - loading objects on demand
33
+ - ordering, filtering and slicing
34
+
35
+ [uploadcare-changelog-rest-api-v05]: https://uploadcare.com/changelog/tag/rest-api#rest-api-version-05
36
+ [readme]: https://github.com/uploadcare/uploadcare-ruby#file-lists