uploadcare-ruby 2.1.1 → 3.1.0.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/gem-push.yml +20 -0
- data/.github/workflows/ruby.yml +52 -0
- data/.gitignore +13 -6
- data/.rspec +2 -0
- data/.rubocop.yml +32 -0
- data/.yardopts +4 -0
- data/CHANGELOG.md +33 -34
- data/DEVELOPMENT.md +18 -0
- data/Gemfile +2 -0
- data/LICENSE +1 -1
- data/README.md +369 -546
- data/Rakefile +5 -5
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/uploadcare.rb +36 -26
- data/lib/uploadcare/api/api.rb +25 -0
- data/lib/uploadcare/client/conversion/base_conversion_client.rb +54 -0
- data/lib/uploadcare/client/conversion/document_conversion_client.rb +38 -0
- data/lib/uploadcare/client/conversion/video_conversion_client.rb +42 -0
- data/lib/uploadcare/client/file_client.rb +44 -0
- data/lib/uploadcare/client/file_list_client.rb +46 -0
- data/lib/uploadcare/client/group_client.rb +45 -0
- data/lib/uploadcare/client/multipart_upload/chunks_client.rb +48 -0
- data/lib/uploadcare/client/multipart_upload_client.rb +67 -0
- data/lib/uploadcare/client/project_client.rb +18 -0
- data/lib/uploadcare/client/rest_client.rb +74 -0
- data/lib/uploadcare/client/rest_group_client.rb +23 -0
- data/lib/uploadcare/client/upload_client.rb +36 -0
- data/lib/uploadcare/client/uploader_client.rb +109 -0
- data/lib/uploadcare/client/webhook_client.rb +47 -0
- data/lib/uploadcare/concern/error_handler.rb +54 -0
- data/lib/uploadcare/concern/throttle_handler.rb +25 -0
- data/lib/uploadcare/concern/upload_error_handler.rb +32 -0
- data/lib/uploadcare/entity/decorator/paginator.rb +79 -0
- data/lib/uploadcare/entity/document_converter.rb +26 -0
- data/lib/uploadcare/entity/entity.rb +18 -0
- data/lib/uploadcare/entity/file.rb +81 -0
- data/lib/uploadcare/entity/file_list.rb +31 -0
- data/lib/uploadcare/entity/group.rb +40 -0
- data/lib/uploadcare/entity/group_list.rb +24 -0
- data/lib/uploadcare/entity/project.rb +13 -0
- data/lib/uploadcare/entity/uploader.rb +75 -0
- data/lib/uploadcare/entity/video_converter.rb +26 -0
- data/lib/uploadcare/entity/webhook.rb +14 -0
- data/lib/uploadcare/exception/conversion_error.rb +8 -0
- data/lib/uploadcare/exception/request_error.rb +9 -0
- data/lib/uploadcare/exception/throttle_error.rb +16 -0
- data/lib/uploadcare/param/authentication_header.rb +25 -0
- data/lib/uploadcare/param/conversion/document/processing_job_url_builder.rb +39 -0
- data/lib/uploadcare/param/conversion/video/processing_job_url_builder.rb +64 -0
- data/lib/uploadcare/param/param.rb +10 -0
- data/lib/uploadcare/param/secure_auth_header.rb +37 -0
- data/lib/uploadcare/param/simple_auth_header.rb +14 -0
- data/lib/uploadcare/param/upload/signature_generator.rb +24 -0
- data/lib/uploadcare/param/upload/upload_params_generator.rb +23 -0
- data/lib/uploadcare/param/user_agent.rb +21 -0
- data/lib/uploadcare/ruby/version.rb +5 -0
- data/uploadcare-ruby.gemspec +50 -37
- metadata +109 -115
- data/.travis.yml +0 -26
- data/UPGRADE_NOTES.md +0 -36
- data/lib/uploadcare/api.rb +0 -26
- data/lib/uploadcare/api/file_api.rb +0 -7
- data/lib/uploadcare/api/file_list_api.rb +0 -19
- data/lib/uploadcare/api/file_storage_api.rb +0 -34
- data/lib/uploadcare/api/group_api.rb +0 -38
- data/lib/uploadcare/api/group_list_api.rb +0 -17
- data/lib/uploadcare/api/project_api.rb +0 -9
- data/lib/uploadcare/api/raw_api.rb +0 -38
- data/lib/uploadcare/api/uploading_api.rb +0 -71
- data/lib/uploadcare/api/uploading_api/upload_params.rb +0 -72
- data/lib/uploadcare/api/validators/file_list_options_validator.rb +0 -73
- data/lib/uploadcare/api/validators/group_list_options_validator.rb +0 -49
- data/lib/uploadcare/errors/errors.rb +0 -64
- data/lib/uploadcare/resources/file.rb +0 -164
- data/lib/uploadcare/resources/file_list.rb +0 -14
- data/lib/uploadcare/resources/group.rb +0 -115
- data/lib/uploadcare/resources/group_list.rb +0 -14
- data/lib/uploadcare/resources/project.rb +0 -13
- data/lib/uploadcare/resources/resource_list.rb +0 -83
- data/lib/uploadcare/rest/auth/auth.rb +0 -31
- data/lib/uploadcare/rest/auth/secure.rb +0 -43
- data/lib/uploadcare/rest/auth/simple.rb +0 -16
- data/lib/uploadcare/rest/connections/api_connection.rb +0 -53
- data/lib/uploadcare/rest/connections/upload_connection.rb +0 -22
- data/lib/uploadcare/rest/middlewares/auth_middleware.rb +0 -24
- data/lib/uploadcare/rest/middlewares/parse_json_middleware.rb +0 -33
- data/lib/uploadcare/rest/middlewares/raise_error_middleware.rb +0 -21
- data/lib/uploadcare/utils/parser.rb +0 -71
- data/lib/uploadcare/utils/user_agent.rb +0 -44
- data/lib/uploadcare/version.rb +0 -3
- data/spec/api/file_list_api_spec.rb +0 -95
- data/spec/api/file_storage_api_spec.rb +0 -88
- data/spec/api/group_list_api_spec.rb +0 -59
- data/spec/api/raw_api_spec.rb +0 -25
- data/spec/api/uploading_api/upload_params_spec.rb +0 -99
- data/spec/api/uploading_api_spec.rb +0 -59
- data/spec/resources/file_list_spec.rb +0 -25
- data/spec/resources/file_spec.rb +0 -223
- data/spec/resources/group_list_spec.rb +0 -25
- data/spec/resources/group_spec.rb +0 -101
- data/spec/resources/operations_spec.rb +0 -59
- data/spec/resources/project_spec.rb +0 -21
- data/spec/rest/api_connection_spec.rb +0 -68
- data/spec/rest/auth/secure_spec.rb +0 -66
- data/spec/rest/auth/simple_spec.rb +0 -31
- data/spec/rest/errors_spec.rb +0 -75
- data/spec/rest/upload_connection_spec.rb +0 -19
- data/spec/shared/resource_list.rb +0 -188
- data/spec/spec_helper.rb +0 -54
- data/spec/uploadcare_spec.rb +0 -16
- data/spec/utils/parser_spec.rb +0 -85
- data/spec/utils/user_agent_spec.rb +0 -46
- data/spec/view.png +0 -0
- data/spec/view2.jpg +0 -0
data/.travis.yml
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
# currently supported rubies: https://www.ruby-lang.org/en/downloads/branches/
|
2
|
-
|
3
|
-
language: ruby
|
4
|
-
|
5
|
-
rvm:
|
6
|
-
- 1.9.3
|
7
|
-
- 2.2
|
8
|
-
- 2.3
|
9
|
-
- 2.4
|
10
|
-
- ruby-head
|
11
|
-
|
12
|
-
jobs:
|
13
|
-
include:
|
14
|
-
- stage: style checks
|
15
|
-
rvm: 2.4
|
16
|
-
before_install:
|
17
|
-
- gem install rubocop
|
18
|
-
script:
|
19
|
-
- rubocop
|
20
|
-
|
21
|
-
allow_failures:
|
22
|
-
- stage: style checks
|
23
|
-
- rvm: ruby-head
|
24
|
-
|
25
|
-
before_install:
|
26
|
-
- gem update bundler
|
data/UPGRADE_NOTES.md
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
# Upgrade notes
|
2
|
-
|
3
|
-
## v1.* -> v2.*
|
4
|
-
|
5
|
-
In 2.* release we've moved to [REST API v0.5][uploadcare-changelog-rest-api-v05] which introduces a new pagination for `/files/` and `/groups/` endpoints, so `Uploadcare::Api::FileList` and `Uploadcare::Api::GroupList` were completely reimplemented.
|
6
|
-
|
7
|
-
Previously, the file/group list API was:
|
8
|
-
|
9
|
-
```ruby
|
10
|
-
# creating
|
11
|
-
list = api.file_list # => #<Uploadcare::Api::FileList page=1 ...>
|
12
|
-
|
13
|
-
# accessing files/groups
|
14
|
-
list.results # => [#<Uploadcare::Api::File>, ...]
|
15
|
-
|
16
|
-
# pagination
|
17
|
-
list.next_page # => #<Uploadcare::Api::FileList page=2 ...>
|
18
|
-
list.previous_page # => #<Uploadcare::Api::FileList page=1 ...>
|
19
|
-
list.go_to 5 # => #<Uploadcare::Api::FileList page=5 ...>
|
20
|
-
|
21
|
-
# metadata
|
22
|
-
list.pages # => 15
|
23
|
-
list.page # => 5
|
24
|
-
list.total # => 308 (files in a project)
|
25
|
-
```
|
26
|
-
|
27
|
-
It **won't work anymore**. For the details on the new file/group lists interface see [readme][readme]
|
28
|
-
|
29
|
-
The core features of the new file/group list API are:
|
30
|
-
|
31
|
-
- transparent pagination via enumerable interface
|
32
|
-
- loading objects on demand
|
33
|
-
- ordering, filtering and slicing
|
34
|
-
|
35
|
-
[uploadcare-changelog-rest-api-v05]: https://uploadcare.com/changelog/tag/rest-api#rest-api-version-05
|
36
|
-
[readme]: https://github.com/uploadcare/uploadcare-ruby#file-lists
|
data/lib/uploadcare/api.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
require 'ostruct'
|
3
|
-
Dir[File.dirname(__FILE__) + '/utils/*.rb'].each {|file| require file }
|
4
|
-
Dir[File.dirname(__FILE__) + '/errors/*.rb'].each {|file| require file }
|
5
|
-
Dir[File.dirname(__FILE__) + '/rest/middlewares/*.rb'].each {|file| require file }
|
6
|
-
Dir[File.dirname(__FILE__) + '/rest/connections/*.rb'].each {|file| require file }
|
7
|
-
Dir[File.dirname(__FILE__) + '/rest/auth/*.rb'].sort.each {|file| require file }
|
8
|
-
Dir[File.dirname(__FILE__) + '/api/validators/*.rb'].each {|file| require file }
|
9
|
-
Dir[File.dirname(__FILE__) + '/api/*.rb'].each {|file| require file }
|
10
|
-
Dir[File.dirname(__FILE__) + '/resources/*.rb'].each {|file| require file }
|
11
|
-
|
12
|
-
|
13
|
-
module Uploadcare
|
14
|
-
class Api
|
15
|
-
attr_reader :options
|
16
|
-
|
17
|
-
include Uploadcare::RawApi
|
18
|
-
include Uploadcare::UploadingApi
|
19
|
-
include Uploadcare::FileApi
|
20
|
-
include Uploadcare::ProjectApi
|
21
|
-
include Uploadcare::FileListApi
|
22
|
-
include Uploadcare::GroupApi
|
23
|
-
include Uploadcare::GroupListApi
|
24
|
-
include Uploadcare::FileStorageApi
|
25
|
-
end
|
26
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Uploadcare
|
2
|
-
module FileListApi
|
3
|
-
# Available options:
|
4
|
-
#
|
5
|
-
# limit -- a number of objects retrieved per request. Default: 100
|
6
|
-
# ordering -- sorting order of files in a list. Default: datetime_uploaded
|
7
|
-
# from -- a starting point for filtering files.
|
8
|
-
# stored -- true to include only stored files, false to exclude.
|
9
|
-
# removed -- true to include only removed files, false to exclude. Default: false
|
10
|
-
#
|
11
|
-
# Documentation: http://uploadcare.com/documentation/rest/#file-files
|
12
|
-
def file_list options={}
|
13
|
-
Validators::FileListOptionsValidator.new(options).validate
|
14
|
-
|
15
|
-
data = get '/files/', options
|
16
|
-
Uploadcare::Api::FileList.new self, data, options
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Uploadcare
|
2
|
-
module FileStorageApi
|
3
|
-
MAX_BATCH_SIZE = 100
|
4
|
-
UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
|
5
|
-
|
6
|
-
def store_files(objects)
|
7
|
-
if objects.size > MAX_BATCH_SIZE
|
8
|
-
raise ArgumentError, "Up to #{MAX_BATCH_SIZE} files are supported per request, #{objects.size} given"
|
9
|
-
end
|
10
|
-
|
11
|
-
put "/files/storage/", to_uuids(objects)
|
12
|
-
end
|
13
|
-
|
14
|
-
def delete_files(objects)
|
15
|
-
if objects.size > MAX_BATCH_SIZE
|
16
|
-
raise ArgumentError, "Up to #{MAX_BATCH_SIZE} files are supported per request, #{objects.size} given"
|
17
|
-
end
|
18
|
-
|
19
|
-
delete "/files/storage/", to_uuids(objects)
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def to_uuids(objects)
|
25
|
-
objects.map do |object|
|
26
|
-
case object
|
27
|
-
when Uploadcare::Api::File then object.uuid
|
28
|
-
when UUID_REGEX then object
|
29
|
-
else raise(ArgumentError, "Unable to convert object to uuid: #{object}")
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
module Uploadcare
|
2
|
-
module GroupApi
|
3
|
-
|
4
|
-
def group uuid_or_cdn_url
|
5
|
-
group = Uploadcare::Api::Group.new self, uuid_or_cdn_url
|
6
|
-
end
|
7
|
-
|
8
|
-
|
9
|
-
def create_group ary
|
10
|
-
unless ary.kind_of?(Array)
|
11
|
-
raise ArgumentError.new "You should send and array of files or valid UUIDs"
|
12
|
-
else
|
13
|
-
if ary.select {|f| !!f.kind_of?(Uploadcare::Api::File) }.any?
|
14
|
-
files = Hash.new
|
15
|
-
ary.each_with_index do |file, i|
|
16
|
-
files["files[#{i}]"] = file.uuid
|
17
|
-
end
|
18
|
-
elsif ary.select {|f| !!f.kind_of?(String) }.any?
|
19
|
-
files = Hash.new
|
20
|
-
ary.each_with_index do |uuid, i|
|
21
|
-
files["files[#{i}]"] = uuid
|
22
|
-
end
|
23
|
-
else
|
24
|
-
raise ArgumentError.new "You should send and array of files or valid UUIDs"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
data = {
|
30
|
-
pub_key: @options[:public_key],
|
31
|
-
}
|
32
|
-
|
33
|
-
data.merge! files
|
34
|
-
post = @upload_connection.send :post, "/group/", data
|
35
|
-
group = Uploadcare::Api::Group.new self, post.body["id"], post.body
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module Uploadcare
|
2
|
-
module GroupListApi
|
3
|
-
# Available options:
|
4
|
-
#
|
5
|
-
# limit -- a number of objects retrieved per request. Default: 100
|
6
|
-
# ordering -- sorting order of groups in a list. Default: datetime_creataed
|
7
|
-
# from -- a starting point for filtering groups.
|
8
|
-
#
|
9
|
-
# Documentation: http://uploadcare.com/documentation/rest/#group-groups
|
10
|
-
def group_list options={}
|
11
|
-
Validators::GroupListOptionsValidator.new(options).validate
|
12
|
-
|
13
|
-
data = get '/groups/', options
|
14
|
-
list = Uploadcare::Api::GroupList.new self, data, options
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
|
3
|
-
module Uploadcare
|
4
|
-
module RawApi
|
5
|
-
def initialize(options = {})
|
6
|
-
@options = Uploadcare::default_settings.merge(options)
|
7
|
-
@api_connection = Uploadcare::Connections::ApiConnection.new(@options)
|
8
|
-
@upload_connection = Uploadcare::Connections::UploadConnection.new(@options)
|
9
|
-
end
|
10
|
-
|
11
|
-
# basic request method
|
12
|
-
def request(method = :get, path = '/files/', params = {})
|
13
|
-
response = @api_connection.send method, path, params
|
14
|
-
response.body
|
15
|
-
end
|
16
|
-
alias_method :api_request, :request
|
17
|
-
|
18
|
-
# request with GET verb
|
19
|
-
def get(path = '/files/', params = {})
|
20
|
-
request :get, path, params
|
21
|
-
end
|
22
|
-
|
23
|
-
# request with POST verb
|
24
|
-
def post(path = '/files/', params = {})
|
25
|
-
request :post, path, params
|
26
|
-
end
|
27
|
-
|
28
|
-
# request with PUT verb
|
29
|
-
def put(path = '/files/', params = {})
|
30
|
-
request :put, path, params
|
31
|
-
end
|
32
|
-
|
33
|
-
# request with DELETE verb
|
34
|
-
def delete(path = '/files/', params = {})
|
35
|
-
request :delete, path, params
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
require_relative 'uploading_api/upload_params'
|
2
|
-
|
3
|
-
module Uploadcare
|
4
|
-
module UploadingApi
|
5
|
-
# intelegent guess for file or URL uploading
|
6
|
-
def upload(object, options = {})
|
7
|
-
case object
|
8
|
-
when File then upload_file(object, options)
|
9
|
-
when Array then upload_files(object, options)
|
10
|
-
# if object is a string, try to upload it as an URL
|
11
|
-
when String then upload_url(object, options)
|
12
|
-
else
|
13
|
-
raise ArgumentError, "Expected `object` to be an Uploadcare::Api::File, "\
|
14
|
-
"an Array or a valid URL string, received: `#{object}`"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# Upload multiple files
|
19
|
-
def upload_files(files, options = {})
|
20
|
-
data = upload_params(options).for_file_upload(files)
|
21
|
-
|
22
|
-
response = @upload_connection.post('/base/', data)
|
23
|
-
|
24
|
-
response.body.values.map! { |f| Uploadcare::Api::File.new(self, f) }
|
25
|
-
end
|
26
|
-
|
27
|
-
# Upload single file
|
28
|
-
def upload_file(file, options = {})
|
29
|
-
upload_files([file], options).first
|
30
|
-
end
|
31
|
-
alias_method :create_file, :upload_file
|
32
|
-
|
33
|
-
# Upload from an URL
|
34
|
-
def upload_url(url, options = {})
|
35
|
-
params = upload_params(options).for_url_upload(url)
|
36
|
-
token = request_file_upload(params)
|
37
|
-
|
38
|
-
upload_status = poll_upload_result(token)
|
39
|
-
if upload_status['status'] == 'error'
|
40
|
-
raise ArgumentError.new(upload_status['error'])
|
41
|
-
end
|
42
|
-
|
43
|
-
Uploadcare::Api::File.new(self, upload_status['file_id'])
|
44
|
-
end
|
45
|
-
alias_method :upload_from_url, :upload_url
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def get_status_response(token)
|
50
|
-
response = @upload_connection.post('/from_url/status/', {token: token})
|
51
|
-
response.body
|
52
|
-
end
|
53
|
-
|
54
|
-
def request_file_upload(upload_params)
|
55
|
-
response = @upload_connection.post('/from_url/', upload_params)
|
56
|
-
token = response.body['token']
|
57
|
-
end
|
58
|
-
|
59
|
-
def poll_upload_result(token)
|
60
|
-
while true
|
61
|
-
response = get_status_response(token)
|
62
|
-
break(response) if ['success', 'error'].include?(response['status'])
|
63
|
-
sleep 0.5
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def upload_params(request_options)
|
68
|
-
UploadParams.new(@options, request_options)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
require 'uri'
|
2
|
-
require 'mime/types'
|
3
|
-
|
4
|
-
module Uploadcare
|
5
|
-
module UploadingApi
|
6
|
-
class UploadParams
|
7
|
-
def initialize(global_options, request_options)
|
8
|
-
@global_options = global_options
|
9
|
-
@request_options = request_options
|
10
|
-
end
|
11
|
-
|
12
|
-
def for_url_upload(url)
|
13
|
-
{
|
14
|
-
source_url: parse_url(url),
|
15
|
-
pub_key: public_key,
|
16
|
-
store: store
|
17
|
-
}.reject { |k, v| v.nil? }
|
18
|
-
end
|
19
|
-
|
20
|
-
def for_file_upload(files)
|
21
|
-
{
|
22
|
-
UPLOADCARE_PUB_KEY: public_key,
|
23
|
-
UPLOADCARE_STORE: store
|
24
|
-
}.reject { |k, v| v.nil? }.merge(file_params(files))
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
attr_reader :global_options, :request_options
|
30
|
-
|
31
|
-
def public_key
|
32
|
-
global_options[:public_key]
|
33
|
-
end
|
34
|
-
|
35
|
-
def store
|
36
|
-
mapping = { true => 1, false => 0, auto: 'auto' }
|
37
|
-
|
38
|
-
global_value = global_options[:autostore]
|
39
|
-
per_request_value = request_options[:store]
|
40
|
-
|
41
|
-
mapping[per_request_value] || mapping[global_value]
|
42
|
-
end
|
43
|
-
|
44
|
-
def file_params(files)
|
45
|
-
Hash[files.map.with_index { |file, i| ["file[#{i}]", build_upload_io(file)] }]
|
46
|
-
end
|
47
|
-
|
48
|
-
def parse_url(url)
|
49
|
-
uri = URI.parse(url)
|
50
|
-
|
51
|
-
unless uri.is_a?(URI::HTTP) # will also be true for https
|
52
|
-
raise ArgumentError, 'invalid url was given'
|
53
|
-
end
|
54
|
-
|
55
|
-
uri
|
56
|
-
end
|
57
|
-
|
58
|
-
def build_upload_io(file)
|
59
|
-
unless file.is_a?(File)
|
60
|
-
raise ArgumentError, "expected File object, #{file} given"
|
61
|
-
end
|
62
|
-
|
63
|
-
Faraday::UploadIO.new file.path, extract_mime_type(file)
|
64
|
-
end
|
65
|
-
|
66
|
-
def extract_mime_type file
|
67
|
-
types = MIME::Types.of(file.path)
|
68
|
-
types[0].content_type
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
module Uploadcare
|
2
|
-
module Validators
|
3
|
-
|
4
|
-
class FileListOptionsValidator
|
5
|
-
SUPPORTED_KEYS = [:from, :ordering, :limit, :stored, :removed]
|
6
|
-
|
7
|
-
def initialize(options)
|
8
|
-
@options = options
|
9
|
-
end
|
10
|
-
|
11
|
-
def validate
|
12
|
-
check_for_unsupported_keys(@options)
|
13
|
-
|
14
|
-
validate_limit(@options[:limit])
|
15
|
-
validate_stored(@options[:stored])
|
16
|
-
validate_removed(@options[:removed])
|
17
|
-
validate_ordering_and_from(@options[:ordering], @options[:from])
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def check_for_unsupported_keys(options)
|
23
|
-
unsupported_keys = options.keys.reject{|k,_| SUPPORTED_KEYS.include?(k)}
|
24
|
-
error("Unknown options: #{unsupported_keys}") if unsupported_keys.any?
|
25
|
-
end
|
26
|
-
|
27
|
-
def validate_ordering_and_from(ordering, from)
|
28
|
-
case ordering
|
29
|
-
when nil, /^-?datetime_uploaded$/
|
30
|
-
validate_from_as_date(from)
|
31
|
-
when /^-?size$/
|
32
|
-
validate_from_as_size(from)
|
33
|
-
else
|
34
|
-
error("Unknown value for :ordering option: #{ordering.inspect}")
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def validate_from_as_date(from)
|
39
|
-
return if from.nil? || from.to_s =~ /^\d{4}-\d{2}-\d{2}T\d{2}.*/
|
40
|
-
error(":from value should be a DateTime or an iso8601 string when "\
|
41
|
-
"ordering is `datetime_uploaded` or `-datetime_uploaded`, "\
|
42
|
-
"#{from.inspect} given")
|
43
|
-
end
|
44
|
-
|
45
|
-
def validate_from_as_size(from)
|
46
|
-
return if from.nil? || (from.is_a?(Integer) && from >= 0)
|
47
|
-
error(":from value should be a positive integer when ordering is "\
|
48
|
-
"`size` or `-size`, #{from.inspect} given")
|
49
|
-
end
|
50
|
-
|
51
|
-
def validate_limit(limit)
|
52
|
-
return if limit.nil? || (limit.is_a?(Integer) && (1..1000).include?(limit))
|
53
|
-
error(":limit should be a positive integer from 1 to 1000, "\
|
54
|
-
"#{limit.inspect} given")
|
55
|
-
end
|
56
|
-
|
57
|
-
def validate_stored(stored)
|
58
|
-
return if [nil, true, false].include?(stored)
|
59
|
-
error(":stored can be true or false, #{stored.inspect} given")
|
60
|
-
end
|
61
|
-
|
62
|
-
def validate_removed(removed)
|
63
|
-
return if [nil, true, false].include?(removed)
|
64
|
-
error(":removed can be true or false, #{removed.inspect} given")
|
65
|
-
end
|
66
|
-
|
67
|
-
def error(message)
|
68
|
-
raise ArgumentError, message
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
end
|