uploadcare-ruby 3.3.2 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/gem-push.yml +4 -2
  3. data/.github/workflows/ruby.yml +11 -7
  4. data/.gitignore +6 -0
  5. data/.rubocop.yml +1 -0
  6. data/CHANGELOG.md +49 -0
  7. data/README.md +194 -37
  8. data/lib/uploadcare/client/addons_client.rb +56 -0
  9. data/lib/uploadcare/client/conversion/base_conversion_client.rb +2 -2
  10. data/lib/uploadcare/client/conversion/document_conversion_client.rb +1 -1
  11. data/lib/uploadcare/client/conversion/video_conversion_client.rb +1 -1
  12. data/lib/uploadcare/client/file_client.rb +14 -10
  13. data/lib/uploadcare/client/file_list_client.rb +4 -4
  14. data/lib/uploadcare/client/file_metadata_client.rb +36 -0
  15. data/lib/uploadcare/client/group_client.rb +5 -5
  16. data/lib/uploadcare/client/multipart_upload/chunks_client.rb +2 -1
  17. data/lib/uploadcare/client/multipart_upload_client.rb +5 -4
  18. data/lib/uploadcare/client/project_client.rb +1 -1
  19. data/lib/uploadcare/client/rest_client.rb +6 -6
  20. data/lib/uploadcare/client/rest_group_client.rb +24 -4
  21. data/lib/uploadcare/client/upload_client.rb +4 -1
  22. data/lib/uploadcare/client/uploader_client.rb +13 -12
  23. data/lib/uploadcare/client/webhook_client.rb +8 -8
  24. data/lib/uploadcare/concern/error_handler.rb +1 -1
  25. data/lib/uploadcare/concern/upload_error_handler.rb +1 -1
  26. data/lib/uploadcare/entity/addons.rb +14 -0
  27. data/lib/uploadcare/entity/conversion/base_converter.rb +1 -1
  28. data/lib/uploadcare/entity/decorator/paginator.rb +2 -2
  29. data/lib/uploadcare/entity/file.rb +20 -38
  30. data/lib/uploadcare/entity/file_list.rb +1 -0
  31. data/lib/uploadcare/entity/file_metadata.rb +30 -0
  32. data/lib/uploadcare/entity/group.rb +11 -2
  33. data/lib/uploadcare/entity/uploader.rb +13 -13
  34. data/lib/uploadcare/param/authentication_header.rb +2 -2
  35. data/lib/uploadcare/param/secure_auth_header.rb +3 -3
  36. data/lib/uploadcare/param/simple_auth_header.rb +1 -1
  37. data/lib/uploadcare/param/upload/signature_generator.rb +2 -2
  38. data/lib/uploadcare/param/upload/upload_params_generator.rb +25 -8
  39. data/lib/uploadcare/param/webhook_signature_verifier.rb +1 -1
  40. data/lib/uploadcare/ruby/version.rb +1 -1
  41. data/lib/uploadcare.rb +1 -0
  42. data/uploadcare-ruby.gemspec +11 -11
  43. metadata +46 -36
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rest_client'
4
+
5
+ module Uploadcare
6
+ module Client
7
+ # API client for handling single metadata_files
8
+ # @see https://uploadcare.com/docs/file-metadata/
9
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata
10
+ class FileMetadataClient < RestClient
11
+ # Get file's metadata keys and values
12
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/fileMetadata
13
+ def index(uuid)
14
+ get(uri: "/files/#{uuid}/metadata/")
15
+ end
16
+
17
+ # Get the value of a single metadata key.
18
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/fileMetadataKey
19
+ def show(uuid, key)
20
+ get(uri: "/files/#{uuid}/metadata/#{key}/")
21
+ end
22
+
23
+ # Update the value of a single metadata key. If the key does not exist, it will be created.
24
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/updateFileMetadataKey
25
+ def update(uuid, key, value)
26
+ put(uri: "/files/#{uuid}/metadata/#{key}/", content: value.to_json)
27
+ end
28
+
29
+ # Delete a file's metadata key.
30
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/deleteFileMetadataKey
31
+ def delete(uuid, key)
32
+ request(method: 'DELETE', uri: "/files/#{uuid}/metadata/#{key}/")
33
+ end
34
+ end
35
+ end
36
+ end
@@ -10,8 +10,8 @@ module Uploadcare
10
10
  class GroupClient < UploadClient
