uploadcare-ruby 2.1.2 → 3.1.0

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 (117) 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 +33 -0
  7. data/.yardopts +4 -0
  8. data/CHANGELOG.md +29 -47
  9. data/DEVELOPMENT.md +18 -0
  10. data/Gemfile +2 -0
  11. data/LICENSE +1 -1
  12. data/README.md +433 -527
  13. data/Rakefile +5 -5
  14. data/bin/console +15 -0
  15. data/bin/setup +8 -0
  16. data/lib/uploadcare/api/api.rb +25 -0
  17. data/lib/uploadcare/client/conversion/base_conversion_client.rb +59 -0
  18. data/lib/uploadcare/client/conversion/document_conversion_client.rb +41 -0
  19. data/lib/uploadcare/client/conversion/video_conversion_client.rb +46 -0
  20. data/lib/uploadcare/client/file_client.rb +44 -0
  21. data/lib/uploadcare/client/file_list_client.rb +46 -0
  22. data/lib/uploadcare/client/group_client.rb +45 -0
  23. data/lib/uploadcare/client/multipart_upload/chunks_client.rb +57 -0
  24. data/lib/uploadcare/client/multipart_upload_client.rb +64 -0
  25. data/lib/uploadcare/client/project_client.rb +18 -0
  26. data/lib/uploadcare/client/rest_client.rb +74 -0
  27. data/lib/uploadcare/client/rest_group_client.rb +23 -0
  28. data/lib/uploadcare/client/upload_client.rb +43 -0
  29. data/lib/uploadcare/client/uploader_client.rb +101 -0
  30. data/lib/uploadcare/client/webhook_client.rb +47 -0
  31. data/lib/uploadcare/concern/error_handler.rb +54 -0
  32. data/lib/uploadcare/concern/throttle_handler.rb +25 -0
  33. data/lib/uploadcare/concern/upload_error_handler.rb +32 -0
  34. data/lib/uploadcare/entity/conversion/base_converter.rb +36 -0
  35. data/lib/uploadcare/entity/conversion/document_converter.rb +15 -0
  36. data/lib/uploadcare/entity/conversion/video_converter.rb +15 -0
  37. data/lib/uploadcare/entity/decorator/paginator.rb +79 -0
  38. data/lib/uploadcare/entity/entity.rb +18 -0
  39. data/lib/uploadcare/entity/file.rb +106 -0
  40. data/lib/uploadcare/entity/file_list.rb +31 -0
  41. data/lib/uploadcare/entity/group.rb +40 -0
  42. data/lib/uploadcare/entity/group_list.rb +24 -0
  43. data/lib/uploadcare/entity/project.rb +13 -0
  44. data/lib/uploadcare/entity/uploader.rb +81 -0
  45. data/lib/uploadcare/entity/webhook.rb +14 -0
  46. data/lib/uploadcare/exception/conversion_error.rb +8 -0
  47. data/lib/uploadcare/exception/request_error.rb +9 -0
  48. data/lib/uploadcare/exception/throttle_error.rb +16 -0
  49. data/lib/uploadcare/param/authentication_header.rb +25 -0
  50. data/lib/uploadcare/param/conversion/document/processing_job_url_builder.rb +39 -0
  51. data/lib/uploadcare/param/conversion/video/processing_job_url_builder.rb +64 -0
  52. data/lib/uploadcare/param/param.rb +10 -0
  53. data/lib/uploadcare/param/secure_auth_header.rb +37 -0
  54. data/lib/uploadcare/param/simple_auth_header.rb +14 -0
  55. data/lib/uploadcare/param/upload/signature_generator.rb +24 -0
  56. data/lib/uploadcare/param/upload/upload_params_generator.rb +23 -0
  57. data/lib/uploadcare/param/user_agent.rb +21 -0
  58. data/lib/uploadcare/ruby/version.rb +5 -0
  59. data/lib/uploadcare.rb +36 -32
  60. data/uploadcare-ruby.gemspec +50 -37
  61. metadata +107 -113
  62. data/.travis.yml +0 -26
  63. data/UPGRADE_NOTES.md +0 -36
  64. data/lib/uploadcare/api/file_api.rb +0 -7
  65. data/lib/uploadcare/api/file_list_api.rb +0 -19
  66. data/lib/uploadcare/api/file_storage_api.rb +0 -34
  67. data/lib/uploadcare/api/group_api.rb +0 -38
  68. data/lib/uploadcare/api/group_list_api.rb +0 -17
  69. data/lib/uploadcare/api/project_api.rb +0 -9
  70. data/lib/uploadcare/api/raw_api.rb +0 -38
  71. data/lib/uploadcare/api/uploading_api/upload_params.rb +0 -72
  72. data/lib/uploadcare/api/uploading_api.rb +0 -71
  73. data/lib/uploadcare/api/validators/file_list_options_validator.rb +0 -73
  74. data/lib/uploadcare/api/validators/group_list_options_validator.rb +0 -49
  75. data/lib/uploadcare/api.rb +0 -26
  76. data/lib/uploadcare/errors/errors.rb +0 -64
  77. data/lib/uploadcare/resources/file.rb +0 -164
  78. data/lib/uploadcare/resources/file_list.rb +0 -14
  79. data/lib/uploadcare/resources/group.rb +0 -115
  80. data/lib/uploadcare/resources/group_list.rb +0 -14
  81. data/lib/uploadcare/resources/project.rb +0 -13
  82. data/lib/uploadcare/resources/resource_list.rb +0 -83
  83. data/lib/uploadcare/rest/auth/auth.rb +0 -31
  84. data/lib/uploadcare/rest/auth/secure.rb +0 -43
  85. data/lib/uploadcare/rest/auth/simple.rb +0 -16
  86. data/lib/uploadcare/rest/connections/api_connection.rb +0 -53
  87. data/lib/uploadcare/rest/connections/upload_connection.rb +0 -22
  88. data/lib/uploadcare/rest/middlewares/auth_middleware.rb +0 -24
  89. data/lib/uploadcare/rest/middlewares/parse_json_middleware.rb +0 -33
  90. data/lib/uploadcare/rest/middlewares/raise_error_middleware.rb +0 -21
  91. data/lib/uploadcare/utils/parser.rb +0 -71
  92. data/lib/uploadcare/utils/user_agent.rb +0 -44
  93. data/lib/uploadcare/version.rb +0 -3
  94. data/spec/api/file_list_api_spec.rb +0 -95
  95. data/spec/api/file_storage_api_spec.rb +0 -88
  96. data/spec/api/group_list_api_spec.rb +0 -59
  97. data/spec/api/raw_api_spec.rb +0 -25
  98. data/spec/api/uploading_api/upload_params_spec.rb +0 -99
  99. data/spec/api/uploading_api_spec.rb +0 -59
  100. data/spec/resources/file_list_spec.rb +0 -25
  101. data/spec/resources/file_spec.rb +0 -223
  102. data/spec/resources/group_list_spec.rb +0 -25
  103. data/spec/resources/group_spec.rb +0 -101
  104. data/spec/resources/operations_spec.rb +0 -59
  105. data/spec/resources/project_spec.rb +0 -21
  106. data/spec/rest/api_connection_spec.rb +0 -68
  107. data/spec/rest/auth/secure_spec.rb +0 -66
  108. data/spec/rest/auth/simple_spec.rb +0 -31
  109. data/spec/rest/errors_spec.rb +0 -75
  110. data/spec/rest/upload_connection_spec.rb +0 -19
  111. data/spec/shared/resource_list.rb +0 -188
  112. data/spec/spec_helper.rb +0 -54
  113. data/spec/uploadcare_spec.rb +0 -43
  114. data/spec/utils/parser_spec.rb +0 -85
  115. data/spec/utils/user_agent_spec.rb +0 -46
  116. data/spec/view.png +0 -0
  117. data/spec/view2.jpg +0 -0
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env rake
1
+ # frozen_string_literal: true
2
+
2
3
  require 'bundler/gem_tasks'
