uploadcare-ruby 3.0.3 → 3.1.1

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.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +13 -0
  3. data/CHANGELOG.md +21 -7
  4. data/README.md +348 -34
  5. data/lib/uploadcare/client/conversion/base_conversion_client.rb +59 -0
  6. data/lib/uploadcare/client/conversion/document_conversion_client.rb +41 -0
  7. data/lib/uploadcare/client/conversion/video_conversion_client.rb +46 -0
  8. data/lib/uploadcare/client/file_list_client.rb +4 -4
  9. data/lib/uploadcare/client/group_client.rb +1 -1
  10. data/lib/uploadcare/client/multipart_upload/chunks_client.rb +16 -5
  11. data/lib/uploadcare/client/multipart_upload_client.rb +14 -12
  12. data/lib/uploadcare/client/project_client.rb +1 -1
  13. data/lib/uploadcare/client/rest_client.rb +6 -5
  14. data/lib/uploadcare/client/rest_group_client.rb +2 -2
  15. data/lib/uploadcare/client/upload_client.rb +8 -0
  16. data/lib/uploadcare/client/uploader_client.rb +25 -17
  17. data/lib/uploadcare/client/webhook_client.rb +9 -5
  18. data/lib/uploadcare/concern/error_handler.rb +2 -2
  19. data/lib/uploadcare/entity/conversion/base_converter.rb +36 -0
  20. data/lib/uploadcare/entity/conversion/document_converter.rb +15 -0
  21. data/lib/uploadcare/entity/conversion/video_converter.rb +15 -0
  22. data/lib/uploadcare/entity/decorator/paginator.rb +5 -7
  23. data/lib/uploadcare/entity/file.rb +40 -4
  24. data/lib/uploadcare/entity/file_list.rb +1 -0
  25. data/lib/uploadcare/entity/group.rb +1 -2
  26. data/lib/uploadcare/entity/uploader.rb +11 -3
  27. data/lib/uploadcare/exception/conversion_error.rb +8 -0
  28. data/lib/uploadcare/exception/throttle_error.rb +2 -0
  29. data/lib/uploadcare/param/conversion/document/processing_job_url_builder.rb +39 -0
  30. data/lib/uploadcare/param/conversion/video/processing_job_url_builder.rb +64 -0
  31. data/lib/uploadcare/param/user_agent.rb +1 -1
  32. data/lib/uploadcare/ruby/version.rb +1 -1
  33. data/uploadcare-ruby.gemspec +1 -1
  34. metadata +14 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4bdafd33b7c8ae9c4497155542a87b3b1797d72828d9be8649c74b731afefe10
4
- data.tar.gz: e88de8ba3a43eb0ed7fa878d34831a11dadd7e1d103ea9781beed93bcd9b5eb9
3
+ metadata.gz: 50f31851c1ededcc13b14099d5451a2cae8ad1a1c36ce45bbe47447fc536dc3a
4
+ data.tar.gz: 0c5eecb5f2583645455230fb5679c65612e322a4dcae82cae97e5b9fc702dab4
5
5
  SHA512:
6
- metadata.gz: f7adc9b72cf7587f09d6ffc1f3eea789d3c2c7ccb747b364b254a89783449aac38185a0cfcde5c7c89088eee658629804d606a1111d213e97433bc4727b31f2b
7
- data.tar.gz: e88dc8694ac67b3eefba88218b111a5d51f8696e3dc9de9d3f46cf3bd7dbfadc53589ce2f94e43b78d1279758821edbe206c24a62df4088d126f76b90f200d94
6
+ metadata.gz: 0a1c5aef9d35b915e2a691ecaed88a1687bba7c7692e6c5066999e1348c5ba0bc9fe76e4afd3eea08da72b57f9a58379a7fc287385a316c6a12e795005e9b66e
7
+ data.tar.gz: 44a5eb8c106bd7b803428d4d8c7ad056b723dfc014baa4e94fd390c0924732a14b3d7ad9e262fa6e7de02a75bca073ee19b8476eda64c86b6fee1817380124c5
data/.rubocop.yml CHANGED
@@ -7,6 +7,16 @@ Metrics/LineLength:
7
7
  IneffectiveAccessModifier:
8
8
  Enabled: false
9
9
 
