uploadcare-ruby 2.1.2 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/gem-push.yml +20 -0
  3. data/.github/workflows/ruby.yml +52 -0
  4. data/.gitignore +13 -6
  5. data/.rspec +2 -0
  6. data/.rubocop.yml +20 -0
  7. data/.yardopts +4 -0
  8. data/CHANGELOG.md +24 -51
  9. data/DEVELOPMENT.md +18 -0
  10. data/Gemfile +2 -0
  11. data/LICENSE +1 -1
  12. data/README.md +164 -578
  13. data/Rakefile +5 -5
  14. data/bin/console +15 -0
  15. data/bin/setup +8 -0
  16. data/lib/uploadcare.rb +36 -32
  17. data/lib/uploadcare/api/api.rb +25 -0
  18. data/lib/uploadcare/client/file_client.rb +44 -0
  19. data/lib/uploadcare/client/file_list_client.rb +46 -0
  20. data/lib/uploadcare/client/group_client.rb +45 -0
  21. data/lib/uploadcare/client/multipart_upload/chunks_client.rb +46 -0
  22. data/lib/uploadcare/client/multipart_upload_client.rb +62 -0
  23. data/lib/uploadcare/client/project_client.rb +18 -0
  24. data/lib/uploadcare/client/rest_client.rb +73 -0
  25. data/lib/uploadcare/client/rest_group_client.rb +23 -0
  26. data/lib/uploadcare/client/upload_client.rb +35 -0
  27. data/lib/uploadcare/client/uploader_client.rb +93 -0
  28. data/lib/uploadcare/client/webhook_client.rb +43 -0
  29. data/lib/uploadcare/concern/error_handler.rb +54 -0
  30. data/lib/uploadcare/concern/throttle_handler.rb +25 -0
  31. data/lib/uploadcare/concern/upload_error_handler.rb +32 -0
  32. data/lib/uploadcare/entity/decorator/paginator.rb +81 -0
  33. data/lib/uploadcare/entity/entity.rb +18 -0
  34. data/lib/uploadcare/entity/file.rb +81 -0
  35. data/lib/uploadcare/entity/file_list.rb +30 -0
  36. data/lib/uploadcare/entity/group.rb +41 -0
  37. data/lib/uploadcare/entity/group_list.rb +24 -0
  38. data/lib/uploadcare/entity/project.rb +13 -0
  39. data/lib/uploadcare/entity/uploader.rb +73 -0
  40. data/lib/uploadcare/entity/webhook.rb +14 -0
  41. data/lib/uploadcare/exception/request_error.rb +9 -0
  42. data/lib/uploadcare/exception/throttle_error.rb +14 -0
  43. data/lib/uploadcare/param/authentication_header.rb +25 -0
  44. data/lib/uploadcare/param/param.rb +10 -0
  45. data/lib/uploadcare/param/secure_auth_header.rb +37 -0
  46. data/lib/uploadcare/param/simple_auth_header.rb +14 -0
  47. data/lib/uploadcare/param/upload/signature_generator.rb +24 -0
  48. data/lib/uploadcare/param/upload/upload_params_generator.rb +23 -0
  49. data/lib/uploadcare/param/user_agent.rb +21 -0
  50. data/lib/uploadcare/ruby/version.rb +5 -0
  51. data/uploadcare-ruby.gemspec +50 -37
  52. metadata +98 -113
  53. data/.travis.yml +0 -26
  54. data/UPGRADE_NOTES.md +0 -36
  55. data/lib/uploadcare/api.rb +0 -26
  56. data/lib/uploadcare/api/file_api.rb +0 -7
  57. data/lib/uploadcare/api/file_list_api.rb +0 -19
  58. data/lib/uploadcare/api/file_storage_api.rb +0 -34
  59. data/lib/uploadcare/api/group_api.rb +0 -38
  60. data/lib/uploadcare/api/group_list_api.rb +0 -17
  61. data/lib/uploadcare/api/project_api.rb +0 -9
  62. data/lib/uploadcare/api/raw_api.rb +0 -38
  63. data/lib/uploadcare/api/uploading_api.rb +0 -71
  64. data/lib/uploadcare/api/uploading_api/upload_params.rb +0 -72
  65. data/lib/uploadcare/api/validators/file_list_options_validator.rb +0 -73
  66. data/lib/uploadcare/api/validators/group_list_options_validator.rb +0 -49
  67. data/lib/uploadcare/errors/errors.rb +0 -64
  68. data/lib/uploadcare/resources/file.rb +0 -164
  69. data/lib/uploadcare/resources/file_list.rb +0 -14
  70. data/lib/uploadcare/resources/group.rb +0 -115
  71. data/lib/uploadcare/resources/group_list.rb +0 -14
  72. data/lib/uploadcare/resources/project.rb +0 -13
  73. data/lib/uploadcare/resources/resource_list.rb +0 -83
  74. data/lib/uploadcare/rest/auth/auth.rb +0 -31
  75. data/lib/uploadcare/rest/auth/secure.rb +0 -43
  76. data/lib/uploadcare/rest/auth/simple.rb +0 -16
  77. data/lib/uploadcare/rest/connections/api_connection.rb +0 -53
  78. data/lib/uploadcare/rest/connections/upload_connection.rb +0 -22
  79. data/lib/uploadcare/rest/middlewares/auth_middleware.rb +0 -24
  80. data/lib/uploadcare/rest/middlewares/parse_json_middleware.rb +0 -33
  81. data/lib/uploadcare/rest/middlewares/raise_error_middleware.rb +0 -21
  82. data/lib/uploadcare/utils/parser.rb +0 -71
  83. data/lib/uploadcare/utils/user_agent.rb +0 -44
  84. data/lib/uploadcare/version.rb +0 -3
  85. data/spec/api/file_list_api_spec.rb +0 -95
  86. data/spec/api/file_storage_api_spec.rb +0 -88
  87. data/spec/api/group_list_api_spec.rb +0 -59
  88. data/spec/api/raw_api_spec.rb +0 -25
  89. data/spec/api/uploading_api/upload_params_spec.rb +0 -99
  90. data/spec/api/uploading_api_spec.rb +0 -59
  91. data/spec/resources/file_list_spec.rb +0 -25
  92. data/spec/resources/file_spec.rb +0 -223
  93. data/spec/resources/group_list_spec.rb +0 -25
  94. data/spec/resources/group_spec.rb +0 -101
  95. data/spec/resources/operations_spec.rb +0 -59
  96. data/spec/resources/project_spec.rb +0 -21
  97. data/spec/rest/api_connection_spec.rb +0 -68
  98. data/spec/rest/auth/secure_spec.rb +0 -66
  99. data/spec/rest/auth/simple_spec.rb +0 -31
  100. data/spec/rest/errors_spec.rb +0 -75
  101. data/spec/rest/upload_connection_spec.rb +0 -19
  102. data/spec/shared/resource_list.rb +0 -188
  103. data/spec/spec_helper.rb +0 -54
  104. data/spec/uploadcare_spec.rb +0 -43
  105. data/spec/utils/parser_spec.rb +0 -85
  106. data/spec/utils/user_agent_spec.rb +0 -46
  107. data/spec/view.png +0 -0
  108. data/spec/view2.jpg +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 228937684b8d8c141f9e9d9333b938631f3d8a581164d119925815dad392c6e7
