uploadcare-rails 1.2.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +5 -5
  2. data/Rakefile +5 -16
  3. data/lib/generators/templates/uploadcare_config_template.erb +138 -0
  4. data/lib/generators/uploadcare_config_generator.rb +7 -4
  5. data/lib/uploadcare/errors/type_error.rb +7 -0
  6. data/lib/uploadcare/rails/action_view/uploadcare_include_tags.rb +48 -0
  7. data/lib/uploadcare/rails/action_view/uploadcare_uploader_tags.rb +57 -0
  8. data/lib/uploadcare/rails/active_record/mount_uploadcare_file.rb +62 -0
  9. data/lib/uploadcare/rails/active_record/mount_uploadcare_file_group.rb +58 -0
  10. data/lib/uploadcare/rails/api/rest/base.rb +14 -0
  11. data/lib/uploadcare/rails/api/rest/conversion_api.rb +42 -0
  12. data/lib/uploadcare/rails/api/rest/file_api.rb +69 -0
  13. data/lib/uploadcare/rails/api/rest/group_api.rb +48 -0
  14. data/lib/uploadcare/rails/api/rest/project_api.rb +27 -0
  15. data/lib/uploadcare/rails/api/rest/webhook_api.rb +45 -0
  16. data/lib/uploadcare/rails/api/upload/base.rb +13 -0
  17. data/lib/uploadcare/rails/api/upload/upload_api.rb +53 -0
  18. data/lib/uploadcare/rails/configuration.rb +57 -0
  19. data/lib/uploadcare/rails/engine.rb +13 -17
  20. data/lib/uploadcare/rails/jobs/delete_file_job.rb +14 -0
  21. data/lib/uploadcare/rails/jobs/store_file_job.rb +14 -0
  22. data/lib/uploadcare/rails/jobs/store_group_job.rb +14 -0
  23. data/lib/uploadcare/rails/objects/concerns/loadable.rb +46 -0
  24. data/lib/uploadcare/rails/objects/file.rb +40 -20
  25. data/lib/uploadcare/rails/objects/group.rb +51 -57
  26. data/lib/uploadcare/rails/services/files_count_extractor.rb +14 -0
  27. data/lib/uploadcare/rails/services/id_extractor.rb +15 -0
  28. data/lib/uploadcare/rails/transformations/image_transformations.rb +45 -0
  29. data/lib/uploadcare/rails/version.rb +3 -1
  30. data/lib/uploadcare/rails.rb +10 -0
  31. data/lib/uploadcare-rails.rb +27 -16
  32. data/spec/fixtures/kitten.jpeg +0 -0
  33. data/spec/fixtures/vcr_cassettes/conversion_api_convert_document.yml +62 -0
  34. data/spec/fixtures/vcr_cassettes/conversion_api_convert_document_with_error.yml +62 -0
  35. data/spec/fixtures/vcr_cassettes/conversion_api_convert_video.yml +62 -0
  36. data/spec/fixtures/vcr_cassettes/conversion_api_convert_video_with_error.yml +62 -0
  37. data/spec/fixtures/vcr_cassettes/conversion_api_get_document_conversion_status.yml +59 -0
  38. data/spec/fixtures/vcr_cassettes/conversion_api_get_video_conversion_status.yml +59 -0
  39. data/spec/fixtures/vcr_cassettes/file_api_copy_file.yml +60 -0
  40. data/spec/fixtures/vcr_cassettes/file_api_delete_file.yml +58 -0
  41. data/spec/fixtures/vcr_cassettes/file_api_delete_files.yml +58 -0
  42. data/spec/fixtures/vcr_cassettes/file_api_get_file.yml +58 -0
  43. data/spec/fixtures/vcr_cassettes/file_api_get_files.yml +61 -0
  44. data/spec/fixtures/vcr_cassettes/file_api_load_file.yml +57 -0
  45. data/spec/fixtures/vcr_cassettes/file_api_store_file.yml +58 -0
  46. data/spec/fixtures/vcr_cassettes/file_api_store_files.yml +57 -0
  47. data/spec/fixtures/vcr_cassettes/group_api_create_group.yml +57 -0
  48. data/spec/fixtures/vcr_cassettes/group_api_get_group.yml +52 -0
  49. data/spec/fixtures/vcr_cassettes/group_api_get_groups.yml +57 -0
  50. data/spec/fixtures/vcr_cassettes/group_api_store_group.yml +57 -0
  51. data/spec/fixtures/vcr_cassettes/project_api_get_project.yml +59 -0
  52. data/spec/fixtures/vcr_cassettes/upload_upload_many_files.yml +55 -0
  53. data/spec/fixtures/vcr_cassettes/upload_upload_one_file.yml +105 -0
  54. data/spec/fixtures/vcr_cassettes/upload_upload_one_file_from_cdn.yml +111 -0
  55. data/spec/fixtures/vcr_cassettes/webhook_api_create_webhook.yml +59 -0
  56. data/spec/fixtures/vcr_cassettes/webhook_api_delete_webhook.yml +57 -0
  57. data/spec/fixtures/vcr_cassettes/webhook_api_get_webhooks.yml +59 -0
  58. data/spec/fixtures/vcr_cassettes/webhook_api_update_webhook.yml +59 -0
  59. data/spec/generators/uploadcare_config_generator_spec.rb +18 -26
  60. data/spec/spec_helper.rb +17 -69
  61. data/spec/support/generators.rb +8 -5
  62. data/spec/support/vcr.rb +14 -0
  63. data/spec/tmp/config/initializers/uploadcare.rb +138 -0
  64. data/spec/uploadcare/rails/action_view/uploadcare_uploader_tags_spec.rb +40 -0
  65. data/spec/uploadcare/rails/action_view/uploadcare_widget_tags_spec.rb +46 -0
  66. data/spec/uploadcare/rails/active_record/mount_uploadcare_file_spec.rb +33 -0
  67. data/spec/uploadcare/rails/active_record/mount_uploadcare_group_spec.rb +32 -0
  68. data/spec/uploadcare/rails/api/rest/conversion_api_spec.rb +110 -0
  69. data/spec/uploadcare/rails/api/rest/file_api_spec.rb +85 -0
  70. data/spec/uploadcare/rails/api/rest/group_api_spec.rb +61 -0
  71. data/spec/uploadcare/rails/api/rest/project_api_spec.rb +33 -0
  72. data/spec/uploadcare/rails/api/rest/webhook_api_spec.rb +59 -0
  73. data/spec/uploadcare/rails/api/upload/upload_api_spec.rb +82 -0
  74. data/spec/uploadcare/rails/jobs/delete_file_job_spec.rb +15 -0
  75. data/spec/uploadcare/rails/jobs/store_file_job_spec.rb +15 -0
  76. data/spec/uploadcare/rails/jobs/store_group_job_spec.rb +15 -0
  77. data/spec/uploadcare/rails/objects/file_spec.rb +77 -0
  78. data/spec/uploadcare/rails/objects/group_spec.rb +87 -0
  79. data/spec/uploadcare/rails/services/id_extractor_spec.rb +26 -0
  80. data/spec/uploadcare/rails/transformations/image_transformations_spec.rb +120 -0
  81. data/spec/uploadcare/rails_spec.rb +7 -0
  82. metadata +129 -351
  83. data/config/initializers/uploadcare.rb +0 -26
  84. data/config/uploadcare_defaults.yml +0 -12
  85. data/lib/generators/templates/uploadcare_config_template.yml +0 -67
  86. data/lib/tasks/uploadcare_rails_tasks.rake +0 -4
  87. data/lib/uploadcare/rails/action_view/include_tags.rb +0 -47
  88. data/lib/uploadcare/rails/action_view/uploader_tags.rb +0 -76
  89. data/lib/uploadcare/rails/active_record/has_file.rb +0 -92
  90. data/lib/uploadcare/rails/active_record/has_group.rb +0 -89
  91. data/lib/uploadcare/rails/active_record/has_object.rb +0 -9
  92. data/lib/uploadcare/rails/formtastic/formtastic.rb +0 -73
  93. data/lib/uploadcare/rails/operations.rb +0 -57
  94. data/lib/uploadcare/rails/settings.rb +0 -81
  95. data/lib/uploadcare/rails/simple_form/simple_form.rb +0 -63
  96. data/spec/caching/file_caching_spec.rb +0 -26
  97. data/spec/caching/group_caching_spec.rb +0 -40
  98. data/spec/dummy/README.rdoc +0 -28
  99. data/spec/dummy/Rakefile +0 -6
  100. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  101. data/spec/dummy/app/assets/javascripts/post_with_collections.js +0 -2
  102. data/spec/dummy/app/assets/javascripts/posts.js +0 -2
  103. data/spec/dummy/app/assets/javascripts/posts_with_collection_and_files.js +0 -2
  104. data/spec/dummy/app/assets/stylesheets/application.css +0 -13
  105. data/spec/dummy/app/assets/stylesheets/post_with_collections.css +0 -4
  106. data/spec/dummy/app/assets/stylesheets/posts.css +0 -4
  107. data/spec/dummy/app/assets/stylesheets/posts_with_collection_and_files.css +0 -4
  108. data/spec/dummy/app/assets/stylesheets/scaffold.css +0 -56
  109. data/spec/dummy/app/controllers/application_controller.rb +0 -5
  110. data/spec/dummy/app/controllers/post_with_collections_controller.rb +0 -58
  111. data/spec/dummy/app/controllers/posts_controller.rb +0 -58
  112. data/spec/dummy/app/controllers/posts_with_collection_and_files_controller.rb +0 -58
  113. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  114. data/spec/dummy/app/helpers/post_with_collections_helper.rb +0 -2
  115. data/spec/dummy/app/helpers/posts_helper.rb +0 -2
  116. data/spec/dummy/app/helpers/posts_with_collection_and_files_helper.rb +0 -2
  117. data/spec/dummy/app/models/post.rb +0 -4
  118. data/spec/dummy/app/models/post_with_collection.rb +0 -4
  119. data/spec/dummy/app/models/posts_with_collection_and_file.rb +0 -4
  120. data/spec/dummy/app/views/layouts/application.html.erb +0 -16
  121. data/spec/dummy/app/views/post_with_collections/_form.html.erb +0 -25
  122. data/spec/dummy/app/views/post_with_collections/edit.html.erb +0 -6
  123. data/spec/dummy/app/views/post_with_collections/index.html.erb +0 -29
  124. data/spec/dummy/app/views/post_with_collections/new.html.erb +0 -5
  125. data/spec/dummy/app/views/post_with_collections/show.html.erb +0 -14
  126. data/spec/dummy/app/views/post_with_collections/show.json.jbuilder +0 -1
  127. data/spec/dummy/app/views/posts/_form.html.erb +0 -31
  128. data/spec/dummy/app/views/posts/edit.html.erb +0 -6
  129. data/spec/dummy/app/views/posts/index.html.erb +0 -37
  130. data/spec/dummy/app/views/posts/new.html.erb +0 -5
  131. data/spec/dummy/app/views/posts/post.json.builder +0 -1
  132. data/spec/dummy/app/views/posts/show.html.erb +0 -15
  133. data/spec/dummy/app/views/posts_with_collection_and_files/_form.html.erb +0 -27
  134. data/spec/dummy/app/views/posts_with_collection_and_files/edit.html.erb +0 -6
  135. data/spec/dummy/app/views/posts_with_collection_and_files/index.html.erb +0 -25
  136. data/spec/dummy/app/views/posts_with_collection_and_files/new.html.erb +0 -5
  137. data/spec/dummy/app/views/posts_with_collection_and_files/show.html.erb +0 -4
  138. data/spec/dummy/app/views/posts_with_collection_and_files/show.json.jbuilder +0 -1
  139. data/spec/dummy/bin/bundle +0 -3
  140. data/spec/dummy/bin/rails +0 -4
  141. data/spec/dummy/bin/rake +0 -4
  142. data/spec/dummy/config/application.rb +0 -23
  143. data/spec/dummy/config/boot.rb +0 -4
  144. data/spec/dummy/config/database.yml +0 -25
  145. data/spec/dummy/config/environment.rb +0 -5
  146. data/spec/dummy/config/environments/development.rb +0 -29
  147. data/spec/dummy/config/environments/production.rb +0 -80
  148. data/spec/dummy/config/environments/test.rb +0 -36
  149. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  150. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  151. data/spec/dummy/config/initializers/formtastic.rb +0 -111
  152. data/spec/dummy/config/initializers/inflections.rb +0 -16
  153. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  154. data/spec/dummy/config/initializers/secret_token.rb +0 -12
  155. data/spec/dummy/config/initializers/session_store.rb +0 -3
  156. data/spec/dummy/config/initializers/simple_form.rb +0 -167
  157. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  158. data/spec/dummy/config/locales/en.yml +0 -23
  159. data/spec/dummy/config/locales/simple_form.en.yml +0 -31
  160. data/spec/dummy/config/routes.rb +0 -63
  161. data/spec/dummy/config/uploadcare.yml +0 -64
  162. data/spec/dummy/config.ru +0 -4
  163. data/spec/dummy/db/migrate/20171012001801_create_tables.rb +0 -27
  164. data/spec/dummy/db/migrate/20181001132710_add_other_file_to_post.rb +0 -8
  165. data/spec/dummy/db/migrate/20181001141434_add_other_file_to_post_with_collections.rb +0 -8
  166. data/spec/dummy/db/schema.rb +0 -39
  167. data/spec/dummy/lib/templates/erb/scaffold/_form.html.erb +0 -13
  168. data/spec/dummy/public/404.html +0 -58
  169. data/spec/dummy/public/422.html +0 -58
  170. data/spec/dummy/public/500.html +0 -57
  171. data/spec/dummy/public/favicon.ico +0 -0
  172. data/spec/helpers/form_helpers_has_file_spec.rb +0 -30
  173. data/spec/helpers/form_helpers_has_group_spec.rb +0 -30
  174. data/spec/helpers/form_helpers_spec.rb +0 -33
  175. data/spec/helpers/formtastic_spec.rb +0 -25
  176. data/spec/helpers/include_tags_spec.rb +0 -54
  177. data/spec/helpers/simple_form_spec.rb +0 -25
  178. data/spec/helpers/uploader_tags_spec.rb +0 -21
  179. data/spec/models/has_both_file_and_group_spec.rb +0 -43
  180. data/spec/models/has_file_spec.rb +0 -46
  181. data/spec/models/has_group_spec.rb +0 -57
  182. data/spec/models/has_several_files_spec.rb +0 -55
  183. data/spec/models/has_several_groups_spec.rb +0 -62
  184. data/spec/objects/file_spec.rb +0 -27
  185. data/spec/objects/group_spec.rb +0 -51
  186. data/spec/operations_spec.rb +0 -28
  187. data/spec/uploadcare/rails/settings_spec.rb +0 -136
  188. data/spec/uploadcare_rails_settings_spec.rb +0 -56
  189. data/spec/view.png +0 -0
  190. data/spec/view2.jpg +0 -0
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uploadcare/rails/api/rest/base'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ module Api
8
+ module Rest
9
+ # A class that contains Webhook related methods for Uploadcare REST API
10
+ class WebhookApi < Base
11
+ class << self
12
+ # Returns a list (not paginated) of webhooks
13
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/webhooksList
14
+ # rubocop:disable Naming/AccessorMethodName
15
+ def get_webhooks
16
+ Uploadcare::Webhook.list
17
+ end
18
+ # rubocop:enable Naming/AccessorMethodName
19
+
20
+ # Create a webhook
21
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/webhookCreate
22
+ def create_webhook(target_url, event: 'file.uploaded', is_active: true)
23
+ options = { target_url: target_url, event: event, is_active: is_active }
24
+ Uploadcare::Webhook.create(**options)
25
+ end
26
+
27
+ # Updates a webhook
28
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/updateWebhook
29
+ def update_webhook(id, **options)
30
+ Uploadcare::Webhook.update(id, **options)
31
+ end
32
+
33
+ # Permanently deletes a webhook
34
+ # @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/webhookUnsubscribe
35
+ def delete_webhook(target_url)
36
+ Uploadcare::Webhook.delete(target_url)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ Uploadcare::WebhookApi = Uploadcare::Rails::Api::Rest::WebhookApi
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uploadcare'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ module Api
8
+ module Upload
9
+ class Base; end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uploadcare/rails/api/upload/base'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ module Api
8
+ module Upload
9
+ # A client for general uploads
10
+ #
11
+ # @see https://uploadcare.com/api-refs/upload-api/#tag/Upload
12
+ class UploadApi < Base
13
+ class << self
14
+ # Uploads single files smaller. Supports small (less that 100 MB) and large single files.
15
+ #
16
+ # @see https://uploadcare.com/api-refs/upload-api/#operation/baseUpload
17
+ # @see https://uploadcare.com/api-refs/upload-api/#operation/multipartFileUploadStart
18
+ def upload_file(file, **options)
19
+ return upload(file, options) if file?(file) || file.is_a?(String)
20
+
21
+ raise TypeError, "The first argument must be a File or String (URL), #{file.class} given"
22
+ end
23
+
24
+ # Uploads several files smaller than 100MB.
25
+ #
26
+ # https://uploadcare.com/api-refs/upload-api/#operation/multipartFileUploadStart
27
+ def upload_files(files, **options)
28
+ return upload(files, options) if array_of_files?(files)
29
+
30
+ raise TypeError, 'The first argument must be an Array of File objects'
31
+ end
32
+
33
+ private
34
+
35
+ def array_of_files?(files)
36
+ files.is_a?(Array) && files.all? { |file| file?(file) }
37
+ end
38
+
39
+ def file?(object)
40
+ object.respond_to?(:path) && ::File.exist?(object.path)
41
+ end
42
+
43
+ def upload(files, options)
44
+ Uploadcare::Uploader.upload(files, **options)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ Uploadcare::UploadApi = Uploadcare::Rails::Api::Upload::UploadApi
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ # A class for storing config parameters
8
+ class Configuration
9
+ include Singleton
10
+
11
+ CONFIG_GLOBAL_PARAMS = %w[
12
+ public_key secret_key cache_files cache_expires_in cache_namespace cdn_hostname
13
+ store_files_after_save store_files_async
14
+ delete_files_after_destroy delete_files_async
15
+ ].freeze
16
+
17
+ WIDGET_PARAMS = %w[
18
+ public_key images_only preview_step crop image_shrink
19
+ clearable tabs input_accept_types preferred_types system_dialog multipart_min_size
20
+ preview_proxy cdn_base do_not_store audio_bits_per_second video_preferred_mime_types
21
+ video_bits_per_second camera_mirror_default live manual_start
22
+ locale locale_translations locale_pluralize
23
+ ].freeze
24
+
25
+ attr_accessor(*(CONFIG_GLOBAL_PARAMS + WIDGET_PARAMS).uniq)
26
+
27
+ def uploader_parameters
28
+ WIDGET_PARAMS.map do |param_name|
29
+ param_value = instance_variable_get("@#{param_name}")
30
+ next if param_value.nil?
31
+
32
+ param_value = handle_param_value(param_value)
33
+ "UPLOADCARE_#{param_name.upcase} = #{param_value};"
34
+ end.compact.join("\n")
35
+ end
36
+
37
+ def widget
38
+ OpenStruct.new(WIDGET_PARAMS.map { |param| [param, public_send(param)] }.to_h)
39
+ end
40
+
41
+ private
42
+
43
+ def handle_param_value(param_value)
44
+ case param_value
45
+ when Hash
46
+ param_value.deep_stringify_keys.to_json
47
+ when Array
48
+ "'#{param_value.join(' ')}'"
49
+ when TrueClass, FalseClass
50
+ param_value
51
+ else
52
+ "'#{param_value}'"
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,28 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/engine'
2
- require 'uploadcare/rails/settings'
3
4
 