3
4
  require 'rspec/core/rake_task'
4
- require 'coveralls/rake/task'
5
5
 
6
- task :default => :spec
7
- RSpec::Core::RakeTask.new
8
- Coveralls::RakeTask.new
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'uploadcare/ruby'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem.find_files('client/**/*.rb').each { |path| require path }
4
+ Gem.find_files('entity/**/*.rb').each { |path| require path }
5
+
6
+ module Uploadcare
7
+ # End-user interface
8
+ #
9
+ # It delegates methods to other classes:
10
+ # * To class methods of Entity objects
11
+ # * To instance methods of Client objects
12
+ # @see Uploadcare::Entity
13
+ # @see Uploadcare::Client
14
+ class Api
15
+ extend Forwardable
16
+ include Entity
17
+
18
+ def_delegator File, :file
19
+ def_delegators FileList, :file_list, :store_files, :delete_files
20
+ def_delegators Group, :group
21
+ def_delegators Project, :project
22
+ def_delegators Uploader, :upload, :upload_files, :upload_url
23
+ def_delegators Webhook, :create_webhook, :list_webhooks, :delete_webhook, :update_webhook
24
+ end
25
+ end
@@ -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
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'client/conversion/base_conversion_client'
4
+ require 'param/conversion/document/processing_job_url_builder'
5
+
6
+ module Uploadcare
7
+ module Client
8
+ module Conversion
9
+ # This is client for document conversion
10
+ #
11
+ # @see https://uploadcare.com/api-refs/rest-api/v0.6.0/#operation/documentConvert
12
+ class DocumentConversionClient < BaseConversionClient
13
+ def convert_many(
14
+ arr,
15
+ options = {},
16
+ url_builder_class = Param::Conversion::Document::ProcessingJobUrlBuilder
17
+ )
18
+ send_convert_request(arr, options, url_builder_class)
19
+ end
20
+
21
+ def get_conversion_status(token)
22
+ get(uri: "/convert/document/status/#{token}/")
23
+ end
24
+
25
+ private
26
+
27
+ def convert_uri
28
+ '/convert/document/'
29
+ end
30
+
31
+ def build_paths_body(params)
32
+ {
33
+ uuid: params[:uuid],
34
+ format: params[:format],
35
+ page: params[:page]
36
+ }.compact
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'client/conversion/base_conversion_client'
4
+ require 'param/conversion/video/processing_job_url_builder'
5
+ require 'exception/conversion_error'
6
+
7
+ module Uploadcare
8
+ module Client
9
+ module Conversion
10
+ # This is client for video conversion
11
+ #
12
+ # @see https://uploadcare.com/api-refs/rest-api/v0.6.0/#operation/videoConvert
13
+ class VideoConversionClient < BaseConversionClient
14
+ def convert_many(
15
+ params,
16
+ options = {},
17
+ url_builder_class = Param::Conversion::Video::ProcessingJobUrlBuilder
18
+ )
19
+ video_params = params.is_a?(Hash) ? [params] : params
20
+ send_convert_request(video_params, options, url_builder_class)
21
+ end
22
+
23
+ def get_conversion_status(token)
24
+ get(uri: "/convert/video/status/#{token}/")
25
+ end
26
+
27
+ private
28
+
29
+ def convert_uri
30
+ '/convert/video/'
31
+ end
32
+
33
+ def build_paths_body(params)
34
+ {
35
+ uuid: params[:uuid],
36
+ quality: params[:quality],
37
+ format: params[:format],
38
+ size: params[:size],
39
+ cut: params[:cut],
40
+ thumbs: params[:thumbs]
41
+ }.compact
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rest_client'
4
+
5
+ module Uploadcare
6
+ module Client
7
+ # API client for handling single files
8
+ # @see https://uploadcare.com/docs/api_reference/rest/accessing_files/
9
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#tag/File
10
+ class FileClient < RestClient
11
+ # Gets list of files without pagination fields
12
+ def index
13
+ response = get(uri: '/files/')
14
+ response.fmap { |i| i[:results] }
15
+ end
16
+
17
+ # Acquire file info
18
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/fileInfo
19
+ def info(uuid)
20
+ get(uri: "/files/#{uuid}/")
21
+ end
22
+ alias file info
23
+
24
+ # 'copy' method is used to copy original files or their modified versions to default storage.
25
+ # Source files MAY either be stored or just uploaded and MUST NOT be deleted.
26
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/copyFile
27
+ def copy(**options)
28
+ body = options.compact.to_json
29
+ post(uri: '/files/', content: body)
30
+ end
31
+
32
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/deleteFile
33
+ def delete(uuid)
34
+ request(method: 'DELETE', uri: "/files/#{uuid}/")
35
+ end
36
+
37
+ # Store a single file, preventing it from being deleted in 2 weeks
38
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/storeFile
39
+ def store(uuid)
40
+ put(uri: "/files/#{uuid}/storage/")
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rest_client'
4
+
5
+ module Uploadcare
6
+ module Client
7
+ # API client for handling file lists
8
+ class FileListClient < RestClient
9
+ # Returns a pagination json of files stored in project
10
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/filesList
11
+ #
12
+ # valid options:
13
+ # removed: [true|false]
14
+ # stored: [true|false]
15
+ # limit: (1..1000)
16
+ # ordering: ["datetime_uploaded"|"-datetime_uploaded"|"size"|"-size"]
17
+ # from: number of files skipped
18
+ def file_list(options = {})
19
+ query = options.empty? ? '' : "?#{URI.encode_www_form(options)}"
20
+ get(uri: "/files/#{query}")
21
+ end
22
+
23
+ # Make a set of files "stored". This will prevent them from being deleted automatically
24
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/filesStoring
25
+ # uuids: Array
26
+ def batch_store(uuids)
27
+ body = uuids.to_json
28
+ put(uri: '/files/storage/', content: body)
29
+ end
30
+
31
+ alias request_delete delete
32
+
33
+ # Delete several files by list of uids
34
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/filesDelete
35
+ # uuids: Array
36
+ def batch_delete(uuids)
37
+ body = uuids.to_json
38
+ request_delete(uri: '/files/storage/', content: body)
39
+ end
40
+
41
+ alias store_files batch_store
42
+ alias delete_files batch_delete
43
+ alias list file_list
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'upload_client'
4
+
5
+ module Uploadcare
6
+ module Client
7
+ # Groups serve a purpose of better organizing files in your Uploadcare projects.
8
+ # You can create one from a set of files by using their UUIDs.
9
+ # @see https://uploadcare.com/docs/api_reference/upload/groups/
10
+ class GroupClient < UploadClient
11
+ # Create files group from a set of files by using their UUIDs.
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)
15
+ body = HTTP::FormData::Multipart.new(body_hash)
16
+ post(path: 'group/',
17
+ headers: { 'Content-Type': body.content_type },
18
+ body: body)
19
+ end
20
+
21
+ # Get group info
22
+ # @see https://uploadcare.com/api-refs/upload-api/#operation/filesGroupInfo
23
+ def info(group_id)
24
+ get(path: 'group/info/', params: { 'pub_key': Uploadcare.config.public_key, 'group_id': group_id })
25
+ end
26
+
27
+ private
28
+
29
+ def file_params(file_ids)
30
+ ids = (0...file_ids.size).map { |i| "files[#{i}]" }
31
+ ids.zip(file_ids).to_h
32
+ end
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)
36
+ end
37
+
38
+ # API accepts only list of ids, but some users may want to upload list of files
39
+ # @return [Array] of [String]
40
+ def parse_file_list(file_list)
41
+ file_list.map { |file| file.methods.include?(:uuid) ? file.uuid : file }
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'parallel'
4
+ require 'api_struct'
5
+
6
+ module Uploadcare
7
+ module Client
8
+ module MultipartUpload
9
+ # This class splits file into chunks of set chunk_size
10
+ # and uploads them into cloud storage.
11
+ # Used for multipart uploads
12
+ # @see https://uploadcare.com/api-refs/upload-api/#tag/Upload/paths/https:~1~1uploadcare.s3-accelerate.amazonaws.com~1%3C%3Cpresigned-url%3E/put
13
+ class ChunksClient < ApiStruct::Client
14
+ CHUNK_SIZE = 5_242_880
15
+
16
+ # In multiple threads, split file into chunks and upload those chunks into respective Amazon links
17
+ # @param object [File]
18
+ # @param links [Array] of strings; by default list of Amazon storage urls
19
+ def self.upload_chunks(object, links)
20
+ Parallel.each(0...links.count, in_threads: Uploadcare.config.upload_threads) do |link_id|
21
+ offset = link_id * CHUNK_SIZE
22
+ chunk = IO.read(object, CHUNK_SIZE, offset)
23
+ new.upload_chunk(chunk, links[link_id])
24
+ next unless block_given?
25
+
26
+ yield(
27
+ chunk_size: CHUNK_SIZE,
28
+ object: object,
29
+ offset: offset,
30
+ link_id: link_id,
31
+ links: links,
32
+ links_count: links.count
33
+ )
34
+ end
35
+ end
36
+
37
+ def api_root
38
+ ''
39
+ end
40
+
41
+ def headers
42
+ {}
43
+ end
44
+
45
+ def upload_chunk(chunk, link)
46
+ put(path: link, body: chunk, headers: { 'Content-Type': 'application/octet-stream' })
47
+ end
48
+
49
+ private
50
+
51
+ def default_params
52
+ {}
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'client/multipart_upload/chunks_client'
4
+ require_relative 'upload_client'
5
+
6
+ module Uploadcare
7
+ module Client
8
+ # Client for multipart uploads
9
+ #
10
+ # @see https://uploadcare.com/api-refs/upload-api/#tag/Upload
11
+ class MultipartUploaderClient < UploadClient
12
+ include MultipartUpload
13
+
14
+ # Upload a big file by splitting it into parts and sending those parts into assigned buckets
15
+ # object should be File
16
+ def upload(object, store: false, &block)
17
+ response = upload_start(object, store: store)
18
+ return response unless response.success[:parts] && response.success[:uuid]
19
+
20
+ links = response.success[:parts]
21
+ uuid = response.success[:uuid]
22
+ ChunksClient.upload_chunks(object, links, &block)
23
+ upload_complete(uuid)
24
+ end
25
+
26
+ # Asks Uploadcare server to create a number of storage bin for uploads
27
+ def upload_start(object, store: false)
28
+ body = HTTP::FormData::Multipart.new(
29
+ Param::Upload::UploadParamsGenerator.call(store).merge(form_data_for(object))
30
+ )
31
+ post(path: 'multipart/start/',
32
+ headers: { 'Content-Type': body.content_type },
33
+ body: body)
34
+ end
35
+
36
+ # When every chunk is uploaded, ask Uploadcare server to finish the upload
37
+ def upload_complete(uuid)
38
+ body = HTTP::FormData::Multipart.new(
39
+ {
40
+ UPLOADCARE_PUB_KEY: Uploadcare.config.public_key,
41
+ uuid: uuid
42
+ }
43
+ )
44
+ post(path: 'multipart/complete/', body: body, headers: { 'Content-Type': body.content_type })
45
+ end
46
+
47
+ private
48
+
49
+ def form_data_for(file)
50
+ form_data_file = super(file)
51
+ {
52
+ filename: form_data_file.filename,
53
+ size: form_data_file.size,
54
+ content_type: form_data_file.content_type
55
+ }
56
+ end
57
+
58
+ alias api_struct_post post
59
+ def post(**args)
60
+ handle_throttling { api_struct_post(**args) }
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uploadcare
4
+ module Client
5
+ # API client for getting project info
6
+ # @see https://uploadcare.com/docs/api_reference/rest/handling_projects/
7
+ class ProjectClient < RestClient
8
+ # get information about current project
9
+ # current project is determined by public and private key combination
10
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#tag/Project
11
+ def show
12
+ get(uri: '/project/')
13
+ end
14
+
15
+ alias project show
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rest_client'
4
+ require 'api_struct'
5
+ require 'uploadcare/concern/error_handler'
6
+ require 'uploadcare/concern/throttle_handler'
7
+ require 'param/authentication_header'
8
+
9
+ module Uploadcare
10
+ module Client
11
+ # @abstract
12
+ # General client for signed REST requests
13
+ class RestClient < ApiStruct::Client
14
+ include Uploadcare::Concerns::ErrorHandler
15
+ include Uploadcare::Concerns::ThrottleHandler
16
+ include Exception
17
+
18
+ alias api_struct_delete delete
19
+ alias api_struct_get get
20
+ alias api_struct_post post
21
+ alias api_struct_put put
22
+
23
+ # Send request with authentication header
24
+ #
25
+ # Handle throttling as well
26
+ def request(uri:, method: 'GET', **options)
27
+ request_headers = Param::AuthenticationHeader.call(method: method.upcase, uri: uri,
28
+ content_type: headers[:'Content-Type'], **options)
29
+ handle_throttling do
30
+ send("api_struct_#{method.downcase}", path: remove_trailing_slash(uri),
31
+ headers: request_headers, body: options[:content])
32
+ end
33
+ end
34
+
35
+ def get(**options)
36
+ request(method: 'GET', **options)
37
+ end
38
+
39
+ def post(**options)
40
+ request(method: 'POST', **options)
41
+ end
42
+
43
+ def put(**options)
44
+ request(method: 'PUT', **options)
45
+ end
46
+
47
+ def delete(**options)
48
+ request(method: 'DELETE', **options)
49
+ end
50
+
51
+ def api_root
52
+ Uploadcare.config.rest_api_root
53
+ end
54
+
55
+ def headers
56
+ {
57
+ 'Content-Type': 'application/json',
58
+ 'Accept': 'application/vnd.uploadcare-v0.5+json',
59
+ 'User-Agent': Uploadcare::Param::UserAgent.call
60
+ }
61
+ end
62
+
63
+ private
64
+
65
+ def remove_trailing_slash(str)
66
+ str.gsub(%r{^/}, '')
67
+ end
68
+
69
+ def default_params
70
+ {}
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rest_client'
4
+
5
+ module Uploadcare
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
8
+ class RestGroupClient < RestClient
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
11
+ def store(uuid)
12
+ put(uri: "/groups/#{uuid}/storage/")
13
+ end
14
+
15
+ # return paginated list of groups
16
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/groupsList
17
+ def list(options = {})
18
+ query = options.empty? ? '' : "?#{URI.encode_www_form(options)}"
19
+ get(uri: "/groups/#{query}")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'api_struct'
4
+ require 'param/user_agent'
5
+ require 'uploadcare/concern/error_handler'
6
+ require 'uploadcare/concern/throttle_handler'
7
+
8
+ module Uploadcare
9
+ module Client
10
+ # @abstract
11
+ #
12
+ # Headers and helper methods for clients working with upload API
13
+ # @see https://uploadcare.com/docs/api_reference/upload/
14
+ class UploadClient < ApiStruct::Client
15
+ include Concerns::ErrorHandler
16
+ include Concerns::ThrottleHandler
17
+ include Exception
18
+
19
+ def api_root
20
+ Uploadcare.config.upload_api_root
21
+ end
22
+
23
+ def headers
24
+ {
25
+ 'User-Agent': Uploadcare::Param::UserAgent.call
26
+ }
27
+ end
28
+
29
+ private
30
+
31
+ def form_data_for(file)
32
+ filename = file.original_filename if file.respond_to?(:original_filename)
33
+ mime_type = file.content_type if file.respond_to?(:content_type)
34
+ options = { filename: filename, content_type: mime_type }.compact
35
+ HTTP::FormData::File.new(file, options)
36
+ end
37
+
38
+ def default_params
39
+ {}
40
+ end
41
+ end
42
+ end
43
+ end