uploadcare-ruby 4.4.3 → 5.0.0.rc1

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 (178) hide show
  1. checksums.yaml +4 -4
  2. data/.env.example +7 -0
  3. data/.github/workflows/gem-push.yml +1 -1
  4. data/.github/workflows/ruby.yml +10 -13
  5. data/.gitignore +9 -0
  6. data/.rubocop.yml +95 -8
  7. data/CHANGELOG.md +71 -1
  8. data/Gemfile +23 -6
  9. data/MIGRATING_V5.md +290 -0
  10. data/README.md +422 -671
  11. data/Rakefile +5 -1
  12. data/api_examples/README.md +77 -0
  13. data/api_examples/rest_api/delete_files_storage.rb +3 -5
  14. data/api_examples/rest_api/delete_files_uuid_metadata_key.rb +3 -4
  15. data/api_examples/rest_api/delete_files_uuid_storage.rb +3 -4
  16. data/api_examples/rest_api/delete_groups_uuid.rb +3 -4
  17. data/api_examples/rest_api/delete_webhooks_unsubscribe.rb +3 -4
  18. data/api_examples/rest_api/get_addons_aws_rekognition_detect_labels_execute_status.rb +3 -6
  19. data/api_examples/rest_api/get_addons_aws_rekognition_detect_moderation_labels_execute_status.rb +3 -6
  20. data/api_examples/rest_api/get_addons_remove_bg_execute_status.rb +3 -6
  21. data/api_examples/rest_api/get_addons_uc_clamav_virus_scan_execute_status.rb +3 -6
  22. data/api_examples/rest_api/get_convert_document_status_token.rb +3 -5
  23. data/api_examples/rest_api/get_convert_document_uuid.rb +3 -5
  24. data/api_examples/rest_api/get_convert_video_status_token.rb +3 -5
  25. data/api_examples/rest_api/get_files.rb +3 -5
  26. data/api_examples/rest_api/get_files_uuid.rb +3 -5
  27. data/api_examples/rest_api/get_files_uuid_metadata.rb +3 -5
  28. data/api_examples/rest_api/get_files_uuid_metadata_key.rb +3 -5
  29. data/api_examples/rest_api/get_groups.rb +3 -5
  30. data/api_examples/rest_api/get_groups_uuid.rb +3 -5
  31. data/api_examples/rest_api/get_project.rb +3 -5
  32. data/api_examples/rest_api/get_webhooks.rb +3 -5
  33. data/api_examples/rest_api/post_addons_aws_rekognition_detect_labels_execute.rb +3 -5
  34. data/api_examples/rest_api/post_addons_aws_rekognition_detect_moderation_labels_execute.rb +3 -5
  35. data/api_examples/rest_api/post_addons_remove_bg_execute.rb +3 -5
  36. data/api_examples/rest_api/post_addons_uc_clamav_virus_scan_execute.rb +3 -5
  37. data/api_examples/rest_api/post_convert_document.rb +3 -8
  38. data/api_examples/rest_api/post_convert_video.rb +3 -10
  39. data/api_examples/rest_api/post_files_local_copy.rb +3 -6
  40. data/api_examples/rest_api/post_files_remote_copy.rb +3 -7
  41. data/api_examples/rest_api/post_webhooks.rb +3 -9
  42. data/api_examples/rest_api/put_files_storage.rb +3 -8
  43. data/api_examples/rest_api/put_files_uuid_metadata_key.rb +3 -7
  44. data/api_examples/rest_api/put_files_uuid_storage.rb +3 -5
  45. data/api_examples/rest_api/put_webhooks_id.rb +3 -11
  46. data/api_examples/support/example_helper.rb +250 -0
  47. data/api_examples/support/run_rest_example.rb +161 -0
  48. data/api_examples/support/run_upload_example.rb +88 -0
  49. data/api_examples/upload_api/get_from_url_status.rb +3 -5
  50. data/api_examples/upload_api/get_group_info.rb +3 -6
  51. data/api_examples/upload_api/get_info.rb +3 -6
  52. data/api_examples/upload_api/post_base.rb +3 -5
  53. data/api_examples/upload_api/post_from_url.rb +3 -5
  54. data/api_examples/upload_api/post_group.rb +3 -8
  55. data/api_examples/upload_api/post_multipart_complete.rb +3 -7
  56. data/api_examples/upload_api/post_multipart_start.rb +3 -7
  57. data/api_examples/upload_api/put_multipart_part.rb +4 -0
  58. data/bin/console +1 -1
  59. data/docs/release-notes-5.0.0.rc1.md +34 -0
  60. data/examples/README.md +39 -0
  61. data/examples/batch_upload.rb +54 -0
  62. data/examples/group_creation.rb +88 -0
  63. data/examples/large_file_upload.rb +88 -0
  64. data/examples/simple_upload.rb +39 -0
  65. data/examples/upload_with_progress.rb +84 -0
  66. data/examples/url_upload.rb +56 -0
  67. data/lib/uploadcare/api/rest/addons.rb +107 -0
  68. data/lib/uploadcare/api/rest/document_conversions.rb +65 -0
  69. data/lib/uploadcare/api/rest/file_metadata.rb +71 -0
  70. data/lib/uploadcare/api/rest/files.rb +112 -0
  71. data/lib/uploadcare/api/rest/groups.rb +49 -0
  72. data/lib/uploadcare/api/rest/project.rb +23 -0
  73. data/lib/uploadcare/api/rest/video_conversions.rb +52 -0
  74. data/lib/uploadcare/api/rest/webhooks.rb +74 -0
  75. data/lib/uploadcare/api/rest.rb +254 -0
  76. data/lib/uploadcare/api/upload/files.rb +313 -0
  77. data/lib/uploadcare/api/upload/groups.rb +72 -0
  78. data/lib/uploadcare/api/upload.rb +272 -0
  79. data/lib/uploadcare/client/addons_accessor.rb +85 -0
  80. data/lib/uploadcare/client/api.rb +33 -0
  81. data/lib/uploadcare/client/conversions_accessor.rb +33 -0
  82. data/lib/uploadcare/client/document_conversions_accessor.rb +41 -0
  83. data/lib/uploadcare/client/file_metadata_accessor.rb +46 -0
  84. data/lib/uploadcare/client/files_accessor.rb +82 -0
  85. data/lib/uploadcare/client/groups_accessor.rb +35 -0
  86. data/lib/uploadcare/client/project_accessor.rb +17 -0
  87. data/lib/uploadcare/client/video_conversions_accessor.rb +33 -0
  88. data/lib/uploadcare/client/webhooks_accessor.rb +42 -0
  89. data/lib/uploadcare/client.rb +127 -0
  90. data/lib/uploadcare/cname_generator.rb +68 -0
  91. data/lib/uploadcare/collections/batch_result.rb +35 -0
  92. data/lib/uploadcare/collections/paginated.rb +165 -0
  93. data/lib/uploadcare/configuration.rb +81 -0
  94. data/lib/uploadcare/exception/auth_error.rb +2 -6
  95. data/lib/uploadcare/exception/configuration_error.rb +4 -0
  96. data/lib/uploadcare/exception/conversion_error.rb +2 -6
  97. data/lib/uploadcare/exception/invalid_request_error.rb +4 -0
  98. data/lib/uploadcare/exception/multipart_upload_error.rb +4 -0
  99. data/lib/uploadcare/exception/not_found_error.rb +4 -0
  100. data/lib/uploadcare/exception/request_error.rb +2 -6
  101. data/lib/uploadcare/exception/retry_error.rb +2 -6
  102. data/lib/uploadcare/exception/throttle_error.rb +7 -11
  103. data/lib/uploadcare/exception/unknown_status_error.rb +4 -0
  104. data/lib/uploadcare/exception/upload_error.rb +4 -0
  105. data/lib/uploadcare/exception/upload_timeout_error.rb +4 -0
  106. data/lib/uploadcare/internal/authenticator.rb +101 -0
  107. data/lib/uploadcare/internal/error_handler.rb +102 -0
  108. data/lib/uploadcare/internal/signature_generator.rb +31 -0
  109. data/lib/uploadcare/internal/throttle_handler.rb +36 -0
  110. data/lib/uploadcare/internal/upload_io.rb +110 -0
  111. data/lib/uploadcare/internal/upload_params_generator.rb +86 -0
  112. data/lib/uploadcare/internal/user_agent.rb +22 -0
  113. data/lib/uploadcare/operations/multipart_upload.rb +213 -0
  114. data/lib/uploadcare/operations/upload_router.rb +162 -0
  115. data/lib/uploadcare/resources/addon_execution.rb +97 -0
  116. data/lib/uploadcare/resources/base_resource.rb +61 -0
  117. data/lib/uploadcare/resources/document_conversion.rb +81 -0
  118. data/lib/uploadcare/resources/file.rb +366 -0
  119. data/lib/uploadcare/resources/file_metadata.rb +135 -0
  120. data/lib/uploadcare/resources/group.rb +142 -0
  121. data/lib/uploadcare/resources/project.rb +26 -0
  122. data/lib/uploadcare/resources/video_conversion.rb +59 -0
  123. data/lib/uploadcare/resources/webhook.rb +85 -0
  124. data/lib/uploadcare/result.rb +85 -0
  125. data/lib/uploadcare/signed_url_generators/akamai_generator.rb +50 -51
  126. data/lib/uploadcare/signed_url_generators/base_generator.rb +15 -15
  127. data/lib/uploadcare/version.rb +7 -0
  128. data/lib/uploadcare/webhook_signature_verifier.rb +60 -0
  129. data/lib/uploadcare.rb +84 -50
  130. data/mise.toml +2 -0
  131. data/uploadcare-ruby.gemspec +8 -7
  132. metadata +102 -74
  133. data/api_examples/upload_api/put_presigned_url_x.rb +0 -8
  134. data/lib/uploadcare/api/api.rb +0 -25
  135. data/lib/uploadcare/client/addons_client.rb +0 -69
  136. data/lib/uploadcare/client/conversion/base_conversion_client.rb +0 -60
  137. data/lib/uploadcare/client/conversion/document_conversion_client.rb +0 -45
  138. data/lib/uploadcare/client/conversion/video_conversion_client.rb +0 -46
  139. data/lib/uploadcare/client/file_client.rb +0 -48
  140. data/lib/uploadcare/client/file_list_client.rb +0 -46
  141. data/lib/uploadcare/client/file_metadata_client.rb +0 -36
  142. data/lib/uploadcare/client/group_client.rb +0 -45
  143. data/lib/uploadcare/client/multipart_upload/chunks_client.rb +0 -58
  144. data/lib/uploadcare/client/multipart_upload_client.rb +0 -64
  145. data/lib/uploadcare/client/project_client.rb +0 -20
  146. data/lib/uploadcare/client/rest_client.rb +0 -77
  147. data/lib/uploadcare/client/rest_group_client.rb +0 -43
  148. data/lib/uploadcare/client/upload_client.rb +0 -46
  149. data/lib/uploadcare/client/uploader_client.rb +0 -128
  150. data/lib/uploadcare/client/webhook_client.rb +0 -49
  151. data/lib/uploadcare/concern/error_handler.rb +0 -54
  152. data/lib/uploadcare/concern/throttle_handler.rb +0 -25
  153. data/lib/uploadcare/concern/upload_error_handler.rb +0 -32
  154. data/lib/uploadcare/entity/addons.rb +0 -14
  155. data/lib/uploadcare/entity/conversion/base_converter.rb +0 -43
  156. data/lib/uploadcare/entity/conversion/document_converter.rb +0 -15
  157. data/lib/uploadcare/entity/conversion/video_converter.rb +0 -15
  158. data/lib/uploadcare/entity/decorator/paginator.rb +0 -79
  159. data/lib/uploadcare/entity/entity.rb +0 -18
  160. data/lib/uploadcare/entity/file.rb +0 -103
  161. data/lib/uploadcare/entity/file_list.rb +0 -32
  162. data/lib/uploadcare/entity/file_metadata.rb +0 -30
  163. data/lib/uploadcare/entity/group.rb +0 -49
  164. data/lib/uploadcare/entity/group_list.rb +0 -24
  165. data/lib/uploadcare/entity/project.rb +0 -13
  166. data/lib/uploadcare/entity/uploader.rb +0 -93
  167. data/lib/uploadcare/entity/webhook.rb +0 -14
  168. data/lib/uploadcare/param/authentication_header.rb +0 -37
  169. data/lib/uploadcare/param/conversion/document/processing_job_url_builder.rb +0 -39
  170. data/lib/uploadcare/param/conversion/video/processing_job_url_builder.rb +0 -64
  171. data/lib/uploadcare/param/param.rb +0 -10
  172. data/lib/uploadcare/param/secure_auth_header.rb +0 -51
  173. data/lib/uploadcare/param/simple_auth_header.rb +0 -14
  174. data/lib/uploadcare/param/upload/signature_generator.rb +0 -24
  175. data/lib/uploadcare/param/upload/upload_params_generator.rb +0 -41
  176. data/lib/uploadcare/param/user_agent.rb +0 -21
  177. data/lib/uploadcare/param/webhook_signature_verifier.rb +0 -23
  178. data/lib/uploadcare/ruby/version.rb +0 -5