4
5
  module Uploadcare
5
6
  module Rails
7
+ # Rails engine that allows to extend default rails libraries
6
8
  class Engine < ::Rails::Engine
7
9
  initializer 'uploadcare-rails.load' do
8
- # load actual rails extentions
9
- # active record extention for stand-alone file models and models has files
10
-
11
- ActiveSupport.on_load :active_record do
12
- require 'uploadcare/rails/active_record/has_file'
13
- require 'uploadcare/rails/active_record/has_group'
14
- end
15
-
16
- # JS options, widets from cdn etc
10
+ # Load extensions
11
+ # Extend action_view with widget_tag
17
12
  ActiveSupport.on_load :action_view do
18
- require 'uploadcare/rails/action_view/include_tags'
19
- require 'uploadcare/rails/action_view/uploader_tags'
20
-
21
- # Simple Form helpers
22
- require 'uploadcare/rails/simple_form/simple_form' if defined?(SimpleForm)
13
+ require 'uploadcare/rails/action_view/uploadcare_include_tags'
14
+ require 'uploadcare/rails/action_view/uploadcare_uploader_tags'
15
+ end
23
16
 
24
- # Formastic helpers
25
- require 'uploadcare/rails/formtastic/formtastic' if defined?(Formtastic)
17
+ # Load extensions for active_record
18
+ # Extend active_record with mount_uploadcare_file and mount_uploadcare_file_group methods
19
+ ActiveSupport.on_load :active_record do
20
+ require 'uploadcare/rails/active_record/mount_uploadcare_file'
21
+ require 'uploadcare/rails/active_record/mount_uploadcare_file_group'
26
22
  end
