uploadcare-ruby 2.1.2 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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,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
@@ -1,49 +0,0 @@
1
- module Uploadcare
2
- module Validators
3
-
4
- class GroupListOptionsValidator
5
- SUPPORTED_KEYS = [:from, :ordering, :limit]
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_ordering(@options[:ordering])
16
- validate_from(@options[:from])
17
- end
18
-
19
- private
20
-
21
- def check_for_unsupported_keys(options)
22
- unsupported_keys = options.keys.reject{|k,_| SUPPORTED_KEYS.include?(k)}
23
- error("Unknown options: #{unsupported_keys}") if unsupported_keys.any?
24
- end
25
-
26
- def validate_ordering(ordering)
27
- return if !ordering || ordering =~ /^-?datetime_created$/
28
- error("Unknown value for :ordering option: #{ordering.inspect}")
29
- end
30
-
31
- def validate_from(from)
32
- return if from.nil? || from.to_s =~ /^\d{4}-\d{2}-\d{2}T\d{2}.*/
33
- error(":from value should be a DateTime or an iso8601 string, "\
34
- "#{from.inspect} given")
35
- end
36
-
37
- def validate_limit(limit)
38
- return if limit.nil? || (limit.is_a?(Integer) && (1..1000).include?(limit))
39
- error(":limit should be a positive integer from 1 to 1000, "\
40
- "#{limit.inspect} given")
41
- end
42
-
43
- def error(message)
44
- raise ArgumentError, message
45
- end
46
- end
47
-
48
- end
49
- end
@@ -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,64 +0,0 @@
1
- module Uploadcare
2
- class Error < ::StandardError
3
-
4
- def self.define_error code, klass, message
5
- class_eval <<-EOD
6
- class #{klass} < self
7
- def initialize( message = nil )
8
- super( message || "HTTP #{code} - #{message}" )
9
- end
10
- end
11
- EOD
12
- end
13
-
14
- def self.errors
15
- @errors ||= {
16
- 400 => Uploadcare::Error::RequestError::BadRequest,
17
- 401 => Uploadcare::Error::RequestError::Unauthorized,
18
- 403 => Uploadcare::Error::RequestError::Forbidden,
19
- 404 => Uploadcare::Error::RequestError::NotFound,
20
- 406 => Uploadcare::Error::RequestError::NotAcceptable,
21
- 408 => Uploadcare::Error::RequestError::RequestTimeout,
22
- 422 => Uploadcare::Error::RequestError::UnprocessableEntity,
23
- 429 => Uploadcare::Error::RequestError::TooManyRequests,
24
- 500 => Uploadcare::Error::ServerError::InternalServerError,
25
- 502 => Uploadcare::Error::ServerError::BadGateway,
26
- 503 => Uploadcare::Error::ServerError::ServiceUnavailable,
27
- 504 => Uploadcare::Error::ServerError::GatewayTimeout,
28
- }
29
- end
30
-
31
-
32
- # Overall service error so you could escape it no matter what code is return
33
-
34
- # all 4xx error
35
- class RequestError < self;
36
- def initialize( message = nil )
37
- super( message || "HTTP 4xx - a request error occured." )
38
- end
39
-
40
- define_error 400, "BadRequest", "the request cannot be fulfilled due to bad syntax."
41
- define_error 401, "Unauthorized", "authentication is required and has failed or has not yet been provided."
42
- define_error 403, "Forbidden", "the request was a valid request, but the server is refusing to respond to it."
43
- define_error 404, "NotFound", "the requested resource could not be found."
44
- define_error 406, "NotAcceptable", "the requested resource is only capable of generating content"
45
- define_error 408, "RequestTimeout", "the server timed out waiting for the request."
46
- define_error 422, "UnprocessableEntity", "the request was well-formed but was unable to be followed due to semantic errors."
47
- define_error 429, "TooManyRequests", "too many requests in a given amount of time."
48
- end
49
-
50
- # all 5xx error
51
- class ServerError < self;
52
- def initialize( message = nil )
53
- super( message || "HTTP 5xx - a server error occured." )
54
- end
55
-
56
- define_error 500, "InternalServerError", "some error occured on server."
57
- define_error 502, "BadGateway", "received an invalid response from the upstream server."
58
- define_error 503, "ServiceUnavailable", "the server is currently unavailable."
59
- define_error 504, "GatewayTimeout", "the server did not receive a timely response from the upstream server."
60
- end
61
-
62
- # specific error
63
- end
64
- end
@@ -1,164 +0,0 @@
1
- require 'ostruct'
2
-
3
- module Uploadcare
4
- class Api
5
- class File < OpenStruct
6
- def initialize api, uuid_or_cdn_url, data=nil
7
- result = Uploadcare::Parser.parse_file_string uuid_or_cdn_url
8
-
9
- file = {uuid: result.uuid, operations: result.operations}
10
-
11
- @api = api
12
-
13
- super file
14
-
15
- set_data(data) if data
16
- end
17
-
18
- def cdn_url add_operations=false
19
- if add_operations
20
- cdn_url_with_operations
21
- else
22
- cdn_url_without_operations
23
- end
24
- end
25
- alias_method :public_url, :cdn_url
26
-
27
-
28
- def cdn_url_without_operations
29
- @api.options[:static_url_base] + "/#{@table[:uuid]}/"
30
- end
31
- alias_method :public_url_without_operations, :cdn_url_without_operations
32
-
33
-
34
- def cdn_url_with_operations
35
- url = cdn_url_without_operations
36
- unless operations.empty?
37
- ops = operations.join("/-/")
38
- url = url + "-/#{ops}/"
39
- end
40
- url
41
- end
42
- alias_method :public_url_with_operations, :cdn_url_with_operations
43
-
44
-
45
- def load_data
46
- load_data! unless is_loaded?
47
- self
48
- end
49
- alias_method :load, :load_data
50
-
51
- def load_data!
52
- data = @api.get "/files/#{uuid}/"
53
- set_data(data)
54
-
55
- self
56
- end
57
- alias_method :load!, :load_data!
58
-
59
- def is_loaded?
60
- !send(:datetime_uploaded).nil?
61
- end
62
- alias_method :loaded?, :is_loaded?
63
-
64
-
65
- def store
66
- data = @api.put "/files/#{uuid}/storage/"
67
- set_data data
68
- self
69
- end
70
-
71
- # nil is returning if there is no way to say for sure
72
- def is_stored?
73
- return nil unless is_loaded?
74
- !send(:datetime_stored).nil?
75
- end
76
- alias_method :stored?, :is_stored?
77
-
78
-
79
- def delete
80
- data = @api.delete "/files/#{uuid}/storage/"
81
- set_data data
82
- self
83
- end
84
-
85
- # nil is returning if there is no way to say for sure
86
- def is_deleted?
87
- return nil unless is_loaded?
88
- !send(:datetime_removed).nil?
89
- end
90
- alias_method :deleted?, :is_deleted?
91
- alias_method :removed?, :is_deleted?
92
- alias_method :is_removed?, :is_deleted?
93
-
94
-
95
- # copy file to target location
96
- # note what file copied with operations
97
- def copy with_operations=true, target=nil
98
- warn "[DEPRECATION] `copy` is deprecated and will be removed in "\
99
- "version 3.0. Please use `internal_copy` or `external_copy` instead."
100
- data = Hash.new
101
- data[:target] = target if target
102
- data[:source] = self.cdn_url_with_operations if with_operations
103
- data[:source] = self.cdn_url_without_operations unless with_operations
104
-
105
- @api.post "/files/", data
106
- end
107
-
108
- # Create a copy of the file in a default storage
109
- def internal_copy(options={})
110
- data = {
111
- source: cdn_url(!options.fetch(:strip_operations){ false }),
112
- store: options.fetch(:store){ nil }
113
- }.reject{|_,v| v.nil?}
114
-
115
- @api.post "/files/", data
116
- end
117
-
118
- # Copy file to a custom storage
119
- def external_copy(target, options={})
120
- data = {
121
- source: cdn_url(!options.fetch(:strip_operations){ false }),
122
- target: target,
123
- pattern: options.fetch(:pattern){ nil },
124
- make_public: options.fetch(:make_public){ nil },
125
- }.reject{|_,v| v.nil?}
126
-
127
- @api.post "/files/", data
128
- end
129
-
130
-
131
- # Datetime methods
132
- # practicly try and parse the string to date objects
133
- ["original", "uploaded", "stored", "removed"].each do |dt|
134
- define_method "datetime_#{dt}" do
135
- date = @table["datetime_#{dt}".to_sym]
136
- if date.is_a?(String)
137
- begin
138
- parsed = DateTime.parse(date)
139
- self.send("datetime_#{dt}=", parsed)
140
- parsed
141
- rescue Exception => e
142
- date
143
- end
144
- else
145
- date
146
- end
147
- end
148
- end
149
- alias_method :datetime_deleted, :datetime_removed
150
-
151
-
152
- private
153
- def set_data data
154
- if data.respond_to? :each
155
- data.each do |key, value|
156
- self.send "#{key}=", value
157
- end
158
- else
159
- self.data = data
160
- end
161
- end
162
- end
163
- end
164
- end
@@ -1,14 +0,0 @@
1
- require 'ostruct'
2
- require_relative 'resource_list'
3
-
4
- module Uploadcare
5
- class Api
6
- class FileList < ResourceList
7
- private
8
-
9
- def to_resource(api, file_data)
10
- Uploadcare::Api::File.new(api, file_data['uuid'], file_data)
11
- end
12
- end
13
- end
14
- end
@@ -1,115 +0,0 @@
1
- require 'ostruct'
2
-
3
- module Uploadcare
4
- class Api
5
- class Group < OpenStruct
6
- def initialize api, uuid_or_cdn_url, data=nil
7
- result = Uploadcare::Parser.parse_group_string(uuid_or_cdn_url)
8
-
9
- @api = api
10
- group = {uuid: result.uuid, files_count: result.count}
11
- super group
12
-
13
- # if data is suplide - just pass it to builder.
14
- set_data(data) if data
15
- end
16
-
17
- def cdn_url
18
- @api.options[:static_url_base] + "/#{uuid}/"
19
- end
20
-
21
- def file_cdn_url index=0
22
- raise ArgumentError.new "The index was given is greater than files count in group." if index + 1 > files_count
23
- cdn_url + "nth/#{index}/"
24
- end
25
-
26
- # Loading logic
27
- def is_loaded?
28
- !send(:files).nil?
29
- end
30
- alias_method :loaded?, :is_loaded?
31
-
32
- def load_data
33
- unless is_loaded?
34
- load_data!
35
- end
36
- self
37
- end
38
- alias_method :load, :load_data
39
-
40
- def load_data!
41
- data = @api.get "/groups/#{uuid}/"
42
- set_data data
43
-
44
- self
45
- end
46
- alias_method :load!, :load_data!
47
-
48
- # Store group (and all files in group)
49
- def store
50
- unless is_stored?
51
- store!
52
- end
53
- self
54
- end
55
-
56
- def store!
57
- data = @api.put "/groups/#{uuid}/storage/"
58
- set_data(data)
59
- self
60
- end
61
-
62
- def is_stored?
63
- return nil unless is_loaded?
64
- !send(:datetime_stored).nil?
65
- end
66
- alias_method :stored?, :is_stored?
67
-
68
-
69
- ["created", "stored"].each do |dt|
70
- define_method "datetime_#{dt}" do
71
- date = @table["datetime_#{dt}".to_sym]
72
- if date.is_a?(String)
73
- begin
74
- parsed = DateTime.parse(date)
75
- self.send("datetime_#{dt}=", parsed)
76
- parsed
77
- rescue Exception => e
78
- date
79
- end
80
- else
81
- date
82
- end
83
- end
84
- end
85
-
86
-
87
- private
88
- def set_data data
89
- data = map_files(data) unless data["files"].nil?
90
-
91
- if data.respond_to? (:each)
92
- data.each do |k, v|
93
- self.send "#{k}=", v
94
- end
95
- end
96
-
97
- @is_loaded = true
98
- end
99
-
100
- # map files (hashes basicly) to
101
- # actual File objects
102
- def map_files data
103
- data["files"].map! do |file|
104
- unless file.nil?
105
- Uploadcare::Api::File.new(@api, file["uuid"], file)
106
- else
107
- file
108
- end
109
- end
110
-
111
- data
112
- end
113
- end
114
- end
115
- end
@@ -1,14 +0,0 @@
1
- require 'ostruct'
2
- require_relative 'resource_list'
3
-
4
- module Uploadcare
5
- class Api
6
- class GroupList < ResourceList
7
- private
8
-
9
- def to_resource(api, group_data)
10
- Uploadcare::Api::Group.new api, group_data["id"], group_data
11
- end
12
- end
13
- end
14
- end
@@ -1,13 +0,0 @@
1
- require 'ostruct'
2
-
3
- module Uploadcare
4
- class Api
5
- class Project < OpenStruct
6
- def initialize api
7
- @api = api
8
- data = @api.get "/project/"
9
- super data
10
- end
11
- end
12
- end
13
- end
@@ -1,83 +0,0 @@
1
- require 'ostruct'
2
-
3
- module Uploadcare
4
- class Api
5
- class ResourceList
6
- include Enumerable
7
-
8
- extend Forwardable
9
- def_delegator :@data, :meta
10
- def_delegator :@data, :objects
11
-
12
- attr_reader :options
13
-
14
- def initialize(api, data, options)
15
- @api = api
16
- @data = build_data(data)
17
- @options = options.dup.freeze
18
- end
19
-
20
- def [](index)
21
- first(index + 1).last
22
- end
23
-
24
- def each
25
- return enum_for(:each) unless block_given?
26
-
27
- resource_enumerator.each { |object| yield object }
28
-
29
- self
30
- end
31
-
32
- def total
33
- meta['total']
34
- end
35
-
36
- def loaded
37
- objects.size
38
- end
39
-
40
- def fully_loaded?
41
- meta['next'].nil?
42
- end
43
-
44
- private
45
-
46
- attr_reader :api
47
-
48
- def build_data(data_hash)
49
- OpenStruct.new(
50
- meta: data_hash.reject{|k, _| k == 'results'}.freeze,
51
- objects: data_hash['results'].map{|object| to_resource(api, object)}
52
- )
53
- end
54
-
55
- def get_next_page
56
- return nil if fully_loaded?
57
-
58
- next_page = build_data(api.get(@data.meta['next']))
59
-
60
- @data = OpenStruct.new(
61
- meta: next_page.meta,
62
- objects: objects + next_page.objects
63
- )
64
-
65
- next_page
66
- end
67
-
68
- def to_resource(*args)
69
- raise NotImplementedError, 'You must define this method in a child class'
70
- end
71
-
72
- def resource_enumerator
73
- Enumerator.new do |yielder|
74
- objects.each { |obj| yielder << obj }
75
-
76
- while next_page = get_next_page do
77
- next_page.objects.each { |obj| yielder << obj }
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end