4
- data.tar.gz: 3bcd8e452aa580499afcd736beb03fb1c6eb8a91e0ea1d17759f2947ac925292
3
+ metadata.gz: 4bdafd33b7c8ae9c4497155542a87b3b1797d72828d9be8649c74b731afefe10
4
+ data.tar.gz: e88de8ba3a43eb0ed7fa878d34831a11dadd7e1d103ea9781beed93bcd9b5eb9
5
5
  SHA512:
6
- metadata.gz: c0d81628907eb8d0c8db49d63c23b1a4c4049eae305536a21cb553a057b2efda95659232ddb67467749b9e24708068f8e3564987eeca6c398be56446ea462542
7
- data.tar.gz: 838ff37a6ecdbb783fb9677694fc28985ec83054dabaf10a3c932de5aab22d8129612ebdaa6a13dbdba5d8c624fe601a4f6e679763c7c5a30ae345707d378dcb
6
+ metadata.gz: f7adc9b72cf7587f09d6ffc1f3eea789d3c2c7ccb747b364b254a89783449aac38185a0cfcde5c7c89088eee658629804d606a1111d213e97433bc4727b31f2b
7
+ data.tar.gz: e88dc8694ac67b3eefba88218b111a5d51f8696e3dc9de9d3f46cf3bd7dbfadc53589ce2f94e43b78d1279758821edbe206c24a62df4088d126f76b90f200d94
@@ -0,0 +1,20 @@
1
+ name: Publish Gem
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - v*
7
+ jobs:
8
+ build:
9
+ runs-on: ubuntu-latest
10
+
11
+ steps:
12
+ - uses: actions/checkout@v1
13
+
14
+ - name: Release Gem
15
+ if: contains(github.ref, 'refs/tags/v')
16
+ uses: cadwallion/publish-rubygems-action@v1.0.0
17
+ env:
18
+ GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
19
+ RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
20
+ RELEASE_COMMAND: rake release
@@ -0,0 +1,52 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on: [push, pull_request]
11
+
12
+ jobs:
13
+ test:
14
+
15
+ runs-on: ubuntu-latest
16
+ strategy:
17
+ matrix:
18
+ ruby-version: [
19
+ '2.4', '2.5', # eol
20
+ '2.6', '2.7', # maintained
21
+ ]
22
+
23
+ steps:
24
+ - uses: actions/checkout@v2
25
+ - name: Set up Ruby
26
+ uses: ruby/setup-ruby@v1
27
+ with:
28
+ ruby-version: ${{ matrix.ruby-version }}
29
+ bundler-cache: true
30
+ - name: Run tests
31
+ run: bundle exec rake
32
+ env:
33
+ UPLOADCARE_PUBLIC_KEY: demopublickey
34
+ UPLOADCARE_SECRET_KEY: demoprivatekey
35
+
36
+ style-check:
37
+ runs-on: ubuntu-latest
38
+ continue-on-error: true
39
+ strategy:
40
+ matrix:
41
+ ruby-version: ['2.7',]
42
+ steps:
43
+ - uses: actions/checkout@v2
44
+ - name: Set up Ruby
45
+ uses: ruby/setup-ruby@v1
46
+ with:
47
+ ruby-version: ${{ matrix.ruby-version }}
48
+ bundler-cache: true
49
+ - name: Install Rubocop
50
+ run: gem install rubocop
51
+ - name: Check codestyle
52
+ run: rubocop
data/.gitignore CHANGED
@@ -1,7 +1,14 @@
1
- Gemfile.lock
2
- .ruby-gemset
3
- .ruby-version
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /spec/fixtures/big.jpeg
4
10
 
5
- *.DS_Store
6
- *.gem
7
- coverage
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .byebug_history
14
+ Gemfile.lock
data/.rspec CHANGED
@@ -1 +1,3 @@
1
+ --format documentation
1
2
  --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,20 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
7
+ IneffectiveAccessModifier:
8
+ Enabled: false
9
+
10
+ Metrics/BlockLength:
11
+ Exclude:
12
+ - 'bin/'
13
+ - 'spec/**/*'
14
+ - 'uploadcare-ruby.gemspec'
15
+
16
+ Metrics/MethodLength:
17
+ Max: 20
18
+
19
+ Style/Documentation:
20
+ Enabled: false
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ -
2
+ DEVELOPMENT.md
3
+ CHANGELOG.md
4
+ LICENSE
data/CHANGELOG.md CHANGED
@@ -1,65 +1,38 @@
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
+ ## [Unreleased](https://github.com/uploadcare/uploadcare-ruby/tree/develop)
6
4
 