27
23
  end
28
24
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_job'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ # A job deleting files from Uploadcare
8
+ class DeleteFileJob < ActiveJob::Base
9
+ def perform(file_uuid)
10
+ Uploadcare::FileApi.delete_file(file_uuid) if file_uuid
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_job'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ # A job storing files to Uploadcare
8
+ class StoreFileJob < ActiveJob::Base
9
+ def perform(file_uuid)
10
+ Uploadcare::FileApi.store_file(file_uuid) if file_uuid
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_job'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ # A job storing group files to Uploadcare
8
+ class StoreGroupJob < ActiveJob::Base
9
+ def perform(group_id)
10
+ Uploadcare::GroupApi.store_group(group_id) if group_id
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ module Objects
8
+ # A module that contains methods for attribute assignation and caching
9
+ module Loadable
10
+ extend ActiveSupport::Concern
11
+ include ::ActiveRecord::AttributeAssignment
12
+
13
+ class_methods do
14
+ def build_cache_key(key)
15
+ [uploadcare_configuration.cache_namespace, key].flatten.reject(&:blank?)
16
+ end
17
+
18
+ def uploadcare_configuration
19
+ Uploadcare::Rails.configuration
20
+ end
21
+ end
22
+
23
+ def update_attrs(new_attrs)
24
+ assign_attributes(new_attrs)
25
+ self
26
+ end
27
+
28
+ def cache_expires_in
29
+ uploadcare_configuration.cache_expires_in
30
+ end
31
+
32
+ def caching_enabled?
33
+ uploadcare_configuration.cache_files
34
+ end
35
+
36
+ def uploadcare_configuration
37
+ self.class.uploadcare_configuration
38
+ end
39
+
40
+ def cache_key
41
+ self.class.build_cache_key(cdn_url)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,40 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uploadcare/rails/api/rest/file_api'
4
+ require 'uploadcare/rails/transformations/image_transformations'
5
+ require 'uploadcare/rails/objects/concerns/loadable'
6
+
1
7
  module Uploadcare