11
11
  # Create files group from a set of files by using their UUIDs.
12
12
  # @see https://uploadcare.com/api-refs/upload-api/#operation/createFilesGroup
13
- def create(file_list, **options)
14
- body_hash = group_body_hash(file_list, **options)
13
+ def create(file_list, options = {})
14
+ body_hash = group_body_hash(file_list, options)
15
15
  body = HTTP::FormData::Multipart.new(body_hash)
16
16
  post(path: 'group/',
17
17
  headers: { 'Content-Type': body.content_type },
@@ -21,7 +21,7 @@ module Uploadcare
21
21
  # Get group info
22
22
  # @see https://uploadcare.com/api-refs/upload-api/#operation/filesGroupInfo
23
23
  def info(group_id)
24
- get(path: 'group/info/', params: { 'pub_key': Uploadcare.config.public_key, 'group_id': group_id })
24
+ get(path: 'group/info/', params: { pub_key: Uploadcare.config.public_key, group_id: group_id })
25
25
  end
26
26
 
27
27
  private
@@ -31,8 +31,8 @@ module Uploadcare
31
31
  ids.zip(file_ids).to_h
32
32
  end
33
33
 
34
- def group_body_hash(file_list, **options)
35
- { pub_key: Uploadcare.config.public_key }.merge(file_params(parse_file_list(file_list))).merge(**options)
34
+ def group_body_hash(file_list, options = {})
35
+ { pub_key: Uploadcare.config.public_key }.merge(file_params(parse_file_list(file_list))).merge(options)
36
36
  end
37
37
 
38
38
  # API accepts only list of ids, but some users may want to upload list of files
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'parallel'
4
+ require 'dry/monads'
4
5
  require 'api_struct'
5
6
 
6
7
  module Uploadcare
@@ -19,7 +20,7 @@ module Uploadcare
19
20
  def self.upload_chunks(object, links)
20
21
  Parallel.each(0...links.count, in_threads: Uploadcare.config.upload_threads) do |link_id|
21
22
  offset = link_id * CHUNK_SIZE
22
- chunk = IO.read(object, CHUNK_SIZE, offset)
23
+ chunk = File.read(object, CHUNK_SIZE, offset)
23
24
  new.upload_chunk(chunk, links[link_id])
24
25
  next unless block_given?
25
26
 
@@ -13,8 +13,8 @@ module Uploadcare
13
13
 
14
14
  # Upload a big file by splitting it into parts and sending those parts into assigned buckets
15
15
  # object should be File
16
- def upload(object, store: false, &block)
17
- response = upload_start(object, store: store)
16
+ def upload(object, options = {}, &block)
17
+ response = upload_start(object, options)
18
18
  return response unless response.success[:parts] && response.success[:uuid]
19
19
 
20
20
  links = response.success[:parts]
@@ -24,9 +24,10 @@ module Uploadcare
24
24
  end
25
25
 
26
26
  # Asks Uploadcare server to create a number of storage bin for uploads
27
- def upload_start(object, store: false)
27
+ def upload_start(object, options = {})
28
+ options.merge!(store: options[:store] || false)
28
29
  body = HTTP::FormData::Multipart.new(
29
- Param::Upload::UploadParamsGenerator.call(store).merge(form_data_for(object))
30
+ Param::Upload::UploadParamsGenerator.call(options).merge(form_data_for(object))
30
31
  )
31
32
  post(path: 'multipart/start/',
32
33
  headers: { 'Content-Type': body.content_type },
@@ -9,7 +9,7 @@ module Uploadcare
9
9
  class ProjectClient < RestClient
10
10
  # get information about current project
11
11
  # current project is determined by public and secret key combination
12
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#tag/Project
12
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/Project
13
13
  def show
14
14
  get(uri: '/project/')
15
15
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'rest_client'
3
+ require 'dry/monads'
4
4
  require 'api_struct'
5
5
  require 'uploadcare/concern/error_handler'
6
6
  require 'uploadcare/concern/throttle_handler'
@@ -32,19 +32,19 @@ module Uploadcare
32
32
  end
33
33
  end
34
34
 
35
- def get(**options)
35
+ def get(options = {})
36
36
  request(method: 'GET', **options)
37
37
  end
38
38
 
39
- def post(**options)
39
+ def post(options = {})
40
40
  request(method: 'POST', **options)
41
41
  end
42
42
 
43
- def put(**options)
43
+ def put(options = {})
44
44
  request(method: 'PUT', **options)
45
45
  end
46
46
 
47
- def delete(**options)
47
+ def delete(options = {})
48
48
  request(method: 'DELETE', **options)
49
49
  end
50
50
 
@@ -55,7 +55,7 @@ module Uploadcare
55
55
  def headers
56
56
  {
57
57
  'Content-Type': 'application/json',
58
- 'Accept': 'application/vnd.uploadcare-v0.5+json',
58
+ Accept: 'application/vnd.uploadcare-v0.7+json',
59
59
  'User-Agent': Uploadcare::Param::UserAgent.call
60
60
  }
61
61
  end
@@ -4,20 +4,40 @@ require_relative 'rest_client'
4
4
 
5
5
  module Uploadcare
6
6
  module Client
7
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#tag/Group/paths/~1groups~1%3Cuuid%3E~1storage~1/put
7
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/Group/paths/~1groups~1%3Cuuid%3E~1storage~1/put
8
8
  class RestGroupClient < RestClient
9
9
  # store all files in a group
10
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#tag/Group/paths/~1groups~1%3Cuuid%3E~1storage~1/put
10
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/storeFile
11
11
  def store(uuid)
12
- put(uri: "/groups/#{uuid}/storage/")
12
+ files = info(uuid).success[:files].compact
13
+ client = ::Uploadcare::Client::FileClient.new
14
+ files.each_slice(Uploadcare.config.file_chunk_size) do |file_chunk|
15
+ file_chunk.each do |file|
16
+ client.store(file[:uuid])
17
+ end
18
+ end
19
+
20
+ Dry::Monads::Success(nil)
21
+ end
22
+
23
+ # Get a file group by its ID.
24
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/groupInfo
25
+ def info(uuid)
26
+ get(uri: "/groups/#{uuid}/")
13
27
  end
14
28
 
15
29
  # return paginated list of groups
16
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/groupsList
30
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/groupsList
17
31
  def list(options = {})
18
32
  query = options.empty? ? '' : "?#{URI.encode_www_form(options)}"
19
33
  get(uri: "/groups/#{query}")
20
34
  end
35
+
36
+ # Delete a file group by its ID.
37
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/deleteGroup
38
+ def delete(uuid)
39
+ request(method: 'DELETE', uri: "/groups/#{uuid}/")
40
+ end
21
41
  end
22
42
  end
23
43
  end
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'dry/monads'
3
4
  require 'api_struct'
4
5
  require 'param/user_agent'
5
6
  require 'uploadcare/concern/error_handler'
6
7
  require 'uploadcare/concern/throttle_handler'
8
+ require 'mimemagic'
7
9
 
8
10
  module Uploadcare
9
11
  module Client
@@ -30,7 +32,8 @@ module Uploadcare
30
32
 
31
33
  def form_data_for(file)
32
34
  filename = file.original_filename if file.respond_to?(:original_filename)
33
- mime_type = file.content_type if file.respond_to?(:content_type)
35
+ mime_type = MimeMagic.by_magic(file)&.type
36
+ mime_type = file.content_type if mime_type.nil? && file.respond_to?(:content_type)
34
37
  options = { filename: filename, content_type: mime_type }.compact
35
38
  HTTP::FormData::File.new(file, options)
36
39
  end
@@ -12,8 +12,8 @@ module Uploadcare
12
12
  class UploaderClient < UploadClient
13
13
  # @see https://uploadcare.com/api-refs/upload-api/#operation/baseUpload
14
14
 
15
- def upload_many(arr, **options)
16
- body = upload_many_body(arr, **options)
15
+ def upload_many(arr, options = {})
16
+ body = upload_many_body(arr, options)
17
17
  post(path: 'base/',
18
18
  headers: { 'Content-Type': body.content_type },
19
19
  body: body)
@@ -22,8 +22,8 @@ module Uploadcare
22
22
  # syntactic sugar for upload_many
23
23
  # There is actual upload method for one file, but it is redundant
24
24
 
25
- def upload(file, **options)
26
- upload_many([file], **options)
25
+ def upload(file, options = {})
26
+ upload_many([file], options)
27
27
  end
28
28
 
29
29
  # Upload files from url
@@ -33,8 +33,9 @@ module Uploadcare
33
33
  # - filename
34
34
  # - save_URL_duplicates
35
35
  # - async - returns upload token instead of upload data
36
- def upload_from_url(url, **options)
37
- body = upload_from_url_body(url, **options)
36
+ # - metadata - file metadata, hash
37
+ def upload_from_url(url, options = {})
38
+ body = upload_from_url_body(url, options)
38
39
  token_response = post(path: 'from_url/', headers: { 'Content-Type': body.content_type }, body: body)
39
40
  return token_response if options[:async]
40
41
 
@@ -55,7 +56,7 @@ module Uploadcare
55
56
  private
56
57
 
57
58
  alias api_struct_post post
58
- def post(**args)
59
+ def post(args = {})
59
60
  handle_throttling { api_struct_post(**args) }
60
61
  end
61
62
 
@@ -71,13 +72,13 @@ module Uploadcare
71
72
  end
72
73
 
73
74
  # Prepares body for upload_many method
74
- def upload_many_body(arr, **options)
75
- files_formdata = arr.map do |file|
75
+ def upload_many_body(arr, options = {})
76
+ files_formdata = arr.to_h do |file|
76
77
  [HTTP::FormData::File.new(file).filename,
77
78
  form_data_for(file)]
78
- end.to_h
79
+ end
79
80
  HTTP::FormData::Multipart.new(
80
- Param::Upload::UploadParamsGenerator.call(options[:store]).merge(files_formdata)
81
+ Param::Upload::UploadParamsGenerator.call(options).merge(files_formdata)
81
82
  )
82
83
  end
83
84
 
@@ -87,7 +88,7 @@ module Uploadcare
87
88
  }.freeze
88
89
 
89
90
  # Prepare upload_from_url initial request body
90
- def upload_from_url_body(url, **options)
91
+ def upload_from_url_body(url, options = {})
91
92
  HTTP::FormData::Multipart.new(
92
93
  options.merge(
93
94
  'pub_key' => Uploadcare.config.public_key,
@@ -5,17 +5,17 @@ require_relative 'rest_client'
5
5
  module Uploadcare
6
6
  module Client
7
7
  # client for webhook management
8
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#tag/Webhook
8
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/Webhook
9
9
  class WebhookClient < RestClient
10
10
  # Create webhook
11
11
  # @see https://uploadcare.com/docs/api_reference/rest/webhooks/#subscribe
12
12
  def create(options = {})
13
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]
14
+ target_url: options[:target_url],
15
+ event: options[:event] || 'file.uploaded',
16
+ is_active: options[:is_active].nil? ? true : options[:is_active]
17
17
  }.merge(
18
- { 'signing_secret': options[:signing_secret] }.compact
18
+ { signing_secret: options[:signing_secret] }.compact
19
19
  ).to_json
20
20
  post(uri: '/webhooks/', content: body)
21
21
  end
@@ -29,15 +29,15 @@ module Uploadcare
29
29
  # Permanently deletes subscription
30
30
  # @see https://uploadcare.com/docs/api_reference/rest/webhooks/#unsubscribe
31
31
  def delete(target_url)
32
- body = { 'target_url': target_url }.to_json
33
- post(uri: '/webhooks/unsubscribe/', content: body)
32
+ body = { target_url: target_url }.to_json
33
+ request(method: 'DELETE', uri: '/webhooks/unsubscribe/', content: body)
34
34
  end
35
35
 
36
36
  # Updates webhook
37
37
  # @see https://uploadcare.com/docs/api_reference/rest/webhooks/#subscribe-update
38
38
  def update(id, options = {})
39
39
  body = options.to_json
40
- post(uri: "/webhooks/#{id}/", content: body)
40
+ put(uri: "/webhooks/#{id}/", content: body)
41
41
  end
42
42
 
43
43
  alias create_webhook create
@@ -36,7 +36,7 @@ module Uploadcare
36
36
 
37
37
  # Raise ThrottleError. Also, tells in error when server will be ready for next request
38
38
  def raise_throttling_error(response)
39
- retry_after = response.headers['Retry-After'].to_i + 1 || 11
39
+ retry_after = (response.headers['Retry-After'].to_i + 1) || 11
40
40
  raise ThrottleError.new(retry_after), "Response throttled, retry #{retry_after} seconds later"
41
41
  end
42
42
 
@@ -24,7 +24,7 @@ module Uploadcare
24
24
  def catch_throttling_error(response)
25
25
  return unless response.code == 429
26
26
 
27
- retry_after = response.headers['Retry-After'].to_i + 1 || 11
27
+ retry_after = (response.headers['Retry-After'].to_i + 1) || 11
28
28
  raise ThrottleError.new(retry_after), "Response throttled, retry #{retry_after} seconds later"
29
29
  end
30
30
  end
@@ -0,0 +1,14 @@
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
@@ -4,7 +4,7 @@ module Uploadcare
4
4
  module Entity
5
5
  module Conversion
6
6
  # This serializer lets a user convert uploaded documents
7
- # @see https://uploadcare.com/api-refs/rest-api/v0.6.0/#operation/documentConvert
7
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/documentConvert
8
8
  class BaseConverter < Entity
9
9
  class << self
10
10
  # Converts files
@@ -26,7 +26,7 @@ module Uploadcare
26
26
  return unless url
27
27
 
28
28
  query = URI.decode_www_form(URI(url).query).to_h
29
- query = query.map { |k, v| [k.to_sym, v] }.to_h
29
+ query = query.to_h { |k, v| [k.to_sym, v] }
30
30
  self.class.list(**query)
31
31
  end
32
32
 
@@ -36,7 +36,7 @@ module Uploadcare
36
36
  return unless url
37
37
 
38
38
  query = URI.decode_www_form(URI(url).query).to_h
39
- query = query.map { |k, v| [k.to_sym, v] }.to_h
39
+ query = query.to_h { |k, v| [k.to_sym, v] }
40
40
  self.class.list(**query)
41
41
  end
42
42
 
@@ -6,11 +6,14 @@ module Uploadcare
6
6
  #
7
7
  # @see https://uploadcare.com/docs/api_reference/rest/handling_projects/
8
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
+
9
14
  client_service FileClient
10
15
 
11
- attr_entity :datetime_removed, :datetime_stored, :datetime_uploaded, :image_info, :is_image, :is_ready,
12
- :mime_type, :original_file_url, :original_filename, :size, :url, :uuid, :variations, :video_info,
13
- :source, :rekognition_info
16
+ attr_entity(*RESPONSE_PARAMS)
14
17
 
15
18
  # gets file's uuid - even if it's only initialized with url
16
19
  # @returns [String]
@@ -40,64 +43,43 @@ module Uploadcare
40
43
  convert_file(params, converter, options)
41
44
  end
42
45
 
43
- # 'copy' method is used to copy original files or their modified versions to default storage.
44
- #
45
- # Source files MAY either be stored or just uploaded and MUST NOT be deleted.
46
- #
47
- # @param [String] source uuid or uploadcare link to file.
48
- # @param [Hash] args
49
- # @option args [Boolean] :store Whether to store the file
50
- # @option args [Boolean] :strip_operations Copies file without transformations (if source has them)
51
- # @option args [String] :target points to a target custom storage.
52
- # @option args [Boolean] :make_public make files on custom storage available via public links.
53
- # @option args [String] :pattern define file naming pattern for the custom storage scenario.
54
- #
55
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/copyFile
56
- def self.copy(source, **args)
57
- response = FileClient.new.copy(source: source, **args).success[:result]
58
- File.new(response)
59
- end
60
-
61
46
  # Copies file to current project
62
47
  #
63
48
  # source can be UID or full CDN link
64
49
  #
65
- # @see .copy
66
- def self.local_copy(source, **args)
67
- File.copy(source, **args)
50
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/createLocalCopy
51
+ def self.local_copy(source, args = {})
52
+ response = FileClient.new.local_copy(source: source, **args).success[:result]
53
+ File.new(response)
68
54
  end
69
55
 
70
56
  # copy file to different project
71
57
  #
72
58
  # source can be UID or full CDN link
73
59
  #
74
- # @see .copy
75
- def self.remote_copy(source, target, **args)
76
- File.copy(source: source, target: target, **args)
77
- end
78
-
79
- # Instance version of #{copy}. Copies current file.
80
- def copy(**args)
81
- File.copy(uuid, **args)
60
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/createRemoteCopy
61
+ def self.remote_copy(source, target, args = {})
62
+ response = FileClient.new.remote_copy(source: source, target: target, **args).success[:result]
63
+ File.new(response)
82
64
  end
83
65
 
84
66
  # Instance version of {internal_copy}
85
- def local_copy(**args)
67
+ def local_copy(args = {})
86
68
  File.local_copy(uuid, **args)
87
69
  end
88
70
 
89
71
  # Instance version of {external_copy}
90
- def remote_copy(target, **args)
91
- File.copy(uuid, target: target, **args)
72
+ def remote_copy(target, args = {})
73
+ File.remote_copy(uuid, target, **args)
92
74
  end
93
75
 
94
76
  # Store a single file, preventing it from being deleted in 2 weeks
95
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/storeFile
77
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/storeFile
96
78
  def store
97
79
  File.store(uuid)
98
80
  end
99
81
 
100
- # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/deleteFile
82
+ # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/deleteFileStorage
101
83
  def delete
102
84
  File.delete(uuid)
103
85
  end
@@ -107,7 +89,7 @@ module Uploadcare
107
89
  def convert_file(params, converter, options = {})
108
90
  raise Uploadcare::Exception::ConversionError, 'The first argument must be a Hash' unless params.is_a?(Hash)
109
91
 
110
- params_with_symbolized_keys = params.map { |k, v| [k.to_sym, v] }.to_h
92
+ params_with_symbolized_keys = params.to_h { |k, v| [k.to_sym, v] }
111
93
  params_with_symbolized_keys[:uuid] = uuid
112
94
  result = converter.convert(params_with_symbolized_keys, options)
113
95
  result.success? ? File.info(result.value![:result].first[:uuid]) : result
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'uploadcare/entity/file'
4
4
  require 'uploadcare/entity/decorator/paginator'
5
+ require 'dry/monads'
5
6
  require 'api_struct'
6
7
 
7
8
  module Uploadcare
@@ -0,0 +1,30 @@
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
@@ -10,7 +10,7 @@ module Uploadcare
10
10
  #
11
11
  # @see https://uploadcare.com/docs/api_reference/upload/groups/
12
12
  class Group < Entity
13
- client_service RestGroupClient, prefix: 'rest', only: :store
13
+ client_service RestGroupClient, prefix: 'rest', only: %i[store info delete]
14
14
  client_service GroupClient
15
15
 
16
16
  attr_entity :id, :datetime_created, :datetime_stored, :files_count, :cdn_url, :url
@@ -19,7 +19,16 @@ module Uploadcare
19
19
  # Remove these lines and bump api_struct version when this PR is accepted:
20
20
  # @see https://github.com/rubygarage/api_struct/pull/15
21
21
  def self.store(uuid)
22
- rest_store(uuid)
22
+ rest_store(uuid).success || '200 OK'
23
+ end
24
+
25
+ # Get a file group by its ID.
26
+ def self.group_info(uuid)
27
+ rest_info(uuid)
28
+ end
29
+
30
+ def self.delete(uuid)
31
+ rest_delete(uuid).success || '200 OK'
23
32
  end
24
33
 
25
34
  # gets groups's id - even if it's only initialized with cdn_url
@@ -16,42 +16,42 @@ module Uploadcare
16
16
  # @param object [Array], [String] or [File]
17
17
  # @param [Hash] options options for upload
18
18
  # @option options [Boolean] :store (false) whether to store file on servers.
19
- def self.upload(object, **options)
19
+ def self.upload(object, options = {})
20
20
  if big_file?(object)
21
- multipart_upload(object, **options)
21
+ multipart_upload(object, options)
22
22
  elsif file?(object)
23
- upload_file(object, **options)
23
+ upload_file(object, options)
24
24
  elsif object.is_a?(Array)
25
- upload_files(object, **options)
25
+ upload_files(object, options)
26
26
  elsif object.is_a?(String)
27
- upload_from_url(object, **options)
27
+ upload_from_url(object, options)
28
28
  else
29
29
  raise ArgumentError, "Expected input to be a file/Array/URL, given: `#{object}`"
30
30
  end
31
31
  end
32
32
 
33
33
  # upload single file
34
- def self.upload_file(file, **options)
35
- response = UploaderClient.new.upload_many([file], **options)
34
+ def self.upload_file(file, options = {})
35
+ response = UploaderClient.new.upload_many([file], options)
36
36
  Uploadcare::Entity::File.info(response.success.to_a.flatten[-1])
37
37
  end
38
38
 
39
39
  # upload multiple files
40
- def self.upload_files(arr, **options)
41
- response = UploaderClient.new.upload_many(arr, **options)
40
+ def self.upload_files(arr, options = {})
41
+ response = UploaderClient.new.upload_many(arr, options)
42
42
  response.success.map { |pair| Uploadcare::Entity::File.new(uuid: pair[1], original_filename: pair[0]) }
43
43
  end
44
44
 
45
45
  # upload file of size above 10mb (involves multipart upload)
46
- def self.multipart_upload(file, **options, &block)
47
- response = MultipartUploaderClient.new.upload(file, **options, &block)
46
+ def self.multipart_upload(file, options = {}, &block)
47
+ response = MultipartUploaderClient.new.upload(file, options, &block)
48
48
  Uploadcare::Entity::File.new(response.success)
49
49
  end
50
50
 
51
51
  # upload files from url
52
52
  # @param url [String]
53
- def self.upload_from_url(url, **options)
54
- response = UploaderClient.new.upload_from_url(url, **options)
53
+ def self.upload_from_url(url, options = {})
54
+ response = UploaderClient.new.upload_from_url(url, options)
55
55
  return response.success[:token] unless response.success[:files]
56
56
 
57
57
  response.success[:files].map { |file_data| Uploadcare::Entity::File.new(file_data) }
@@ -10,10 +10,10 @@ module Uploadcare
10
10
  # This authentication method is more secure, but more tedious
11
11
  class AuthenticationHeader
12
12
  # @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare
13
- def self.call(**options)
13
+ def self.call(options = {})
14
14
  case Uploadcare.config.auth_type
15
15
  when 'Uploadcare'
16
- SecureAuthHeader.call(**options)
16
+ SecureAuthHeader.call(options)
17
17
  when 'Uploadcare.Simple'
18
18
  SimpleAuthHeader.call
19
19
  else