10
+ Style/HashTransformKeys:
11
+ Exclude:
12
+ - 'lib/uploadcare/entity/decorator/paginator.rb'
13
+ - 'lib/uploadcare/client/conversion/video_conversion_client.rb'
14
+ - 'lib/uploadcare/entity/file.rb'
15
+
16
+ Gemspec/RequiredRubyVersion:
17
+ Exclude:
18
+ - 'uploadcare-ruby.gemspec'
19
+
10
20
  Metrics/BlockLength:
11
21
  Exclude:
12
22
  - 'bin/'
@@ -18,3 +28,6 @@ Metrics/MethodLength:
18
28
 
19
29
  Style/Documentation:
20
30
  Enabled: false
31
+
32
+ Style/OptionalBooleanParameter:
33
+ AllowedMethods: ['create']
data/CHANGELOG.md CHANGED
@@ -1,31 +1,45 @@
1
1
  # Changelog
2
2
 
3
- ## [Unreleased](https://github.com/uploadcare/uploadcare-ruby/tree/develop)
3
+ ## 3.1.1 2021-10-13
4
4
 
5
- [Full Changelog](https://github.com/uploadcare/uploadcare-ruby/compare/0baded5593869f1d741f0fff22c58814970726b2...HEAD)
5
+ - Fix Uploadcare::File#store
6
+ - Fix Uploadcare::File#delete
6
7
 
7
- ## [3.0.4-dev] 2020-03-19
8
+ ## 3.1.0 2021-09-21
9
+
10
+ - Added documents and videos conversion
11
+ - Added new attributes to the Entity class (variations, video_info, source, rekognition_info)
12
+ - Added an opportunity to add custom logic to large files uploading process
13
+
14
+ ## 3.0.5 2021-04-15
15
+
16
+ - Replace Travis-CI with Github Actions
17
+ - Automate gem pushing
18
+
19
+ ## 3.0.4-dev 2020-03-19
8
20
 
9
21
  - Added better pagination methods for GroupList & FileList
10
22
  - Improved documentation and install instructions
11
23
  - Added CI
12
24
 
13
- ## [3.0.3-dev] 2020-03-13
25
+ ## 3.0.3-dev 2020-03-13
14
26
  - Added better pagination and iterators for GroupList & FileList
15
27
 
16
- ## [3.0.2-dev] 2020-03-11
28
+ ## 3.0.2-dev 2020-03-11
17
29
 
18
30
  - Expanded File and Group entities
19
31
  - Changed user agent syntax
20
32
 
21
- ## [3.0.1-dev] 2020-03-11
33
+ ## 3.0.1-dev 2020-03-11
22
34
 
23
35
  - Added Upload/group functionality
24
36
  - Added user API
25
37
  - Added user agent
26
38
  - Isolated clients, entities and concerns
27
39
  - Expanded documentation
28
- ## [3.0.0-dev] 2020-02-18
40
+
41
+ ## 3.0.0-dev 2020-02-18
42
+
29
43
  ### Changed
30
44
  - Rewrote gem from scratch
31
45
 
data/README.md CHANGED
@@ -12,12 +12,24 @@
12
12
  [stack-img]: https://img.shields.io/badge/tech-stack-0690fa.svg?style=flat
13
13
  [stack]: https://stackshare.io/uploadcare/stacks/
14
14
 
15
-
16
15
  Uploadcare Ruby integration handles uploads and further operations with files by
17
16
  wrapping Upload and REST APIs.
18
17
 
19
18
  * [Installation](#installation)
20
19
  * [Usage](#usage)
20
+ * [Uploading files](#uploading-files)
21
+ * [Uploading and storing a single file](#uploading-and-storing-a-single-file)
22
+ * [Multiple ways to upload files](#multiple-ways-to-upload-files)
23
+ * [Uploading options](#uploading-options)
24
+ * [File management](#file-management)
25
+ * [File](#file)
26
+ * [FileList](#filelist)
27
+ * [Pagination](#pagination)
28
+ * [Group](#group)
29
+ * [GroupList](#grouplist)
30
+ * [Webhook](#webhook)
31
+ * [Project](#project)
32
+ * [Conversion](#conversion)
21
33
  * [Useful links](#useful-links)
22
34
 
23
35
  ## Requirements
@@ -25,7 +37,7 @@ wrapping Upload and REST APIs.
25
37
 
26
38
  ## Compatibility
27
39
 
28
- Note that `uploadcare-ruby` **3.x** is not backward compativble with
40
+ Note that `uploadcare-ruby` **3.x** is not backward compatible with
29
41
  **[2.x](https://github.com/uploadcare/uploadcare-ruby/tree/v2.x)**.
30
42
 
31
43
  ## Installation
@@ -33,7 +45,7 @@ Note that `uploadcare-ruby` **3.x** is not backward compativble with
33
45
  Add this line to your application's Gemfile:
34
46
 
35
47
  ```ruby
36
- gem 'uploadcare-ruby'
48
+ gem "uploadcare-ruby"
37
49
  ```
38
50
 
39
51
  And then execute:
@@ -55,8 +67,8 @@ settings can be seen in [`lib/uploadcare.rb`](lib/uploadcare.rb)
55
67
 
56
68
  ```ruby
57
69
  # your_config_initializer_file.rb
58
- Uploadcare.config.public_key = 'demopublickey'
59
- Uploadcare.config.secret_key = 'demoprivatekey'
70
+ Uploadcare.config.public_key = "demopublickey"
71
+ Uploadcare.config.secret_key = "demoprivatekey"
60
72
  ```
61
73
 
62
74
  ## Usage
@@ -65,7 +77,8 @@ This section contains practical usage examples. Please note, everything that
65
77
  follows gets way more clear once you've looked through our
66
78
  [docs](https://uploadcare.com/docs/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby).
67
79
 
68
- ### Uploading and storing a single file
80
+ ### Uploading files
81
+ #### Uploading and storing a single file
69
82
 
70
83
  Using Uploadcare is simple, and here are the basics of handling files.
71
84
 
@@ -78,7 +91,7 @@ Using Uploadcare is simple, and here are the basics of handling files.
78
91
  # => "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"
79
92
 
80
93
  # URL for the file, can be used with your website or app right away
81
- @uc_file.cdn_url
94
+ @uc_file.url
82
95
  # => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/"
83
96
  ```
84
97
 
@@ -97,28 +110,71 @@ within a 24-hour period.
97
110
  # => #<Uploadcare::Api::File ...
98
111
  ```
99
112
 
100
- ### Uploads
113
+ #### Multiple ways to upload files
101
114
 
102
115
  Uploadcare supports multiple ways to upload files:
103
116
 
104
117
  ```ruby
105
118
  # Smart upload - detects type of passed object and picks appropriate upload method
106
- Uploadcare::Uploader.upload('https://placekitten.com/96/139')
119
+ Uploadcare::Uploader.upload("https://placekitten.com/96/139")
107
120
  ```
108
121
 
109
122
  There are explicit ways to select upload type:
110
123
 
111
124
  ```ruby
112
- files = [File.open('1.jpg'), File.open('1.jpg']
125
+ files = [File.open("1.jpg"), File.open("1.jpg"]
113
126
  Uploadcare::Uploader.upload_files(files)
114
127
 
115
- Uploadcare::Uploader.upload_from_url('https://placekitten.com/96/139')
128
+ Uploadcare::Uploader.upload_from_url("https://placekitten.com/96/139")
129
+ ```
130
+ It is possible to track progress of the upload-from-URL process. To do that, you should specify the `async` option and get a token:
131
+
132
+ ```ruby
133
+ Uploadcare::Uploader.upload_from_url("https://placekitten.com/96/139", async: true)
134
+ # => "c6e31082-6bdc-4cb3-bef5-14dd10574d72"
135
+ ```
136
+
137
+ After the request for uploading-from-URL is sent, you can check the progress of the upload by sending the `get_upload_from_url_status` request:
116
138
 
139
+ ```ruby
140
+ Uploadcare::Uploader.get_upload_from_url_status("1251ee66-3631-4416-a2fb-96ba59f5a515")
141
+ # => Success({:size=>28511, :total=>28511, :done=>28511, :uuid=>"b829753b-6b64-4989-a167-ef15e4f3d190", :file_id=>"b859753b-zb64-4989-a167-ef15e4f3a190", :original_filename=>"video.ogg", :is_image=>false, :is_stored=>false, :image_info=>nil, :video_info=>nil, :is_ready=>true, :filename=>"video.ogg", :mime_type=>"audio/ogg", :status=>"success"})
142
+ ```
143
+
144
+ In case of the `async` option is disabled, uploadcare-ruby tries to request the upload status several times (depending on the `max_request_tries` config param) and then returns uploaded file attributes.
145
+
146
+ ```ruby
117
147
  # multipart upload - can be useful for files bigger than 10 mb
118
- Uploadcare::Uploader.multipart_upload(File.open('big_file.bin'))
148
+ Uploadcare::Uploader.multipart_upload(File.open("big_file.bin"), store: true)
119
149
  ```
120
150
 
121
- ### Upload options
151
+ For the multipart upload you can pass a block to add some additional logic after each file chunk is uploaded.
152
+ For example to track file uploading progress you can do something like this:
153
+
154
+ ```ruby
155
+ file = File.open("big_file.bin")
156
+ progress = 0
157
+ Uploadcare::Uploader.multipart_upload(file, store: true) do |options|
158
+ progress += (100.0 / options[:links_count])
159
+ puts "PROGRESS = #{progress}"
160
+ end
161
+ ```
162
+ Output of the code above looks like:
163
+ ```console
164
+ PROGRESS = 4.545454545454546
165
+ PROGRESS = 9.090909090909092
166
+ PROGRESS = 13.636363636363637
167
+ ...
168
+ ```
169
+ Options available in a block:
170
+ - **:chunk_size** - size of each chunk in bytes;
171
+ - **:object** - file object which is going to be uploaded;
172
+ - **:offset** - offset from the beginning of a File object in bytes;
173
+ - **:link_id** - index of a link provided by Uploadcare API. Might be treated as index of a chunk;
174
+ - **:links** - array of links for uploading file's chunks;
175
+ - **:links_count** - count of the array of links.
176
+
177
+ #### Uploading options
122
178
 
123
179
  You can override global [`:autostore`](#initialization) option for each upload request:
124
180
 
@@ -127,14 +183,7 @@ You can override global [`:autostore`](#initialization) option for each upload r
127
183
  @api.upload_from_url(url, store: :auto)
128
184
  ```
129
185
 
130
- ### Api
131
- Most methods are also available through `Uploadcare::Api` object:
132
- ```ruby
133
- # Same as Uploadcare::Uploader.upload
134
- Uploadcare::Api.upload('https://placekitten.com/96/139')
135
- ```
136
-
137
- ### Entity object
186
+ ### File management
138
187
 
139
188
  Entities are representations of objects in Uploadcare cloud.
140
189
 
@@ -143,7 +192,7 @@ Entities are representations of objects in Uploadcare cloud.
143
192
  File entity contains its metadata.
144
193
 
145
194
  ```ruby
146
- @file = Uploadcare::File.file('FILE_ID_IN_YOUR_PROJECT')
195
+ @file = Uploadcare::File.file("FILE_ID_IN_YOUR_PROJECT")
147
196
  {"datetime_removed"=>nil,
148
197
  "datetime_stored"=>"2020-01-16T15:03:15.315064Z",
149
198
  "datetime_uploaded"=>"2020-01-16T15:03:14.676902Z",
@@ -168,22 +217,64 @@ File entity contains its metadata.
168
217
  "https://api.uploadcare.com/files/FILE_ID_IN_YOUR_PROJECT/",
169
218
  "uuid"=>"8f64f313-e6b1-4731-96c0-6751f1e7a50a"}
170
219
 
220
+ @file.copy # copies file, returns a new (copied) file metadata
221
+
171
222
  @file.store # stores file, returns updated metadata
172
223
 
173
224
  @file.delete #deletes file. Returns updated metadata
174
225
  ```
175
226
 
227
+ The File object is also can be converted if it is a document or a video file. Imagine, you have a document file:
228
+
229
+ ```ruby
230
+ @file = Uploadcare::File.file("FILE_ID_IN_YOUR_PROJECT")
231
+ ```
232
+
233
+ To convert it to an another file, just do:
234
+
235
+ ```ruby
236
+ @converted_file = @file.convert_document({ format: "png", page: "1" }, store: true)
237
+ # => {
238
+ # "uuid"=>"<NEW_FILE_UUID>"}
239
+ # ...other file info...
240
+ # }
241
+ # OR
242
+ # Failure({:"<FILE_UUID>/document/-/format/png/-/page/1/"=>"the target_format is not a supported 'to' format for this source file. <you_source_file_extension> -> png"})
243
+ ```
244
+
245
+ Same works for video files:
246
+
247
+ ```ruby
248
+ @converted_file = @file.convert_video(
249
+ {
250
+ format: "ogg",
251
+ quality: "best",
252
+ cut: { start_time: "0:0:0.1", length: "end" },
253
+ size: { resize_mode: "change_ratio", width: "600", height: "400" },
254
+ thumb: { N: 1, number: 2 }
255
+ },
256
+ store: true
257
+ )
258
+ # => {
259
+ # "uuid"=>"<NEW_FILE_UUID>"}
260
+ # ...other file info...
261
+ # }
262
+ # OR
263
+ # Failure({:"<FILE_UUID>/video/-/size/600x400/preserve_ratio/-/quality/best/-/format/ogg/-/cut/0:0:0.1/end/-/thumbs~1/2/"=>"CDN Path error: Failed to parse remainder \"/preserve_ratio\" of \"size/600x400/preserve_ratio\""})
264
+ ```
265
+
266
+ More about file conversion [here](#conversion).
176
267
  Metadata of deleted files is stored permanently.
177
268
 
178
269
  #### FileList
179
270
 
180
- `Uploadcare::Entity::FileList` represents the whole collection of files (or it's
271
+ `Uploadcare::FileList` represents the whole collection of files (or it's
181
272
  subset) and provides a way to iterate through it, making pagination transparent.
182
- FileList objects can be created using `Uploadcare::Entity.file_list` method.
273
+ FileList objects can be created using `Uploadcare::FileList.file_list` method.
183
274
 
184
275
  ```ruby
185
- @list = Uploadcare::Entity.file_list
186
- # Returns instance of Uploadcare::Api::FileList
276
+ @list = Uploadcare::FileList.file_list
277
+ # Returns instance of Uploadcare::Entity::FileList
187
278
  <Hashie::Mash
188
279
  next=nil
189
280
  per_page=100
@@ -198,7 +289,7 @@ FileList objects can be created using `Uploadcare::Entity.file_list` method.
198
289
  @all_files = @list.load
199
290
  ```
200
291
 
201
- This method accepts some options to controll which files should be fetched and
292
+ This method accepts some options to control which files should be fetched and
202
293
  how they should be fetched:
203
294
 
204
295
  - **:limit** — Controls page size. Accepts values from 1 to 1000, defaults to 100.
@@ -215,7 +306,7 @@ stored just for your convenience. That is why they are frozen.
215
306
  options = {
216
307
  limit: 10,
217
308
  stored: true,
218
- ordering: '-datetime_uploaded',
309
+ ordering: "-datetime_uploaded",
219
310
  from: "2017-01-01T00:00:00",
220
311
  }
221
312
  @list = @api.file_list(options)
@@ -226,15 +317,15 @@ To simply get all associated objects:
226
317
  @list.all # => returns Array of Files
227
318
  ```
228
319
 
229
- ##### Pagination
320
+ #### Pagination
230
321
 
231
322
  Initially, `FileList` is a paginated collection. It can be navigated using following methods:
232
323
  ```ruby
233
- @file_list = Uploadcare::Entity::FileList.file_list
324
+ @file_list = Uploadcare::FileList.file_list
234
325
  # Let's assume there are 250 files in cloud. By default, UC loads 100 files. To get next 100 files, do:
235
326
  @next_page = @file_list.next_page
236
327
  # To get previous page:
237
- @previous_page = @next_page.previous_page
328
+ @previous_page = @file_list.previous_page
238
329
  ```
239
330
 
240
331
  Alternatively, it's possible to iterate through full list of groups or files with `each`:
@@ -251,10 +342,14 @@ assigned UUID. Note, group UUIDs include a `~#{files_count}` part at the end.
251
342
  That's a requirement of our API.
252
343
 
253
344
  ```ruby
254
- # group can be created from an array of Uploadcare files
345
+ # group can be created from an array of Uploadcare files (UUIDs)
346
+ @file = "134dc30c-093e-4f48-a5b9-966fe9cb1d01"
347
+ @file2 = "134dc30c-093e-4f48-a5b9-966fe9cb1d02"
255
348
  @files_ary = [@file, @file2]
256
- @files = Uploadcare::Uploader.upload @files_ary
257
349
  @group = Uploadcare::Group.create @files
350
+
351
+ # group can be stored by group ID. It means that all files of a group will be stored on Uploadcare servers permanently
352
+ Uploadcare::Group.store(group.id)
258
353
  ```
259
354
 
260
355
  #### GroupList
@@ -275,7 +370,10 @@ You can use webhooks to provide notifications about your uploads to target urls.
275
370
  This gem lets you create and manage webhooks.
276
371
 
277
372
  ```ruby
278
- Uploadcare::Webhook.create('example.com/listen', event: 'file.uploaded')
373
+ Uploadcare::Webhook.create(target_url: "https://example.com/listen", event: "file.uploaded", is_active: true)
374
+ Uploadcare::Webhook.update(<webhook_id>, target_url: "https://newexample.com/listen/new", event: "file.uploaded", is_active: true)
375
+ Uploadcare::Webhook.delete("https://example.com/listen")
376
+ Uploadcare::Webhook.list
279
377
  ```
280
378
 
281
379
  #### Project
@@ -297,6 +395,222 @@ object is also an Hashie::Mash, so every methods out of
297
395
  # [{"email": collaborator@gmail.com, "name": "Collaborator"}, {"email": collaborator@gmail.com, "name": "Collaborator"}]
298
396
  ```
299
397
 
398
+ #### Conversion
399
+
400
+ ##### Video
401
+
402
+ Uploadcare can encode video files from all popular formats, adjust their quality, format and dimensions, cut out a video fragment, and generate thumbnails via [REST API](https://uploadcare.com/api-refs/rest-api/v0.6.0/).
403
+
404
+ After each video file upload you obtain a file identifier in UUID format.
405
+ Then you can use this file identifier to convert your video in multiple ways:
406
+
407
+ ```ruby
408
+ Uploadcare::VideoConverter.convert(
409
+ [
410
+ {
411
+ uuid: "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40",
412
+ size: { resize_mode: "change_ratio", width: "600", height: "400" },
413
+ quality: "best",
414
+ format: "ogg",
415
+ cut: { start_time: "0:0:0.0", length: "0:0:1.0" },
416
+ thumbs: { N: 2, number: 1 }
417
+ }
418
+ ],
419
+ store: false
420
+ )
421
+ ```
422
+ This method accepts options to set properties of an output file:
423
+
424
+ - **uuid** — the file UUID-identifier.
425
+ - **size**:
426
+ - **resize_mode** - size operation to apply to a video file. Can be `preserve_ratio (default)`, `change_ratio`, `scale_crop` or `add_padding`.
427
+ - **width** - width for a converted video.
428
+ - **height** - height for a converted video.
429
+
430
+ ```
431
+ NOTE: you can choose to provide a single dimension (width OR height).
432
+ The value you specify for any of the dimensions should be a non-zero integer divisible by 4
433
+ ```
434
+
435
+ - **quality** - sets the level of video quality that affects file sizes and hence loading times and volumes of generated traffic. Can be `normal (default)`, `better`, `best`, `lighter`, `lightest`.
436
+ - **format** - format for a converted video. Can be `mp4 (default)`, `webm`, `ogg`.
437
+ - **cut**:
438
+ - **start_time** - defines the starting point of a fragment to cut based on your input file timeline.
439
+ - **length** - defines the duration of that fragment.
440
+ - **thumbs**:
441
+ - **N** - quantity of thumbnails for your video - non-zero integer ranging from 1 to 50; defaults to 1.
442
+ - **number** - zero-based index of a particular thumbnail in a created set, ranging from 1 to (N - 1).
443
+ - **store** - a flag indicating if Uploadcare should store your transformed outputs.
444
+
445
+ ```ruby
446
+ # Response
447
+ {
448
+ :result => [
449
+ {
450
+ :original_source=>"dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/video/-/size/600x400/change_ratio/-/quality/best/-/format/ogg/-/cut/0:0:0.0/0:0:1.0/-/thumbs~2/1/",
451
+ :token=>911933811,
452
+ :uuid=>"6f9b88bd-625c-4d60-bfde-145fa3813d95",
453
+ :thumbnails_group_uuid=>"cf34c5a1-8fcc-4db2-9ec5-62c389e84468~2"
454
+ }
455
+ ],
456
+ :problems=>{}
457
+ }
458
+ ```
459
+ Params in the response:
460
+ - **result** - info related to your transformed output(-s):
461
+ - **original_source** - built path for a particular video with all the conversion operations and parameters.
462
+ - **token** - a processing job token that can be used to get a [job status](https://uploadcare.com/docs/transformations/video-encoding/#status) (see below).
463
+ - **uuid** - UUID of your processed video file.
464
+ - **thumbnails_group_uuid** - holds :uuid-thumb-group, a UUID of a [file group](https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/groupsList) with thumbnails for an output video, based on the thumbs [operation](https://uploadcare.com/docs/transformations/video-encoding/#operation-thumbs) parameters.
465
+ - **problems** - problems related to your processing job, if any.
466
+
467
+ To convert multiple videos just add params as a hash for each video to the first argument of the `Uploadcare::VideoConverter#convert` method:
468
+
469
+ ```ruby
470
+ Uploadcare::VideoConverter.convert(
471
+ [
472
+ { video_one_params }, { video_two_params }, ...
473
+ ],
474
+ store: false
475
+ )
476
+ ```
477
+
478
+
479
+ To check a status of a video processing job you can simply use appropriate method of `Uploadcare::VideoConverter`:
480
+
481
+ ```ruby
482
+ token = 911933811
483
+ Uploadcare::VideoConverter.status(token)
484
+ ```
485
+ `token` here is a processing job token, obtained in a response of a convert video request.
486
+
487
+ ```ruby
488
+ # Response
489
+ {
490
+ :status => "finished",
491
+ :error => nil,
492
+ :result => {
493
+ :uuid => "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40",
494
+ :thumbnails_group_uuid => "0f181f24-7551-42e5-bebc-14b15d9d3838~2"
495
+ }
496
+ }
497
+ ```
498
+
499
+ Params in the response:
500
+ - **status** - processing job status, can have one of the following values:
501
+ - *pending* — video file is being prepared for conversion.
502
+ - *processing* — video file processing is in progress.
503
+ - *finished* — the processing is finished.
504
+ - *failed* — we failed to process the video, see error for details.
505
+ - *canceled* — video processing was canceled.
506
+ - **error** - holds a processing error if we failed to handle your video.
507
+ - **result** - repeats the contents of your processing output.
508
+ - **thumbnails_group_uuid** - holds :uuid-thumb-group, a UUID of a file group with thumbnails for an output video, based on the thumbs operation parameters.
509
+ - **uuid** - a UUID of your processed video file.
510
+
511
+ More examples and options can be found [here](https://uploadcare.com/docs/transformations/video-encoding/#video-encoding)
512
+
513
+ ##### Document
514
+
515
+ Uploadcare allows converting documents to the following target formats: doc, docx, xls, xlsx, odt, ods, rtf, txt, pdf, jpg, png. Document Conversion works via our [REST API](https://uploadcare.com/api-refs/rest-api/v0.6.0/).
516
+
517
+ After each document file upload you obtain a file identifier in UUID format.
518
+ Then you can use this file identifier to convert your document to a new format:
519
+
520
+ ```ruby
521
+ Uploadcare::DocumentConverter.convert(
522
+ [
523
+ {
524
+ uuid: "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40",
525
+ format: "pdf"
526
+ }
527
+ ],
528
+ store: false
529
+ )
530
+ ```
531
+ or create an image of a particular page (if using image format):
532
+ ```ruby
533
+ Uploadcare::DocumentConverter.convert(
534
+ [
535
+ {
536
+ uuid: "a4b9db2f-1591-4f4c-8f68-94018924525d",
537
+ format: "png",
538
+ page: 1
539
+ }
540
+ ],
541
+ store: false
542
+ )
543
+ ```
544
+
545
+ This method accepts options to set properties of an output file:
546
+
547
+ - **uuid** — the file UUID-identifier.
548
+ - **format** - defines the target format you want a source file converted to. The supported values are: `pdf` (default), `doc`, `docx`, `xls`, `xlsx`, `odt`, `ods`, `rtf`, `txt`, `jpg`, `png`. In case the format operation was not found, your input document will be converted to `pdf`.
549
+ - **page** - a page number of a multi-paged document to either `jpg` or `png`. The method will not work for any other target formats.
550
+
551
+ ```ruby
552
+ # Response
553
+ {
554
+ :result => [
555
+ {
556
+ :original_source=>"a4b9db2f-1591-4f4c-8f68-94018924525d/document/-/format/png/-/page/1/",
557
+ :token=>21120220
558
+ :uuid=>"88fe5ada-90f1-422a-a233-3a0f3a7cf23c"
559
+ }
560
+ ],
561
+ :problems=>{}
562
+ }
563
+ ```
564
+ Params in the response:
565
+ - **result** - info related to your transformed output(-s):
566
+ - **original_source** - source file identifier including a target format, if present.
567
+ - **token** - a processing job token that can be used to get a [job status](https://uploadcare.com/docs/transformations/document-conversion/#status) (see below).
568
+ - **uuid** - UUID of your processed document file.
569
+ - **problems** - problems related to your processing job, if any.
570
+
571
+ To convert multiple documents just add params as a hash for each document to the first argument of the `Uploadcare::DocumentConverter#convert` method:
572
+
573
+ ```ruby
574
+ Uploadcare::DocumentConverter.convert(
575
+ [
576
+ { doc_one_params }, { doc_two_params }, ...
577
+ ],
578
+ store: false
579
+ )
580
+ ```
581
+
582
+ To check a status of a document processing job you can simply use appropriate method of `Uploadcare::DocumentConverter`:
583
+
584
+ ```ruby
585
+ token = 21120220
586
+ Uploadcare::DocumentConverter.status(token)
587
+ ```
588
+ `token` here is a processing job token, obtained in a response of a convert document request.
589
+
590
+ ```ruby
591
+ # Response
592
+ {
593
+ :status => "finished",
594
+ :error => nil,
595
+ :result => {
596
+ :uuid => "a4b9db2f-1591-4f4c-8f68-94018924525d"
597
+ }
598
+ }
599
+ ```
600
+
601
+ Params in the response:
602
+ - **status** - processing job status, can have one of the following values:
603
+ - *pending* — document file is being prepared for conversion.
604
+ - *processing* — document file processing is in progress.
605
+ - *finished* — the processing is finished.
606
+ - *failed* — we failed to process the document, see error for details.
607
+ - *canceled* — document processing was canceled.
608
+ - **error** - holds a processing error if we failed to handle your document.
609
+ - **result** - repeats the contents of your processing output.
610
+ - **uuid** - a UUID of your processed document file.
611
+
612
+ More examples and options can be found [here](https://uploadcare.com/docs/transformations/document-conversion/#document-conversion)
613
+
300
614
  ## Useful links
301
615
 
302
616
  * [Development](https://github.com/uploadcare/uploadcare-ruby/blob/main/DEVELOPMENT.md)
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../rest_client'
4
+ require 'exception/conversion_error'
5
+
6
+ module Uploadcare
7
+ module Client
8
+ module Conversion
9
+ # This is a base client for conversion operations
10
+ #
11
+ # @see https://uploadcare.com/api-refs/rest-api/v0.6.0/#tag/Conversion
12
+ class BaseConversionClient < RestClient
13
+ API_VERSION_HEADER_VALUE = 'application/vnd.uploadcare-v0.5+json'
14
+
15
+ def headers
16
+ {
17
+ 'Content-Type': 'application/json',
18
+ 'Accept': API_VERSION_HEADER_VALUE,
19
+ 'User-Agent': Uploadcare::Param::UserAgent.call
20
+ }
21
+ end
22
+
23
+ private
24
+
25
+ def send_convert_request(arr, options, url_builder_class)
26
+ body = build_body_for_many(arr, options, url_builder_class)
27
+ post(uri: convert_uri, content: body)
28
+ end
29
+
30
+ def success(response)
31
+ body = response.body.to_s
32
+ extract_result(body)
33
+ end
34
+
35
+ def extract_result(response_body)
36
+ return if response_body.empty?
37
+
38
+ parsed_body = JSON.parse(response_body, symbolize_names: true)
39
+ errors = parsed_body[:error] || parsed_body[:problems]
40
+ return Dry::Monads::Failure(errors) unless errors.nil? || errors.empty?
41
+
42
+ Dry::Monads::Success(parsed_body)
43
+ end
44
+
45
+ # Prepares body for convert_many method
46
+ def build_body_for_many(arr, options, url_builder_class)
47
+ {
48
+ paths: arr.map do |params|
49
+ url_builder_class.call(
50
+ **build_paths_body(params)
51
+ )
52
+ end,
53
+ store: options[:store]
54
+ }.compact.to_json
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end