2
8
  module Rails
3
- class File < Uploadcare::Api::File
4
- def url(operations = nil)
5
- cdn_url unless operations
6
- cdn_url + prepared_operations(operations)
9
+ # A wrapper class for Uploadcare::File object.
10
+ # Allows caching loaded files and has methods for Rails model attributes
11
+ class File < Uploadcare::Entity::File
12
+ include Objects::Loadable
13
+
14
+ ATTR_ENTITIES = [:cdn_url].freeze
15
+
16
+ attr_entity(*superclass.entity_attributes + ATTR_ENTITIES)
17
+
18
+ def transform_url(transformations, transformator_class = Uploadcare::Rails::Transformations::ImageTransformations)
19
+ return if cdn_url.blank?
20
+
21
+ transformations_query = transformator_class.new(transformations).call if transformations.present?
22
+ [cdn_url, transformations_query].compact.join('-')
7
23
  end
8
24
 
9
- def prepared_operations(operations)
10
- Uploadcare::Rails::Operations.new(operations).to_s
25
+ def store
26
+ file_info = Uploadcare::FileApi.store_file(uuid).merge(cdn_url: cdn_url).to_h
27
+ ::Rails.cache.write(cache_key, file_info, expires_in: cache_expires_in) if caching_enabled?
28
+ update_attrs(file_info)
11
29
  end