@@ -1,128 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'upload_client'
4
- require 'retries'
5
- require 'param/upload/upload_params_generator'
6
- require 'param/upload/signature_generator'
7
-
8
- module Uploadcare
9
- module Client
10
- # This is client for general uploads
11
- #
12
- # @see https://uploadcare.com/api-refs/upload-api/#tag/Upload
13
- class UploaderClient < UploadClient
14
- # @see https://uploadcare.com/api-refs/upload-api/#operation/baseUpload
15
-
16
- def upload_many(arr, options = {})
17
- body = upload_many_body(arr, options)
18
- post(path: 'base/',
19
- headers: { 'Content-Type': body.content_type },
20
- body: body)
21
- end
22
-
23
- # syntactic sugar for upload_many
24
- # There is actual upload method for one file, but it is redundant
25
-
26
- def upload(file, options = {})
27
- upload_many([file], options)
28
- end
29
-
30
- # Upload files from url
31
- # @see https://uploadcare.com/api-refs/upload-api/#operation/fromURLUpload
32
- # options:
33
- # - check_URL_duplicates
34
- # - filename
35
- # - save_URL_duplicates
36
- # - async - returns upload token instead of upload data
37
- # - metadata - file metadata, hash
38
- def upload_from_url(url, options = {})
39
- body = upload_from_url_body(url, options)
40
- token_response = post(path: 'from_url/', headers: { 'Content-Type': body.content_type }, body: body)
41
- return token_response if options[:async]
42
-
43
- uploaded_response = poll_upload_response(token_response.success[:token])
44
- return uploaded_response if uploaded_response.success[:status] == 'error'
45
-
46
- Dry::Monads::Result::Success.call(files: [uploaded_response.success])
47
- end
48
-
49
- # Check upload status
50
- #
51
- # @see https://uploadcare.com/api-refs/upload-api/#operation/fromURLUploadStatus
52
- def get_upload_from_url_status(token)
53
- query_params = { token: token }
54
- get(path: 'from_url/status/', params: query_params)
55
- end
56
-
57
- # Get information about an uploaded file
58
- # Secret key not needed
59
- #
60
- # https://uploadcare.com/api-refs/upload-api/#tag/Upload/operation/fileUploadInfo
61
- def file_info(uuid)
62
- query_params = {
63
- file_id: uuid,
64
- pub_key: Uploadcare.config.public_key
65
- }
66
- get(path: 'info/', params: query_params)
67
- end
68
-
69
- private
70
-
71
- alias api_struct_post post
72
- def post(args = {})
73
- handle_throttling { api_struct_post(**args) }
74
- end
75
-
76
- def poll_upload_response(token)
77
- with_retries(max_tries: Uploadcare.config.max_request_tries,
78
- base_sleep_seconds: Uploadcare.config.base_request_sleep,
79
- max_sleep_seconds: Uploadcare.config.max_request_sleep,
80
- rescue: RetryError) do
81
- response = get_upload_from_url_status(token)
82
- handle_polling_response(response)
83
- end
84
- end
85
-
86
- def handle_polling_response(response)
87
- case response.success[:status]
88
- when 'error'
89
- raise RequestError, response.success[:error]
90
- when 'progress', 'waiting', 'unknown'
91
- raise RetryError, response.success[:error] || 'Upload is taking longer than expected. Try increasing the max_request_tries config if you know your file uploads will take more time.' # rubocop:disable Layout/LineLength
92
- end
93
-
94
- response
95
- end
96
-
97
- # Prepares body for upload_many method
98
- def upload_many_body(arr, options = {})
99
- files_formdata = arr.to_h do |file|
100
- [HTTP::FormData::File.new(file).filename,
101
- form_data_for(file)]
102
- end
103
- HTTP::FormData::Multipart.new(
104
- Param::Upload::UploadParamsGenerator.call(options).merge(files_formdata)
105
- )
106
- end
107
-
108
- # Prepare upload_from_url initial request body
109
- def upload_from_url_body(url, options = {})
110
- opts = {
111
- 'pub_key' => Uploadcare.config.public_key,
112
- 'source_url' => url,
113
- 'store' => store_value(options[:store])
114
- }
115
- opts.merge!(Param::Upload::SignatureGenerator.call) if Uploadcare.config.sign_uploads
116
- HTTP::FormData::Multipart.new(options.merge(opts))
117
- end
118
-
119
- def store_value(store)
120
- case store
121
- when true, '1', 1 then '1'
122
- when false, '0', 0 then '0'
123
- else 'auto'
124
- end
125
- end
126
- end
127
- end
128
- end
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'rest_client'
4
-
5
- module Uploadcare
6
- module Client
7
- # client for webhook management
8
- # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/Webhook
9
- class WebhookClient < RestClient
10
- # Create webhook
11
- # @see https://uploadcare.com/docs/api_reference/rest/webhooks/#subscribe
12
- def create(options = {})
13
- body = {
14
- target_url: options[:target_url],
15
- event: options[:event] || 'file.uploaded',
16
- is_active: options[:is_active].nil? ? true : options[:is_active]
17
- }.merge(
18
- { signing_secret: options[:signing_secret] }.compact
19
- ).to_json
20
- post(uri: '/webhooks/', content: body)
21
- end
22
-
23
- # Returns array (not paginated list) of webhooks
24
- # @see https://uploadcare.com/docs/api_reference/rest/webhooks/#get-list
25
- def list
26
- get(uri: '/webhooks/')
27
- end
28
-
29
- # Permanently deletes subscription
30
- # @see https://uploadcare.com/docs/api_reference/rest/webhooks/#unsubscribe
31
- def delete(target_url)
32
- body = { target_url: target_url }.to_json
33
- request(method: 'DELETE', uri: '/webhooks/unsubscribe/', content: body)
34
- end
35
-
36
- # Updates webhook
37
- # @see https://uploadcare.com/docs/api_reference/rest/webhooks/#subscribe-update
38
- def update(id, options = {})
39
- body = options.to_json
40
- put(uri: "/webhooks/#{id}/", content: body)
41
- end
42
-
43
- alias create_webhook create
44
- alias list_webhooks list
45
- alias delete_webhook delete
46
- alias update_webhook update
47
- end
48
- end
49
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Uploadcare
4
- module Concerns
5
- # Wrapper for responses
6
- # raises errors instead of returning monads
7
- module ErrorHandler
8
- include Exception
9
-
10
- # Extension of ApiStruct's failure method
11
- #
12
- # Raises errors instead of returning falsey objects
13
- # @see https://github.com/rubygarage/api_struct/blob/master/lib/api_struct/client.rb#L55
14
- def failure(response)
15
- catch_upload_errors(response)
16
- parsed_response = JSON.parse(response.body.to_s)
17
- raise RequestError, parsed_response['detail'] || parsed_response.map { |k, v| "#{k}: #{v}" }.join('; ')
18
- rescue JSON::ParserError
19
- raise RequestError, response.body.to_s
20
- end
21
-
22
- # Extension of ApiStruct's wrap method
23
- #
24
- # Catches throttling errors and Upload API errors
25
- #
26
- # @see https://github.com/rubygarage/api_struct/blob/master/lib/api_struct/client.rb#L45
27
- def wrap(response)
28
- raise_throttling_error(response) if response.status == 429
29
- return failure(response) if response.status >= 300
30
-
31
- catch_upload_errors(response)
32
- success(response)
33
- end
34
-
35
- private
36
-
37
- # Raise ThrottleError. Also, tells in error when server will be ready for next request
38
- def raise_throttling_error(response)
39
- retry_after = (response.headers['Retry-After'].to_i + 1) || 11
40
- raise ThrottleError.new(retry_after), "Response throttled, retry #{retry_after} seconds later"
41
- end
42
-
43
- # Upload API returns its errors with code 200, and stores its actual code and details within response message
44
- # This methods detects that and raises apropriate error
45
- def catch_upload_errors(response)
46
- return unless response.code == 200
47
-
48
- parsed_response = JSON.parse(response.body.to_s)
49
- error = parsed_response['error'] if parsed_response.is_a?(Hash)
50
- raise RequestError, error if error
51
- end
52
- end
53
- end
54
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Uploadcare
4
- module Concerns
5
- # This module lets clients send request multiple times if request is throttled
6
- module ThrottleHandler
7
- # call given block. If ThrottleError is returned, it will wait and attempt again 4 more times
8
- # @yield executable block (HTTP request that may be throttled)
9
- def handle_throttling
10
- (Uploadcare.config.max_throttle_attempts - 1).times do
11
- # rubocop:disable Style/RedundantBegin
12
- begin
13
- return yield
14
- rescue(Exception::ThrottleError) => e
15
- wait_time = e.timeout
16
- sleep(wait_time)
17
- next
18
- end
19
- # rubocop:enable Style/RedundantBegin
20
- end
21
- yield
22
- end
23
- end
24
- end
25
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Uploadcare
4
- module Concerns
5
- # Wrapper for responses
6
- # raises errors instead of returning monads
7
- module UploadErrorHandler
8
- include Exception
9
-
10
- # Extension of ApiStruct's failure method
11
- #
12
- # Raises errors instead of returning falsey objects
13
- # @see https://github.com/rubygarage/api_struct/blob/master/lib/api_struct/client.rb#L55
14
- def failure(response)
15
- catch_throttling_error(response)
16
- parsed_response = JSON.parse(response.body.to_s)
17
- raise RequestError, parsed_response['detail']
18
- rescue JSON::ParserError
19
- raise RequestError, response.status
20
- end
21
-
22
- private
23
-
24
- def catch_throttling_error(response)
25
- return unless response.code == 429
26
-
27
- retry_after = (response.headers['Retry-After'].to_i + 1) || 11
28
- raise ThrottleError.new(retry_after), "Response throttled, retry #{retry_after} seconds later"
29
- end
30
- end
31
- end
32
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Uploadcare
4
- module Entity
5
- # This serializer is responsible for addons handling
6
- #
7
- # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/Add-Ons
8
- class Addons < Entity
9
- client_service AddonsClient
10
-
11
- attr_entity :request_id, :status, :result
12
- end
13
- end
14
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Uploadcare
4
- module Entity
5
- module Conversion
6
- # This serializer lets a user convert uploaded documents
7
- # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/documentConvert
8
- class BaseConverter < Entity
9
- class << self
10
- # Converts files
11
- #
12
- # @param params [Array] of hashes with params or [Hash]
13
- # @option options [Boolean] :store whether to store file on servers.
14
- def convert(params, options = {})
15
- files_params = params.is_a?(Hash) ? [params] : params
16
- conversion_client.new.convert_many(files_params, options)
17
- end
18
-
19
- # Returns a status of a conversion job
20
- #
21
- # @param token [Integer, String] token obtained from a server in convert method
22
- def status(token)
23
- conversion_client.new.get_conversion_status(token)
24
- end
25
-
26
- # Returns the document format and possible conversion formats.
27
- #
28
- # @param uuid [String] UUID of the document
29
- def info(uuid)
30
- conversion_client.new.document_info(uuid)
31
- end
32
-
33
- private
34
-
35
- def conversion_client
36
- clients[:base]
37
- end
38
- end
39
- end
40
- end
41
- end
42
- include Conversion
43
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base_converter'
4
-
5
- module Uploadcare
6
- module Entity
7
- module Conversion
8
- # This serializer lets a user convert uploaded documents
9
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/documentConvert
10
- class DocumentConverter < BaseConverter
11
- client_service Client::Conversion::DocumentConversionClient
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base_converter'
4
-
5
- module Uploadcare
6
- module Entity
7
- module Conversion
8
- # This serializer lets a user convert uploaded videos, and usually returns an array of results
9
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/videoConvert
10
- class VideoConverter < BaseConverter
11
- client_service Client::Conversion::VideoConversionClient
12
- end
13
- end
14
- end
15
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Uploadcare
4
- module Entity
5
- # @abstract
6
- module Decorator
7
- # provides pagination methods for things in Uploadcare that paginate,
8
- # namely [FileList] and [Group]
9
- #
10
- # Requirements:
11
- # - Should be Entity with Client
12
- # - Associated Client should have `list` method that returns objects with pagination
13
- # - Response should have :next, :previous, :total, :per_page params and :results fields
14
- module Paginator
15
- @entity ||= Hashie::Mash.new
16
-
17
- # meta data of a pagination object
18
- def meta
19
- Hashie::Mash.new(next: @entity[:next], previous: @entity[:previous],
20
- total: @entity[:total], per_page: @entity[:per_page])
21
- end
22
-
23
- # Returns new instance of current object on next page
24
- def next_page
25
- url = @entity[:next]
26
- return unless url
27
-
28
- query = URI.decode_www_form(URI(url).query).to_h
29
- query = query.to_h { |k, v| [k.to_sym, v] }
30
- self.class.list(**query)
31
- end
32
-
33
- # Returns new instance of current object on previous page
34
- def previous_page
35
- url = @entity[:previous]
36
- return unless url
37
-
38
- query = URI.decode_www_form(URI(url).query).to_h
39
- query = query.to_h { |k, v| [k.to_sym, v] }
40
- self.class.list(**query)
41
- end
42
-
43
- # Attempts to load the entire list after offset into results of current object
44
- #
45
- # It's possible to avoid loading objects on previous pages by offsetting them first
46
- def load
47
- return self if @entity[:next].nil? || @entity[:results].length == @entity[:total]
48
-
49
- np = self
50
- until np.next.nil?
51
- np = np.next_page
52
- @entity[:results].concat(np.results.map(&:to_h))
53
- end
54
- @entity[:next] = nil
55
- @entity[:per_page] = @entity[:total]
56
- self
57
- end
58
-
59
- # iterate through pages, starting with current one
60
- #
61
- # @yield [Block]
62
- def each(&block)
63
- current_page = self
64
- while current_page
65
- current_page.results.each(&block)
66
- current_page = current_page.next_page
67
- end
68
- end
69
-
70
- # Load and return all objects in list
71
- #
72
- # @return [Array]
73
- def all
74
- load[:results]
75
- end
76
- end
77
- end
78
- end
79
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- Gem.find_files('client/**/*.rb').each { |path| require path }
4
-
5
- module Uploadcare
6
- # Entities represent objects existing in Uploadcare cloud
7
- #
8
- # Typically, Entities inherit class methods from {Client} instance methods
9
- # @see Client
10
- module Entity
11
- # @abstract
12
- class Entity < ApiStruct::Entity
13
- include Client
14
- end
15
- end
16
-
17
- include Entity
18
- end
@@ -1,103 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Uploadcare
4
- module Entity
5
- # This serializer returns a single file
6
- #
7
- # @see https://uploadcare.com/docs/api_reference/rest/handling_projects/
8
- class File < Entity
9
- RESPONSE_PARAMS = %i[
10
- datetime_removed datetime_stored datetime_uploaded is_image is_ready mime_type original_file_url
11
- original_filename size url uuid variations content_info metadata appdata source
12
- ].freeze
13
-
14
- client_service FileClient
15
-
16
- attr_entity(*RESPONSE_PARAMS)
17
-
18
- def datetime_stored
19
- Uploadcare.config.logger&.warn 'datetime_stored property has been deprecated, and will be removed without a replacement in future.' # rubocop:disable Layout/LineLength
20
- @entity.datetime_stored
21
- end
22
-
23
- # gets file's uuid - even if it's only initialized with url
24
- # @returns [String]
25
- def uuid
26
- return @entity.uuid if @entity.uuid
27
-
28
- uuid = @entity.url.gsub('https://ucarecdn.com/', '')
29
- uuid.gsub(%r{/.*}, '')
30
- end
31
-
32
- # loads file metadata, if it's initialized with url or uuid
33
- def load
34
- initialize(File.info(uuid).entity)
35
- end
36
-
37
- # The method to convert a document file to another file
38
- # gets (conversion) params [Hash], options (store: Boolean) [Hash], converter [Class]
39
- # @returns [File]
40
- def convert_document(params = {}, options = {}, converter = Conversion::DocumentConverter)
41
- convert_file(params, converter, options)
42
- end
43
-
44
- # The method to convert a video file to another file
45
- # gets (conversion) params [Hash], options (store: Boolean) [Hash], converter [Class]
46
- # @returns [File]
47
- def convert_video(params = {}, options = {}, converter = Conversion::VideoConverter)
48
- convert_file(params, converter, options)
49
- end
50
-
51
- # Copies file to current project
52
- #
53
- # source can be UID or full CDN link
54
- #
55
- # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/createLocalCopy
56
- def self.local_copy(source, args = {})
57
- response = FileClient.new.local_copy(source: source, **args).success[:result]
58
- File.new(response)
59
- end
60
-
61
- # copy file to different project
62
- #
63
- # source can be UID or full CDN link
64
- #
65
- # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/createRemoteCopy
66
- def self.remote_copy(source, target, args = {})
67
- FileClient.new.remote_copy(source: source, target: target, **args).success[:result]
68
- end
69
-
70
- # Instance version of {internal_copy}
71
- def local_copy(args = {})
72
- File.local_copy(uuid, **args)
73
- end
74
-
75
- # Instance version of {external_copy}
76
- def remote_copy(target, args = {})
77
- File.remote_copy(uuid, target, **args)
78
- end
79
-
80
- # Store a single file, preventing it from being deleted in 2 weeks
81
- # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/storeFile
82
- def store
83
- File.store(uuid)
84
- end
85
-
86
- # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/deleteFileStorage
87
- def delete
88
- File.delete(uuid)
89
- end
90
-
91
- private
92
-
93
- def convert_file(params, converter, options = {})
94
- raise Uploadcare::Exception::ConversionError, 'The first argument must be a Hash' unless params.is_a?(Hash)
95
-
96
- params_with_symbolized_keys = params.to_h { |k, v| [k.to_sym, v] }
97
- params_with_symbolized_keys[:uuid] = uuid
98
- result = converter.convert(params_with_symbolized_keys, options)
99
- result.success? ? File.info(result.value![:result].first[:uuid]) : result
100
- end
101
- end
102
- end
103
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'uploadcare/entity/file'
4
- require 'uploadcare/entity/decorator/paginator'
5
- require 'dry/monads'
6
- require 'api_struct'
7
-
8
- module Uploadcare
9
- module Entity
10
- # This serializer returns lists of files
11
- #
12
- # This is a paginated list, so all pagination methods apply
13
- # @see Uploadcare::Entity::Decorator::Paginator
14
- class FileList < ApiStruct::Entity
15
- include Uploadcare::Entity::Decorator::Paginator
16
- client_service Client::FileListClient
17
-
18
- attr_entity :next, :previous, :total, :per_page
19
-
20
- has_entities :results, as: Uploadcare::Entity::File
21
- has_entities :result, as: Uploadcare::Entity::File
22
-
23
- # alias for result/results, depending on which API this FileList was initialized from
24
- # @return [Array] of [Uploadcare::Entity::File]
25
- def files
26
- results
27
- rescue ApiStruct::EntityError
28
- result
29
- end
30
- end
31
- end
32
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Uploadcare
4
- module Entity
5
- # This serializer is responsible for file metadata handling
6
- #
7
- # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata
8
- class FileMetadata < Entity
9
- client_service FileMetadataClient
10
-
11
- class << self
12
- def index(uuid)
13
- ::Uploadcare::Client::FileMetadataClient.new.index(uuid).success
14
- end
15
-
16
- def show(uuid, key)
17
- ::Uploadcare::Client::FileMetadataClient.new.show(uuid, key).success
18
- end
19
-
20
- def update(uuid, key, value)
21
- ::Uploadcare::Client::FileMetadataClient.new.update(uuid, key, value).success
22
- end
23
-
24
- def delete(uuid, key)
25
- ::Uploadcare::Client::FileMetadataClient.new.delete(uuid, key).success || '200 OK'
26
- end
27
- end
28
- end
29
- end
30
- end