7
- ## [2.1.2] - 2018-06-11
5
+ [Full Changelog](https://github.com/uploadcare/uploadcare-ruby/compare/0baded5593869f1d741f0fff22c58814970726b2...HEAD)
8
6
 
9
- ### Fixed
10
- - Show Uploadcare::USER_AGENT deprecation warning only when the constant is being used
7
+ ## [3.0.4-dev] 2020-03-19
11
8
 
12
- ## [2.1.1] - 2018-05-24
9
+ - Added better pagination methods for GroupList & FileList
10
+ - Improved documentation and install instructions
11
+ - Added CI
13
12
 
14
- ### Changed
15
- - Allow user to override User-Agent header
16
- - User-Agent format reports gem name, version and environment
17
-
18
- ## 2.1.0 - 2018-04-23 [YANKED]
13
+ ## [3.0.3-dev] 2020-03-13
14
+ - Added better pagination and iterators for GroupList & FileList
19
15
 
20
- ## 2.0.0 - 2017-09-26
16
+ ## [3.0.2-dev] 2020-03-11
21
17
 
22
- There are **breaking** changes in this release, please read [upgrade notes](UPGRADE_NOTES.md#v1---v2)
18
+ - Expanded File and Group entities
19
+ - Changed user agent syntax
23
20
 
24
- ### Added
25
- - Support for `store` flag in [Upload API](https://uploadcare.com/documentation/upload/) methods
26
- - Methods to store/delete multiple files at once: `Uploadcare::Api#store_files` & `Uploadcare::Api#delete_files`
21
+ ## [3.0.1-dev] 2020-03-11
27
22
 
23
+ - Added Upload/group functionality
24
+ - Added user API
25
+ - Added user agent
26
+ - Isolated clients, entities and concerns
27
+ - Expanded documentation
28
+ ## [3.0.0-dev] 2020-02-18
28
29
  ### Changed
29
- - Upgraded to REST API v0.5
30
- - All POST/PUT/DELETE params are now being sent as JSON instead of being form-encoded
31
- - Pagination implementation for files and groups
32
-
33
-
34
- ### [1.2.1] - 2018-05-24
35
-
36
- ### Changed
37
- - Allow user to override User-Agent header
38
- - User-Agent format reports gem name, version and environment
39
-
40
-
41
- ## 1.1.0 - 2017-03-21
30
+ - Rewrote gem from scratch
42
31
 
43
32
  ### Added
44
- - Added to new methods to `Uploadcare::Api::File`, `#internal_copy` and `#external_copy`.
45
- - 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)
46
-
47
- ### Fixed
48
- - Fixed middleware names that could break other gems ([#13](https://github.com/uploadcare/uploadcare-ruby/issues/13)).
49
-
50
- ### Deprecated
51
- - `Uploadcare::Api::File#copy` in favor of `#internal_copy` and `#external_copy`.
52
-
53
-
54
- ## 1.0.6, 2017-01-30
55
-
56
- ### Added
57
- - Ruby version and public API key sent via User-Agent header (can be overriden in config)
58
-
59
- ### Fixed
60
- - Incorrect dependencies
61
-
62
33
 
63
- [Unreleased]: https://github.com/uploadcare/uploadcare-ruby/compare/v2.1.1...HEAD
64
- [2.1.1]: https://github.com/uploadcare/uploadcare-ruby/compare/v2.0.0...v2.1.1
65
- [1.2.1]: https://github.com/uploadcare/uploadcare-ruby/compare/6dde...v1.2.1
34
+ - Client wrappers for REST API
35
+ - Serializers for REST API
36
+ - Client wrappers for Upload API
37
+ - Serializers for Upload API
38
+ - rdoc documentation
data/DEVELOPMENT.md ADDED
@@ -0,0 +1,18 @@
1
+ ## How to contribute
2
+
3
+ https://github.com/uploadcare/.github/blob/master/CONTRIBUTING.md
4
+
5
+ ## Useful docs
6
+
7
+ ### Uploadcare:
8
+
9
+ * https://uploadcare.com/docs/api_reference/
10
+ * https://uploadcare.com/api-refs/rest-api/
11
+
12
+ ### ApiStruct:
13
+
14
+ * https://github.com/rubygarage/api_struct/
15
+
16
+ ## Uploadcare-ruby gem architecture
17
+
18
+ ![http://www.plantuml.com/plantuml/uml/bPHDRzj838Rl-XM4zn1WiHRsOoyR88iuXOFcKWHDqcimaLxBquvcL9AowtxyzqepqYGrYY050cH69X-IByav5pMiVUkAed96X5QTfIy54T7jrWgDVrx16rE1De6fGKkzW2NQB9VB-7_zoRIT0zLmQ8oYfXQw3RMS9ZFgEnTCFLqsrk7UMT7YsnnsxTNguIZoNNIg38DMrrU4P2DWQvpz32wZzPtqnha3MStXXH9rIe8qq2jduPMSEO3_Y7x6rzJ0WwD3fjOK7jwZKg4DXvOOqWKlIB6klAZn5JBPS3v7UOQJ83j9-RnAASsNQHNlPsTdFv3iKJu9yJjT1gPBwp0ZHGCZdMDqHPP-LkPH-YGIjSQR1aACZpr4HnMFElApudwryMyXeUSAzDl5nSMGn0XIUB71HqLJrosacAj_4vHwGKtMqp_bd-LVongxH-0DU6ShJFMyCsn3BxI5wy1JQnDudS77LMJTfdfNjUaKFDnrb4Uc23KgMsDWXprfI7lICPHEKj4d4PBqe0SpfXmyTHnsjivZTZBDQlca8S5NO2zJ2Mlcm5GPhRPWYMEB-6ax71tusG8MY-Xu7praiI0sLY13dpceEYHuDBQRk6KCxTbBdc7QMzrI5MhGlt__bx4f-BqcZlNDm5Rp0K9cAZbcgcmH9uTxJrR9DCRw56-_XSSEF7vke4SvFSfnRTYqUJmXo5qqd5VDFhZfVKeRFIH4kwdthszlhwaRAL6gED2MCVJay8A1pfDyOnjlj6VCCGBBcEMgiuFFr__dD-mqx_VJ1sZ-DTPKf4joc_RlzBdNosC_sHLvOSe9ESFAUVIBYLWcwOeyJ-NCE8Ul-zPz1m00](http://www.plantuml.com/plantuml/svg/bPHDRzj838Rl-XM4zn1WiHRsOoyR88iuXOFcKWHDqcimaLxBquvcL9AowtxyzqepqYGrYY050cH69X-IByav5pMiVUkAed96X5QTfIy54T7jrWgDVrx16rE1De6fGKkzW2NQB9VB-7_zoRIT0zLmQ8oYfXQw3RMS9ZFgEnTCFLqsrk7UMT7YsnnsxTNguIZoNNIg38DMrrU4P2DWQvpz32wZzPtqnha3MStXXH9rIe8qq2jduPMSEO3_Y7x6rzJ0WwD3fjOK7jwZKg4DXvOOqWKlIB6klAZn5JBPS3v7UOQJ83j9-RnAASsNQHNlPsTdFv3iKJu9yJjT1gPBwp0ZHGCZdMDqHPP-LkPH-YGIjSQR1aACZpr4HnMFElApudwryMyXeUSAzDl5nSMGn0XIUB71HqLJrosacAj_4vHwGKtMqp_bd-LVongxH-0DU6ShJFMyCsn3BxI5wy1JQnDudS77LMJTfdfNjUaKFDnrb4Uc23KgMsDWXprfI7lICPHEKj4d4PBqe0SpfXmyTHnsjivZTZBDQlca8S5NO2zJ2Mlcm5GPhRPWYMEB-6ax71tusG8MY-Xu7praiI0sLY13dpceEYHuDBQRk6KCxTbBdc7QMzrI5MhGlt__bx4f-BqcZlNDm5Rp0K9cAZbcgcmH9uTxJrR9DCRw56-_XSSEF7vke4SvFSfnRTYqUJmXo5qqd5VDFhZfVKeRFIH4kwdthszlhwaRAL6gED2MCVJay8A1pfDyOnjlj6VCCGBBcEMgiuFFr__dD-mqx_VJ1sZ-DTPKf4joc_RlzBdNosC_sHLvOSe9ESFAUVIBYLWcwOeyJ-NCE8Ul-zPz1m00)
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in uploadcare-ruby.gemspec
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2018 Uploadcare, LLC
3
+ Copyright (c) 2021 Uploadcare Inc.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,117 +1,79 @@
1
- [![Build Status][travis-img]][travis]
2
- [![Coverage Status][coverals-img]][coverals]
1
+ # Ruby integration for Uploadcare
2
+
3
+ ![license](https://img.shields.io/badge/license-MIT-brightgreen.svg)
4
+ [![Build Status][actions-img]][actions-badge]
3
5
  [![Uploadcare stack on StackShare][stack-img]][stack]
6
+ <!--[![Coverage Status][coverals-img]][coverals]-->
4
7
 
5
- [travis-img]: https://secure.travis-ci.org/uploadcare/uploadcare-ruby.svg?branch=master
6
- [travis]: http://travis-ci.org/uploadcare/uploadcare-ruby
7
- [coverals-img]: https://coveralls.io/repos/github/uploadcare/uploadcare-ruby/badge.svg?branch=master
8
- [coverals]: https://coveralls.io/github/uploadcare/uploadcare-ruby?branch=master
8
+ [actions-badge]: https://github.com/uploadcare/uploadcare-ruby/actions/workflows/ruby.yml
9
+ [actions-img]: https://github.com/uploadcare/uploadcare-ruby/actions/workflows/ruby.yml/badge.svg
10
+ [coverals-img]: https://coveralls.io/repos/github/uploadcare/uploadcare-ruby/badge.svg?branch=main
11
+ [coverals]: https://coveralls.io/github/uploadcare/uploadcare-ruby?branch=main
9
12
  [stack-img]: https://img.shields.io/badge/tech-stack-0690fa.svg?style=flat
10
13
  [stack]: https://stackshare.io/uploadcare/stacks/
11
14
 
12
- A [Ruby](https://www.ruby-lang.org/en/) wrapper for [Uploadcare](https://uploadcare.com).
15
+
16
+ Uploadcare Ruby integration handles uploads and further operations with files by
17
+ wrapping Upload and REST APIs.
18
+
19
+ * [Installation](#installation)
20
+ * [Usage](#usage)
21
+ * [Useful links](#useful-links)
22
+
23
+ ## Requirements
24
+ * ruby 2.4+
25
+
26
+ ## Compatibility
27
+
28
+ Note that `uploadcare-ruby` **3.x** is not backward compativble with
29
+ **[2.x](https://github.com/uploadcare/uploadcare-ruby/tree/v2.x)**.
13
30
 
14
31
  ## Installation
15
32
 
16
- Installing `uploadcare-ruby` is quite simple and takes a couple of steps.
17
- First of, add the following line to your app's Gemfile:
33
+ Add this line to your application's Gemfile:
18
34
 
19
35
  ```ruby
20
36
  gem 'uploadcare-ruby'
21
37
  ```
22
38
 
23
- Once you've added the line, execute this:
39
+ And then execute:
24
40
 
25
- ```shell
26
- $ bundle install
27
- ```
41
+ $ bundle
28
42
 
29
- Or that (for manual install):
43
+ If already not, create your project in [Uploadcare dashboard](https://uploadcare.com/dashboard/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby) and copy
44
+ its API keys from there.
30
45
 
31
- ```shell
32
- $ gem install uploadcare-ruby
46
+ Set your Uploadcare keys in config file or through environment variables:
47
+ ```bash
48
+ export UPLOADCARE_PUBLIC_KEY=demopublickey
49
+ export UPLOADCARE_SECRET_KEY=demoprivatekey
33
50
  ```
34
51
 
35
- ## Initialization
36
-
37
- Init is simply done through creating an API object.
52
+ Or configure your app yourself if you are using different way of storing keys.
53
+ Gem configuration is available in `Uploadcare.configuration`. Full list of
54
+ settings can be seen in [`lib/uploadcare.rb`](lib/uploadcare.rb)
38
55
 
39
56
  ```ruby
40
- require 'uploadcare'
41
-
42
- @api = Uploadcare::Api.new # default settings are used
43
-
44
- @api = Uploadcare::Api.new(settings) # using user-defined settings
57
+ # your_config_initializer_file.rb
58
+ Uploadcare.config.public_key = 'demopublickey'
59
+ Uploadcare.config.secret_key = 'demoprivatekey'
45
60
  ```
46
61
 
47
- Here's how the default settings look like:
48
-
49
- ``` ruby
50
- {
51
- public_key: 'demopublickey', # you need to override this
52
- private_key: 'demoprivatekey', # you need to override this
53
- upload_url_base: 'https://upload.uploadcare.com',
54
- api_url_base: 'https://api.uploadcare.com',
55
- static_url_base: 'https://ucarecdn.com',
56
- api_version: '0.5',
57
- cache_files: true,
58
- autostore: :auto,
59
- auth_scheme: :secure
60
- }
61
- ```
62
-
63
- You're free to use both `demopublickey` and `demoprivatekey`
64
- for initial testing purposes. We wipe out files loaded to our
65
- demo account periodically. For a better experience,
66
- consider creating an Uploadcare account. Check out
67
- [this](http://kb.uploadcare.com/article/234-uc-project-and-account)
68
- article to get up an running in minutes.
69
-
70
- Please note, in order to use [Upload API](https://uploadcare.com/documentation/upload/)
71
- you will only need the public key alone. However, using
72
- [REST API](https://uploadcare.com/documentation/rest/) requires you to
73
- use both public and private keys for authentication.
74
- While “private key” is a common way to name a key from an
75
- authentication key pair, the actual thing for our `auth-param` is `secret_key`.
76
-
77
- `:autostore` option allows you to set the default storage
78
- behaviour upon uploads. For more info see [`store` flag][uploads from url] for
79
- uploads via URL and [`UPLOADCARE_STORE` flag][in-body file uploads] for file uploads
80
-
81
- [in-body file uploads]: https://uploadcare.com/documentation/upload/#upload-body
82
- [uploads from url]: https://uploadcare.com/documentation/upload/#from-url
83
-
84
62
  ## Usage
85
63
 
86
- This section contains practical usage examples. Please note,
87
- everything that follows gets way more clear once you've looked
88
- through our docs [intro](https://uploadcare.com/documentation/).
64
+ This section contains practical usage examples. Please note, everything that
65
+ follows gets way more clear once you've looked through our
66
+ [docs](https://uploadcare.com/docs/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby).
89
67
 
90
- ### Basic usage: uploading a single file, manipulations
68
+ ### Uploading and storing a single file
91
69
 
92
70
  Using Uploadcare is simple, and here are the basics of handling files.
93
71
 
94
- First of, create the API object:
95
-
96
- ```ruby
97
- @api = Uploadcare::Api.new(CONFIG)
98
-
99
- ```
100
-
101
- And yeah, now you can upload a file:
102
-
103
72
  ```ruby
104
73
  @file_to_upload = File.open("your-file.png")
105
74
 
106
- @uc_file = @api.upload(@file_to_upload)
107
- # => #<Uploadcare::Api::File ...
108
- ```
109
-
110
- Then, let's check out UUID and URL of the
111
- file you've just uploaded:
75
+ @uc_file = Uploadcare::Uploader.upload(@file_to_upload)
112
76
 
113
- ```ruby
114
- # file uuid (you'd probably want to store those somewhere)
115
77
  @uc_file.uuid
116
78
  # => "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
117
79
 
@@ -120,10 +82,9 @@ file you've just uploaded:
120
82
  # => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/"
121
83
  ```
122
84
 
123
- Your might then want to store or delete the uploaded file.
124
- Storing files could be crucial if you aren't using the
125
- “Automatic file storing” option for your Uploadcare project.
126
- If not stored manually or automatically, files get deleted
85
+ Your might then want to store or delete the uploaded file. Storing files could
86
+ be crucial if you aren't using the “Automatic file storing” option for your
87
+ Uploadcare project. If not stored manually or automatically, files get deleted
127
88
  within a 24-hour period.
128
89
 
129
90
  ```ruby
@@ -136,48 +97,25 @@ within a 24-hour period.
136
97
  # => #<Uploadcare::Api::File ...
137
98
  ```
138
99
 
139
- ### Uploading a file from URL
100
+ ### Uploads
140
101
 
141
- Now, this one is also quick. Just pass your URL into our API
142
- and you're good to go.
102
+ Uploadcare supports multiple ways to upload files:
143
103
 
144
104
  ```ruby
145
- # the smart upload
146
- @file = @api.upload "http://your.awesome/avatar.jpg"
147
- # => #<Uploadcare::Api::File ...
148
-
149
- # use this one if you want to explicitly upload from URL
150
- @file = @api.upload_from_url "http://your.awesome/avatar.jpg"
151
- # => #<Uploadcare::Api::File ...
105
+ # Smart upload - detects type of passed object and picks appropriate upload method
106
+ Uploadcare::Uploader.upload('https://placekitten.com/96/139')
152
107
  ```
153
- Keep in mind that providing invalid URL
154
- will raise `ArgumentError`.
155
-
156
- ### Uploading multiple files
157
108
 
158
- Uploading multiple files is as simple as passing an array
159
- of `File` instances into our API.
109
+ There are explicit ways to select upload type:
160
110
 
161
111
  ```ruby
162
- file1 = File.open("path/to/your/file.png")
163
- file2 = File.open("path/to/your/another-file.png")
164
- files = [file1, file2]
165
-
166
- @uc_files = @api.upload files
167
- # => [#<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40">,
168
- # #<Uploadcare::Api::File uuid="96cdc400-adc3-435b-9c94-04cd87633fbb">]
169
- ```
112
+ files = [File.open('1.jpg'), File.open('1.jpg']
113
+ Uploadcare::Uploader.upload_files(files)
170
114
 
171
- In case of multiple input, the respective output would also be an array.
172
- You can iterate through the array to address to single files.
173
- You might also want to request more info about a file using `load_data`.
115
+ Uploadcare::Uploader.upload_from_url('https://placekitten.com/96/139')
174
116
 
175
- ```ruby
176
- @uc_files[0]
177
- # => #<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40">
178
-
179
- @uc_files[1].load_data
180
- # => #<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}, ....>
117
+ # multipart upload - can be useful for files bigger than 10 mb
118
+ Uploadcare::Uploader.multipart_upload(File.open('big_file.bin'))
181
119
  ```
182
120
 
183
121
  ### Upload options
@@ -189,246 +127,89 @@ You can override global [`:autostore`](#initialization) option for each upload r
189
127
  @api.upload_from_url(url, store: :auto)
190
128
  ```
191
129
 
192
- ### `File` object
193
-
194
- Now that we've already outlined using arrays of `File` instances
195
- to upload multiple files, let's fix on the `File` itself.
196
- It's the the primary object for Uploadcare API.
197
- Basically, it's an avatar for a file you uploaded.
198
- And all the further operations are performed using this avatar,
199
- the `File` object.
200
-
201
- ```ruby
202
- @file_to_upload = File.open("your-file.png")
203
-
204
- @uc_file = @api.upload(@file_to_upload)
205
- # => #<Uploadcare::Api::File ...
206
-
207
- @uc_file.uuid
208
- # => "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
209
-
210
- @uc_file.cdn_url
211
- # => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/"
212
- ```
213
-
214
- Please note, all the data associated with files is only accessible
215
- through separate HTTP requests only. So if you don't specifically
216
- need file data (filenames, image dimensions, etc.), you'll be just
217
- fine with using `:uuid` and `:cdn_url` methods for file output:
218
-
219
- ```erb
220
- <img src="#{@file.cdn_url}"/>
221
- ```
222
-
223
- Great, we've just lowered a precious loading time.
224
- However, if you do need the data, you can always request
225
- it manually:
226
-
227
- ```ruby
228
- @uc_file.load_data
229
- ```
230
-
231
- That way your file object will respond to any method described
232
- in [API docs](https://uploadcare.com/documentation/rest/#file).
233
- Basically, that's an an OpenStruct, so you know what to do:
234
-
235
- ```ruby
236
- @uc_file.original_filename
237
- # => "logo.png"
238
-
239
- @uc_file.image_info
240
- # => {"width"=>397, "geo_location"=>nil, "datetime_original"=>nil, "height"=>81}
241
- ```
242
-
243
- ### `File` object from UUID or CDN URL
244
-
245
- `File` objects are needed to manipulate files on our CDN.
246
- The usual case would be you as a client storing file UUIDs
247
- or CDN URLs somewhere on your side, e.g. in a database.
248
- This is how you can use those to create `File` objects:
249
-
250
- ```ruby
251
- # file object from UUID
252
- @file = @api.file "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
253
- # => #<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
254
-
255
- # file object from CDN URL
256
- @file = @api.file "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/"
257
- # => #<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
258
-
259
- # note, files you generate won't be loaded on init,
260
- # you'll need to load those manually
261
- @file.is_loaded?
262
- # => false
263
- ```
264
-
265
- ### Operations
266
-
267
- Another way to manipulate files on CDN is through operations.
268
- This is particularly useful for images.
269
- We've got on-the-fly crop, resize, rotation, format conversions, and
270
- [more](https://uploadcare.com/documentation/cdn/).
271
- Image operations are there to help you build responsive designs,
272
- generate thumbnails and galleries, change formats, etc.
273
- Currently, this gem has no specific methods for image operations,
274
- we're planning to implement those in further versions.
275
- However, we do support applying image operations through
276
- adding them to CDN URLs. That's an Uploadcare CDN-native
277
- way described in our [docs](https://uploadcare.com/documentation/cdn/).
278
-
130
+ ### Api
131
+ Most methods are also available through `Uploadcare::Api` object:
279
132
  ```ruby
280
- @file = @api.file "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/-/crop/150x150/center/-/format/png/"
281
- # => #<Uploadcare::Api::File uuid="dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
282
-
283
- @file.operations
284
- # => ["crop/150x150/center", "format/png"]
285
-
286
- # note that by default :cdn_url method returns URLs with no operations:
287
- @file.cdn_url
288
- # => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/""
289
-
290
- # you can pass "true" into the :cdn_url method to get URL including operations:
291
- @file.cdn_url(true)
292
- # => "https://ucarecdn.com/a8775cf7-0c2c-44fa-b071-4dd48637ecac/-/crop/150x150/center/-/format/png/"
293
-
294
- # there also are specific methods to either dump or include image operations
295
- # in the output URL:
296
- @file.cdn_url_with_operations
297
- @file.cdn_url_without_operations
133
+ # Same as Uploadcare::Uploader.upload
134
+ Uploadcare::Api.upload('https://placekitten.com/96/139')
298
135
  ```
299
136
 
300
- While there's no operation wrapper, the best way of handling operations
301
- is through adding them to URLs as strings:
302
-
303
- ```ruby
304
- <img src="#{file.cdn_url}-/crop/#{width}x#{height}/center/"/>
305
- # or something like that
306
- ```
137
+ ### Entity object
307
138
 
308
- ### Copying files
139
+ Entities are representations of objects in Uploadcare cloud.
309
140
 
310
- You can also create file copies using our API.
311
- There are multiple ways of creating those.
312
- Also, copying is important for image files because
313
- it allows you to “apply” all the CDN operations
314
- specified in the source URL to a separate static image.
141
+ #### File
315
142
 
316
- First of all, a copy of your file can be put in the Uploadcare storage.
317
- This is called “internal copy”, and here's how it works:
143
+ File entity contains its metadata.
318
144
 
319
145
  ```ruby
320
- @uc_file.internal_copy
321
-
322
- # =>
323
- {
324
- "type"=>"file",
325
- "result"=> {
326
- "uuid"=>"a191a3df-2c43-4939-9590-784aa371ad6d",
327
- "original_file_url"=>"https://ucarecdn.com/a191a3df-2c43-4939-9590-784aa371ad6d/19xldj.jpg",
328
- "image_info"=>nil,
329
- "datetime_stored"=>nil,
330
- "mime_type"=>"application/octet-stream",
331
- "is_ready"=>true,
332
- "url"=>"https://api.uploadcare.com/files/a191a3df-2c43-4939-9590-784aa371ad6d/",
333
- "original_filename"=>"19xldj.jpg",
334
- "datetime_uploaded"=>"2017-02-10T14:14:18.690581Z",
335
- "size"=>0,
336
- "is_image"=>nil,
337
- "datetime_removed"=>nil,
338
- "source"=>"/4ea293d5-153f-422f-a24e-350237109606/"
339
- }
340
- }
341
- ```
342
-
343
- Once the procedure is complete, a copy would be a separate file
344
- with its own UUID and attributes.
345
-
346
- `#internal_copy` can optionally be used with the options hash argument.
347
- The available options are:
348
-
349
- - *store*
350
-
351
- By default a copy is created without “storing”.
352
- Which means it will be deleted within a 24-hour period.
353
- You can make your output copy permanent by passing the
354
- `store: true` option to the `#internal_copy` method.
355
-
356
- Example:
357
-
358
- ```ruby
359
- @uc_file.internal_copy(store: true)
360
- ```
146
+ @file = Uploadcare::File.file('FILE_ID_IN_YOUR_PROJECT')
147
+ {"datetime_removed"=>nil,
148
+ "datetime_stored"=>"2020-01-16T15:03:15.315064Z",
149
+ "datetime_uploaded"=>"2020-01-16T15:03:14.676902Z",
150
+ "image_info"=>
151
+ {"color_mode"=>"RGB",
152
+ "orientation"=>nil,
153
+ "format"=>"JPEG",
154
+ "sequence"=>false,
155
+ "height"=>183,
156
+ "width"=>190,
157
+ "geo_location"=>nil,
158
+ "datetime_original"=>nil,
159
+ "dpi"=>nil},
160
+ "is_image"=>true,
161
+ "is_ready"=>true,
162
+ "mime_type"=>"image/jpeg",
163
+ "original_file_url"=>
164
+ "https://ucarecdn.com/FILE_ID_IN_YOUR_PROJECT/imagepng.jpeg",
165
+ "original_filename"=>"image.png.jpeg",
166
+ "size"=>5345,
167
+ "url"=>
168
+ "https://api.uploadcare.com/files/FILE_ID_IN_YOUR_PROJECT/",
169
+ "uuid"=>"8f64f313-e6b1-4731-96c0-6751f1e7a50a"}
361
170
 
362
- - *strip_operations*
171
+ @file.store # stores file, returns updated metadata
363
172
 
364
- If your file is an image and you applied some operations to it,
365
- then by default the same set of operations is also applied to a copy.
366
- You can override this by passing `strip_operations: true` to the
367
- `#internal_copy` method.
368
-
369
- Example:
370
-
371
- ```ruby
372
- file = @api.file "https://ucarecdn.com/24626d2f-3f23-4464-b190-37115ce7742a/-/resize/50x50/"
373
- file.internal_copy
374
- # => This will trigger POST /files/ with {"source": "https://ucarecdn.com/24626d2f-3f23-4464-b190-37115ce7742a/-/resize/50x50/"} in the body
375
- file.internal_copy(strip_operations: true)
376
- # => This will trigger POST /files/ with {"source": "https://ucarecdn.com/24626d2f-3f23-4464-b190-37115ce7742a/"} in the body
377
- ```
378
-
379
- Another option is copying your file to a custom storage.
380
- We call it “external copy” and here's the usage example:
381
-
382
- ```ruby
383
- @uc_file.external_copy('my_custom_storage_name')
384
-
385
- # =>
386
- {
387
- "type"=>"url",
388
- "result"=>"s3://my_bucket_name/c969be02-9925-4a7e-aa6d-b0730368791c/view.png"
389
- }
173
+ @file.delete #deletes file. Returns updated metadata
390
174
  ```
391
175
 
392
- First argument of the `#external_copy` method is a name of
393
- a custom destination storage for your file.
394
-
395
- There's also an optional second argument — options hash. The available options are:
396
-
397
- - *make_public*
398
-
399
- Make a copy available via public links. Can be either `true` or `false`.
176
+ Metadata of deleted files is stored permanently.
400
177
 
401
- - *pattern*
178
+ #### FileList
402
179
 
403
- Name pattern for a copy. If the parameter is omitted, custom storage pattern is used.
404
-
405
- - *strip_operations*
406
-
407
- Same as for `#internal_copy`
408
-
409
- You might want to learn more about
410
- [storage options](https://uploadcare.com/documentation/storages/) or
411
- [copying files](https://uploadcare.com/documentation/rest/#files-post)
412
- with Uploadcare.
413
-
414
-
415
- ### File lists
416
-
417
- `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.
180
+ `Uploadcare::Entity::FileList` represents the whole collection of files (or it's
181
+ subset) and provides a way to iterate through it, making pagination transparent.
182
+ FileList objects can be created using `Uploadcare::Entity.file_list` method.
418
183
 
419
184
  ```ruby
420
- @list = @api.file_list # => instance of Uploadcare::Api::FileList
185
+ @list = Uploadcare::Entity.file_list
186
+ # Returns instance of Uploadcare::Api::FileList
187
+ <Hashie::Mash
188
+ next=nil
189
+ per_page=100
190
+ previous=nil
191
+ results=[
192
+ # Array of Entity::File
193
+ ]
194
+ total=8>
195
+ # load last page of files
196
+ @files = @list.files
197
+ # load all files
198
+ @all_files = @list.load
421
199
  ```
422
200
 
423
- This method accepts some options to controll which files should be fetched and how they should be fetched:
201
+ This method accepts some options to controll which files should be fetched and
202
+ how they should be fetched:
424
203
 
425
- - **:limit** - Controls page size. Accepts values from 1 to 1000, defaults to 100.
426
- - **:stored** - Can be either `true` or `false`. When true, file list will contain only stored files. When false - only not stored.
427
- - **:removed** - Can be either `true` or `false`. When true, file list will contain only removed files. When false - all except removed. Defaults to false.
428
- - **: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)
429
- - **: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)
204
+ - **:limit** Controls page size. Accepts values from 1 to 1000, defaults to 100.
205
+ - **:stored** Can be either `true` or `false`. When true, file list will contain only stored files. When false only not stored.
206
+ - **:removed** Can be either `true` or `false`. When true, file list will contain only removed files. When false all except removed. Defaults to false.
207
+ - **: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/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby).
208
+ - **: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, accepts non-negative integers (size in bytes). More info can be found [here](https://uploadcare.com/documentation/rest/#file-files/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby).
430
209
 
431
- 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.
210
+ Options used to create a file list can be accessed through `#options` method.
211
+ Note that, once set, they don't affect file fetching process anymore and are
212
+ stored just for your convenience. That is why they are frozen.
432
213
 
433
214
  ```ruby
434
215
  options = {
@@ -438,286 +219,91 @@ options = {
438
219
  from: "2017-01-01T00:00:00",
439
220
  }
440
221
  @list = @api.file_list(options)
441
- @list.options # => same as options hash above, but frozen
442
- ```
443
-
444
- `Uploadcare::Api::FileList` implements Enumerable interface and holds a collection of `Uploadcare::Api::File` objects, as well as some meta information.
445
-
446
- ```ruby
447
- @list = @api.file_list
448
- @list.total # => 1977
449
- @list.meta # => {
450
- # "next"=> "https://api.uploadcare.com/files/?from=2017-03-09T10%3A30%3A01.877590%2B00%3A00&offset=0",
451
- # "previous"=>nil,
452
- # "total"=>1977,
453
- # "per_page"=>100
454
- # }
455
-
456
- # Enumerable interface
457
- @list.first(5) # => array of 5 x Uploadcare::Api::File
458
- @list.each{|file| puts file.original_filename}
459
- @list.map{|file| file.uuid}
460
- @list.reduce(0){|overall_size, file| overall_size += file.size}
461
- # ...
462
222
  ```
463
223
 
464
- 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.
465
-
466
- Currently loaded files are available through `FileList#objects`. `FileList#loaded` method returns the number of currently loaded files.
467
-
224
+ To simply get all associated objects:
468
225
  ```ruby
469
- @list = @api.file_list(limit: 5) # will load first 5 files
470
- @list.loaded # => 5
471
- @list.fully_loaded? # => false
472
-
473
- @list.objects # => array of 5 x Uploadcare::Api::File
474
- @list.objects[4] # => #<Uploadcare::Api::File ...>
475
- @list.objects[5] # => nil (since 6th file is not yet loaded)
476
-
477
- @list[4] # won't load anything, because 5 files are already loaded
478
- @list[5] # will load the next page
479
- @list.loaded # => 10
480
-
481
- # Note that the example below will load all the files left, page by page,
482
- # and return the count only when they all will be loaded
483
- @list.count # => 132
484
- @list.fully_loaded? # => true
226
+ @list.all # => returns Array of Files
485
227
  ```
486
228
 
487
- Loaded files are preserved when `break` statement or an exception occures inside a block provided to #each, #map, etc.
229
+ ##### Pagination
488
230
 
231
+ Initially, `FileList` is a paginated collection. It can be navigated using following methods:
489
232
  ```ruby
490
- @list = @api.file_list(limit: 10)
491
- @list.loaded # => 10
492
-
493
- i = 0
494
- @list.map do |file|
495
- raise if i >= 19
496
- i += 1
497
- file.uuid
498
- end # => RuntimeError
499
-
500
- @list.loaded # => 20
233
+ @file_list = Uploadcare::Entity::FileList.file_list
234
+ # Let's assume there are 250 files in cloud. By default, UC loads 100 files. To get next 100 files, do:
235
+ @next_page = @file_list.next_page
236
+ # To get previous page:
237
+ @previous_page = @next_page.previous_page
501
238
  ```
502
239
 
503
-
504
- ### Store / delete multiple files at once
505
-
506
- There are two methods to do so: `Uploadcare::Api#store_files` and
507
- `Uploadcare::Api#delete_files`. Both of them accept a list of either
508
- UUIDs or `Uploadcare::Api::File`s:
509
-
240
+ Alternatively, it's possible to iterate through full list of groups or files with `each`:
510
241
  ```ruby
511
- uuids_to_store = ['f5c477e0-22af-469d-859a-712e14e14361', 'ec72c6eb-5ea8-4057-a009-52ffffb27c94']
512
- @api.store_files(uuids_to_store)
513
- # => {'status' => 'ok', 'problems' => {}, 'result' => [{...}, {...}]}
514
-
515
- old_files = @api.file_list(stored: true, ordering: '-datetime_uploaded', from: "2015-01-01T00:00:00")
516
- old_files.each_slice(100) do |batch|
517
- response = @api.delete_files(batch)
518
- # Do something with response if you need to
242
+ @list.each do |file|
243
+ p file.url
519
244
  end
520
245
  ```
521
246
 
522
- Our API supports up to 100 files per request. Calling `#store_files` or
523
- `#delete_files` with more than 100 files at once will cause an ArgumentError.
524
-
525
- For more details see our [documentation on batch file storage/deletion](https://uploadcare.com/documentation/rest/#files-storage)
247
+ #### Group
526
248
 
527
- ### `Group` object
528
-
529
- Groups are structures intended to organize sets of separate files.
530
- Each group is assigned UUID.
531
- Note, group UUIDs include a `~#{files_count}` part at the end.
249
+ Groups are structures intended to organize sets of separate files. Each group is
250
+ assigned UUID. Note, group UUIDs include a `~#{files_count}` part at the end.
532
251
  That's a requirement of our API.
533
252
 
534
253
  ```ruby
535
254
  # group can be created from an array of Uploadcare files
536
255
  @files_ary = [@file, @file2]
537
- @files = @api.upload @files_ary
538
- @group = @api.create_group @files
539
- # => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
540
-
541
- # another way to from a group is via an array of strings holding UUIDs
542
- @uuids_ary = ["c969be02-9925-4a7e-aa6d-b0730368791c", "c969be02-9925-4a7e-aa6d-b0730368791c"]
543
- @group = @api.create_group @uuids_ary
544
- # => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
545
-
546
- # also, you can create a group object via group UUID
547
- @group_uloaded = @api.group "#{uuid}"
256
+ @files = Uploadcare::Uploader.upload @files_ary
257
+ @group = Uploadcare::Group.create @files
548
258
  ```
549
259
 
550
- As with files, groups created via UUIDs are not loaded by default.
551
- You need to load the data manually, as it requires a separate
552
- HTTP GET request. New groups created with the `:create_group` method
553
- are loaded by default.
260
+ #### GroupList
261
+ `GroupList` is a list of `Group`
554
262
 
555
263
  ```ruby
556
- @group = @api.group "#{uuid}"
557
-
558
- @group.is_loaded?
559
- # => false
560
-
561
- @group.load_data
562
- # => #<Uploadcare::Api::Group uuid="0d192d66-c7a6-4465-b2cd-46716c5e3df3~2", files_count=2 ...
563
-
564
- # once a group is loaded, you can use any methods described in our API docs
565
- # the files within a loaded group are loaded by default
566
- @group.files
567
- # => [#<Uploadcare::Api::File uuid="24626d2f-3f23-4464-b190-37115ce7742a" ...>,
568
- # ... #{files_count} of them ...
569
- # #<Uploadcare::Api::File uuid="7bb9efa4-05c0-4f36-b0ef-11a4221867f6" ...>]
264
+ @group_list = Uploadcare::GroupList.list
265
+ # To get an array of groups:
266
+ @groups = @group_list.all
570
267
  ```
571
268
 
572
- Check out our docs to learn more about
573
- [groups](https://uploadcare.com/documentation/rest/#group).
574
-
269
+ This is a paginated list, so [pagination](#Pagination) methods apply
575
270
 
576
- ### Group lists
271
+ #### Webhook
272
+ https://uploadcare.com/docs/api_reference/rest/webhooks/
577
273
 
578
- `Uploadcare::Api::GroupList` represents a group collection. It works in a same way as `Uploadcare::Api::FileList` does, but with groups.
274
+ You can use webhooks to provide notifications about your uploads to target urls.
275
+ This gem lets you create and manage webhooks.
579
276
 
580
277
  ```ruby
581
- @list = @api.group_list(limit: 10) # => instance of Uploadcare::Api::GroupList
582
- @list[0] # => instance of Uploadcare::Api::Group
278
+ Uploadcare::Webhook.create('example.com/listen', event: 'file.uploaded')
583
279
  ```
584
280
 
585
- The only thing that differs is an available options list:
281
+ #### Project
586
282
 
587
- - **:limit** - Controls page size. Accepts values from 1 to 1000, defaults to 100.
588
- - **: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)
589
- - **: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)
590
-
591
-
592
- ### `Project` object
593
-
594
- `Project` provides basic info about the connected Uploadcare project.
595
- That object is also an OpenStruct, so every methods out of
596
- [these](https://uploadcare.com/documentation/rest/#project) will work.
283
+ `Project` provides basic info about the connected Uploadcare project. That
284
+ object is also an Hashie::Mash, so every methods out of
285
+ [these](https://uploadcare.com/documentation/rest/#project/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby) will work.
597
286
 
598
287
  ```ruby
599
- project = @api.project
288
+ @project = Uploadcare::Project.project
600
289
  # => #<Uploadcare::Api::Project collaborators=[], name="demo", pub_key="demopublickey", autostore_enabled=true>
601
290
 
602
- project.name
291
+ @project.name
603
292
  # => "demo"
604
293
 
605
- p.collaborators
294
+ @project.collaborators
606
295
  # => []
607
296
  # while that one was empty, it usually goes like this:
608
297
  # [{"email": collaborator@gmail.com, "name": "Collaborator"}, {"email": collaborator@gmail.com, "name": "Collaborator"}]
609
298
  ```
610
299
 
611
- ### Raw API
612
-
613
- Raw API is a simple interface allowing you to make
614
- custom requests to Uploadcare REST API.
615
- It's mainly used when you want a low-level control
616
- over your app.
617
-
618
- ```ruby
619
- # here's how you make any requests
620
- @api.request :get, "/files/", {page: 2}
621
-
622
- # and there also are shortcuts for methods
623
- @api.get '/files', {page: 2}
624
-
625
- @api.post ...
626
-
627
- @api.put ...
628
-
629
- @api.delete ...
630
-
631
- ```
632
-
633
- All of the raw API methods return a parsed JSON response
634
- or raise an error (handling those is done on your side in the case).
635
-
636
- ### Error handling
637
-
638
- Starting from the version 1.0.2, we've got have custom exceptions
639
- that will be raised in case the Uploadcare service returns
640
- something with 4xx or 5xx HTTP status.
641
-
642
- Check out the list of custom errors:
643
-
644
- ```ruby
645
- 400 => Uploadcare::Error::RequestError::BadRequest,
646
- 401 => Uploadcare::Error::RequestError::Unauthorized,
647
- 403 => Uploadcare::Error::RequestError::Forbidden,
648
- 404 => Uploadcare::Error::RequestError::NotFound,
649
- 406 => Uploadcare::Error::RequestError::NotAcceptable,
650
- 408 => Uploadcare::Error::RequestError::RequestTimeout,
651
- 422 => Uploadcare::Error::RequestError::UnprocessableEntity,
652
- 429 => Uploadcare::Error::RequestError::TooManyRequests,
653
- 500 => Uploadcare::Error::ServerError::InternalServerError,
654
- 502 => Uploadcare::Error::ServerError::BadGateway,
655
- 503 => Uploadcare::Error::ServerError::ServiceUnavailable,
656
- 504 => Uploadcare::Error::ServerError::GatewayTimeout
657
- ```
658
-
659
- That's how you handle a particular error
660
- (in this case, a “404: Not Found” error):
661
-
662
- ```ruby
663
- begin
664
- @connection.send :get, '/random_url/', {}
665
- rescue Uploadcare::Error::RequestError::NotFound => e
666
- nil
667
- end
668
- ```
669
-
670
- Handling any request error (covers all 4xx status codes):
671
-
672
- ```ruby
673
- begin
674
- @connection.send :get, '/random_url/', {}
675
- rescue Uploadcare::Error::RequestError => e
676
- nil
677
- end
678
- ```
679
-
680
- Handling any Uploadcare service error:
681
-
682
- ```ruby
683
- begin
684
- @connection.send :get, '/random_url/', {}
685
- rescue Uploadcare::Error => e
686
- nil
687
- end
688
- ```
689
-
690
- Since many of the above listed things depend on Uploadcare servers,
691
- errors might occasionally occur. Be prepared to handle those.
692
-
693
- ## Testing
694
-
695
- For testing purposes, run `bundle exec rspec`.
696
-
697
- Please note, if you're willing to run tests using your own keys,
698
- make a `spec/config.yml` file containing the following:
699
-
700
- ```yaml
701
- public_key: 'PUBLIC KEY'
702
- private_key: 'PRIVATE KEY'
703
- ```
704
-
705
- ## Contributors
706
-
707
- This is open source so fork, hack, request a pull — get a discount.
708
-
709
- - [@romanonthego](https://github.com/romanonthego)
710
- - [@vizvamitra](https://github.com/vizvamitra)
711
- - [@dmitry-mukhin](https://github.com/dmitry-mukhin)
712
- - [@zenati](https://github.com/zenati)
713
- - [@renius](https://github.com/renius)
714
-
715
- ## Security issues
716
-
717
- If you think you ran into something in Uploadcare libraries
718
- which might have security implications, please hit us up at
719
- [bugbounty@uploadcare.com](mailto:bugbounty@uploadcare.com)
720
- or Hackerone.
300
+ ## Useful links
721
301
 
722
- We'll contact you personally in a short time to fix an issue
723
- through co-op and prior to any public disclosure.
302
+ * [Development](https://github.com/uploadcare/uploadcare-ruby/blob/main/DEVELOPMENT.md)
303
+ * [Uploadcare documentation](https://uploadcare.com/docs/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby)
304
+ * [Upload API reference](https://uploadcare.com/api-refs/upload-api/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby)
305
+ * [REST API reference](https://uploadcare.com/api-refs/rest-api/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby)
306
+ * [Changelog](./CHANGELOG.md)
307
+ * [Contributing guide](https://github.com/uploadcare/.github/blob/master/CONTRIBUTING.md)
308
+ * [Security policy](https://github.com/uploadcare/uploadcare-ruby/security/policy)
309
+ * [Support](https://github.com/uploadcare/.github/blob/master/SUPPORT.md)