12
30
 
13
- # construct image tag for file
14
- def image(operations = nil)
15
- image_tag(url(operations))
31
+ def delete
32
+ Uploadcare::FileApi.delete_file(uuid)
16
33
  end
17
34
 
18
- # override default to string method
19
- # instead of string representation of object it will return simple cdn url of a file
20
35
  def to_s
21
36
  cdn_url
22
37
  end
23
38
 
24
- def to_builder
25
- marshal_dump
39
+ def load
40
+ file_info = if caching_enabled?
41
+ ::Rails.cache.fetch(cache_key, expires_in: cache_expires_in) do
42
+ request_file_info_from_api
43
+ end
44
+ else
45
+ request_file_info_from_api
46
+ end
47
+ update_attrs(file_info)
26
48
  end
27
49
 
28
- def to_json
29
- marshal_dump
50
+ def loaded?
51
+ datetime_uploaded.present?
30
52
  end
31
53
 
32
- def as_json(_options = {})
33
- marshal_dump
34
- end
54
+ private
35
55
 
36
- def marshal_dump
37
- @table.stringify_keys
56
+ def request_file_info_from_api
57
+ Uploadcare::FileApi.get_file(uuid).merge(self).to_h
38
58
  end
39
59
  end
40
60
  end
@@ -1,81 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uploadcare/rails/api/rest/group_api'
4
+ require 'uploadcare/rails/transformations/image_transformations'
5
+ require 'uploadcare/rails/objects/concerns/loadable'
6
+
1
7
  module Uploadcare
2
8
  module Rails
3
- class Group < Uploadcare::Api::Group
9
+ # A wrapper class that for Uploadcare::Group object.
10
+ # Allows caching loaded groups and has methods for Rails model attributes
11
+ class Group < Uploadcare::Entity::Group
12
+ include Objects::Loadable
13
+
14
+ attr_entity(*superclass.entity_attributes)
4
15
 
5
- # warkaround to build images urls without API request
6
- # builds array of image urls including operations
7
- def urls(operations = nil)
8
- (0..files_count.to_i - 1).to_a.map do |i|
9
- [
10
- @api.options[:static_url_base],
11
- uuid,
12
- 'nth',
13
- i,
14
- Operations.new(operations).to_s
15
- ].join('/')
16
+ def transform_file_urls(
17
+ transformations,
18
+ transformator_class = Uploadcare::Rails::Transformations::ImageTransformations
19
+ )
20
+ return if cdn_url.blank?
21
+
22
+ transformations_query = transformator_class.new(transformations).call if transformations.present?
23
+ map_file_urls do |index|
24
+ [group_file_url(index), transformations_query].compact.join('-')
16
25
  end
17
26
  end
18
27
 
19
- def load_data
20
- load_data! unless is_loaded?
21
- self
28
+ def file_urls
29
+ map_file_urls do |index|
30
+ group_file_url(index)
31
+ end
22
32
  end
23
33
 
24
- alias_method :load, :load_data
25
-
26
- def load_data!
27
- set_data(@api.get("/groups/#{ uuid }/"))
28
- cache_data
29
- self
34
+ def store
35
+ group_info = Uploadcare::GroupApi.store_group(id).to_h
36
+ ::Rails.cache.write(cache_key, group_info, expires_in: cache_expires_in) if caching_enabled?
37
+ update_attrs(group_info)
30
38
  end
31
39
 
32
- alias_method :load!, :load_data!
33
-
34
- def cache_data
35
- return unless UPLOADCARE_SETTINGS.cache_groups
36
- ::Rails.cache.write(cdn_url, marshal_dump)
37
- self
40
+ def to_s
41
+ cdn_url
38
42
  end
39
43
 
40
- def marshal_dump
41
- table = @table.deep_dup.stringify_keys!
42
-
43
- if table['files']
44
- table['files'].map! do |file|
45
- file.marshal_dump.stringify_keys
46
- end
47
- end
48
-
49
- table
44
+ def load
45
+ group_info = if caching_enabled?
46
+ ::Rails.cache.fetch(cache_key, expires_in: cache_expires_in) do
47
+ request_group_info_from_api
48
+ end
49
+ else
50
+ request_group_info_from_api
51
+ end
52
+ update_attrs(group_info)
50
53
  end
51
54
 
52
- def to_json
53
- marshal_dump
55
+ def loaded?
56
+ datetime_created.present?
54
57
  end
55
58
 
56
- def as_json(_options = {})
57
- marshal_dump
58
- end
59
+ private
59
60
 
60
- # override default to string method
61
- # for group we just will return the uuid
62
- def to_s
63
- uuid
61
+ def request_group_info_from_api
62
+ Uploadcare::GroupApi.get_group(id).merge(self).to_h
64
63
  end
65
64
 
66
- private
65
+ def group_file_url(index)
66
+ "#{cdn_url}nth/#{index}/"
67
+ end
67
68
 
68
- def map_files(data)
69
- data.stringify_keys!
70
- data['files'].map! do |file|
71
- if file.nil?
72
- file
73
- else
74
- Uploadcare::Rails::File.new(@api, file['uuid'], file)
75
- end
76
- end
69
+ def map_file_urls(&block)
70
+ return [] unless block_given?
77
71
 
78
- data
72
+ Array.new(files_count.to_i, &block)
79
73
  end
80
74
  end
81
75
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uploadcare
4
+ module Rails
5
+ # A class extracting the files count param of a file group from a group ID.
6
+ class FilesCountExtractor
7
+ class << self
8
+ def call(input_str)
9
+ input_str.split('~').last if input_str.present?
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uploadcare
4
+ module Rails
5
+ # A class extracting an ID of an object from an URL.
6
+ class IdExtractor
7
+ class << self
8
+ # regex is an UUID-regex by default
9
+ def call(input_str, regex = /\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b/)
10
+ input_str.match(regex).to_s
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uploadcare/errors/type_error'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ module Transformations
8
+ # A class for building image urls after image transformations.
9
+ class ImageTransformations
10
+ def initialize(options = {})
11
+ raise ArgumentError, "Options argument must be a Hash, #{options.class} is given?" unless options.is_a?(Hash)
12
+
13
+ @options = options.map { |k, v| [k.to_sym, v] }.to_h
14
+ end
15
+
16
+ def call
17
+ options_to_a.compact.join('-').squeeze('/').gsub(/\s/, '').presence
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :options
23
+
24
+ def options_to_a
25
+ options.map do |key, value|
26
+ "/#{key}/#{adjusted_value(value)}/"
27
+ end
28
+ end
29
+
30
+ def adjusted_value(value)
31
+ case value
32
+ when Hash
33
+ value.values.join('/')
34
+ when TrueClass, FalseClass, 'true', 'false'
35
+ nil
36
+ when Array
37
+ value.join(',')
38
+ else
39
+ value
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Uploadcare
2
4
  module Rails
3
- VERSION = '1.2.1'.freeze
5
+ VERSION = '2.0.0'
4
6
  end
5
7
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uploadcare/rails/version'
4
+
5
+ module Uploadcare
6
+ module Rails
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
10
+ end
@@ -1,22 +1,33 @@
1
- require 'uploadcare'
1
+ # frozen_string_literal: true
2
2
 
3
- require_relative 'uploadcare/rails/version'
4
- require_relative 'uploadcare/rails/engine'
5
- require_relative 'uploadcare/rails/settings'
6
- require_relative 'uploadcare/rails/operations'
7
- require_relative 'uploadcare/rails/objects/file'
8
- require_relative 'uploadcare/rails/objects/group'
3
+ require 'uploadcare/rails/engine'
4
+ require 'uploadcare/rails/configuration'
5
+ require 'uploadcare/rails/objects/file'
6
+ require 'uploadcare/rails/objects/group'
7
+ require 'uploadcare/rails/api/rest/file_api'
8
+ require 'uploadcare/rails/api/rest/group_api'
9
+ require 'uploadcare/rails/api/rest/project_api'
10
+ require 'uploadcare/rails/api/rest/webhook_api'
11
+ require 'uploadcare/rails/api/rest/conversion_api'
12
+ require 'uploadcare/rails/api/upload/upload_api'
9
13
 
10
14
  module Uploadcare
15
+ # A root module for the gem
11
16
  module Rails
12
- DEFAULT_SETTINGS = {
13
- widget_version: '3.x',
14
- user_agent_environment: {
15
- framework_name: 'Rails',
16
- framework_version: ::Rails.version,
17
- extension_name: 'UploadcareRails',
18
- extension_version: Uploadcare::Rails::VERSION
19
- }
20
- }.freeze
17
+ module_function
18
+
19
+ def configure
20
+ yield configuration
21
+ end
22
+
23
+ def configuration
24
+ @configuration
25
+ end
26
+
27
+ def initialize_config
28
+ @configuration = Uploadcare::Rails::Configuration.instance
29
+ end
30
+
31
+ initialize_config
21
32
  end
22
33
  end
Binary file