resizing 0.2.1 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce4e2a0408f940809b3e79bb04f914a4ef94422b21bad9fa5ba1d4a2d6ede4b0
4
- data.tar.gz: 57e968dbe1f44b6167d564139455e245e09ad8c9fd317e669defed1215984d5a
3
+ metadata.gz: d17b6239534054ad657a7f394cf765ff7f1944e07b4b82cde4707f022446460b
4
+ data.tar.gz: b95d919cf3e10847d659ce153699237419867b04b3d435fc21c5239063723f3c
5
5
  SHA512:
6
- metadata.gz: 2d359605f82bc68305a227926942e3b7358d2950223121d4dcdcfdc4ac43d6f249cc4b9480751718487d31179c5727ccb824b49887820f5728a0b362d007f274
7
- data.tar.gz: 3dcfaa7598dfc4a604ec2fe0a6fd194742c661b09f7a90cfe0775e06024b27fa47ab0acab179ff42478d3d44127336f94405fb646b5a9d19004c91477109ac1e
6
+ metadata.gz: 61554d168b48d937b57fc98def2264c91b6014248667479ab85d095a521b61d625df663f0e872a7a15dc7156523b1e6171f7445b037a57f2f4779089d10775ec
7
+ data.tar.gz: fdc8ccc54a90e0429461fe06b889d8f3c6701df11fa3be33cba86a1d6e4f54bdbe22da1f708ed6c21457d628d994e52294f443344847eeecb49500fc771a6e5a
data/lib/resizing.rb CHANGED
@@ -6,6 +6,7 @@ require 'json'
6
6
 
7
7
  module Resizing
8
8
  autoload :Client, 'resizing/client'
9
+ autoload :MockClient, 'resizing/mock_client'
9
10
  autoload :Configuration, 'resizing/configuration'
10
11
  autoload :CarrierWave, 'resizing/carrier_wave'
11
12
 
@@ -33,13 +34,11 @@ module Resizing
33
34
  end
34
35
 
35
36
  def self.post(file_or_binary, options)
36
- client = Resizing::Client.new
37
- client.post file_or_binary, options
37
+ self.client.post file_or_binary, options
38
38
  end
39
39
 
40
40
  def self.put(name, file_or_binary, options)
41
- client = Resizing::Client.new
42
- client.put name, file_or_binary, options
41
+ self.client.put name, file_or_binary, options
43
42
  end
44
43
 
45
44
  # TODO: refactoring
@@ -57,4 +56,12 @@ module Resizing
57
56
  def self.separate_public_id public_id
58
57
  public_id.match('/projects/(?<project_id>[0-9a-f-]+)/upload/images/(?<image_id>[0-9a-f-]+)(/v(?<version>[^/]+))?')
59
58
  end
59
+
60
+ def self.client
61
+ if self.configure.enable_mock
62
+ Resizing::MockClient.new
63
+ else
64
+ Resizing::Client.new
65
+ end
66
+ end
60
67
  end
@@ -34,12 +34,12 @@ module Resizing
34
34
  image_id = separted[:image_id]
35
35
  resp = client.delete(image_id)
36
36
  if resp['error'] == 'ActiveRecord::RecordNotFound' # 404 not found
37
- model.send :write_attribute, serialization_column, nil unless model.destroyed?
37
+ model.send :write_attribute, serialization_column, nil
38
38
  return
39
39
  end
40
40
 
41
41
  if public_id == resp['public_id']
42
- model.send :write_attribute, serialization_column, nil unless model.destroyed?
42
+ model.send :write_attribute, serialization_column, nil
43
43
  return
44
44
  end
45
45
 
@@ -17,7 +17,12 @@ module Resizing
17
17
  private
18
18
 
19
19
  def load_yaml filename
20
- YAML.load_file(filename)['http_interactions'].first['response']['body']
20
+ path = "#{library_root}/#{filename}"
21
+ YAML.load_file(path)['http_interactions'].first['response']['body']
22
+ end
23
+
24
+ def library_root
25
+ File.join(File.dirname(__FILE__), '..', '..')
21
26
  end
22
27
  end
23
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resizing
4
- VERSION = '0.2.1'
4
+ VERSION = '0.3.0'
5
5
  end
data/resizing.gemspec CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  # Specify which files should be added to the gem when it is released.
24
24
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
25
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ `git ls-files -z`.split("\x0")
27
27
  end
28
28
  spec.bindir = 'exe'
29
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
Binary file
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Resizing
6
+ module CarrierWave
7
+ module Storage
8
+ class FileTest < Minitest::Test
9
+ def setup
10
+ # NOP
11
+ end
12
+
13
+ def teardown
14
+ # NOP
15
+ end
16
+
17
+ def test_store
18
+ # f = Resizing::CarrierWave::Storage::File.new(uploader)
19
+ # upload_file = File.new(uploader, path)
20
+ # f.store(upload_file)
21
+ # f.save!
22
+ end
23
+
24
+ def uploader
25
+ MiniTest::Mock.expect(:cache_path)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
File without changes
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Resizing
6
+ class CarrierWaveTest < Minitest::Test
7
+ def setup
8
+ TestModel.delete_all
9
+
10
+ @configuration_template = {
11
+ host: 'http://192.168.56.101:5000',
12
+ project_id: '098a2a0d-c387-4135-a071-1254d6d7e70a',
13
+ secret_token: '4g1cshg2lq8j93ufhvqrpjswxmtjz12yhfvq6w79jpwi7cr7nnknoqgwzkwerbs6',
14
+ open_timeout: 10,
15
+ response_timeout: 20
16
+ }
17
+ Resizing.configure = @configuration_template
18
+ end
19
+
20
+ def teardown; end
21
+
22
+ def test_xxxx
23
+ model = prepare_model
24
+
25
+ VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
26
+ SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
27
+ model.remove_resizing_picture!
28
+
29
+ assert_nil model.resizing_picture_url
30
+ end
31
+ end
32
+ end
33
+
34
+ def test_picture_url_return_correct_value_and_when_model_reloaded
35
+ model = prepare_model
36
+ model.save!
37
+ assert_equal(expect_url, model.resizing_picture_url)
38
+
39
+ model.reload
40
+ assert_equal(expect_url, model.resizing_picture_url)
41
+ end
42
+
43
+ def expect_url
44
+ 'http://192.168.56.101:5000/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/'+
45
+ 'upload/images/28c49144-c00d-4cb5-8619-98ce95977b9c/v1Id850__tqNsnoGWWUibtIBZ5NgjV45M/c_limit,w_1000'
46
+ end
47
+
48
+ def prepare_model
49
+ VCR.use_cassette 'carrier_wave_test/save' do
50
+ SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
51
+ model = TestModel.new
52
+ file = File.open('test/data/images/sample1.jpg', 'r')
53
+ uploaded_file = ActionDispatch::Http::UploadedFile.new(
54
+ filename: File.basename(file.path),
55
+ type: 'image/jpeg',
56
+ tempfile: file
57
+ )
58
+
59
+ model.resizing_picture = uploaded_file
60
+ return model
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Resizing
6
+ class ClientTest < Minitest::Test
7
+ def setup
8
+ # NOP
9
+ @configuration_template = {
10
+ host: 'http://192.168.56.101:5000',
11
+ project_id: '098a2a0d-c387-4135-a071-1254d6d7e70a',
12
+ secret_token: '4g1cshg2lq8j93ufhvqrpjswxmtjz12yhfvq6w79jpwi7cr7nnknoqgwzkwerbs6',
13
+ open_timeout: 10,
14
+ response_timeout: 20
15
+ }
16
+ end
17
+
18
+ def teardown
19
+ # NOP
20
+ end
21
+
22
+ def test_that_it_is_initialized
23
+ Resizing.configure = @configuration_template
24
+
25
+ client = Resizing::Client.new
26
+ assert(!client.config.nil?)
27
+ assert_equal(client.config, Resizing.configure)
28
+ end
29
+
30
+ def test_that_it_is_initialized_with_configuration
31
+ config = Resizing::Configuration.new(@configuration_template)
32
+ client = Resizing::Client.new(config)
33
+ assert(!client.config.nil?)
34
+ assert_equal(client.config, config)
35
+ end
36
+
37
+ def test_that_it_postable_file
38
+ Resizing.configure = @configuration_template
39
+
40
+ client = Resizing::Client.new
41
+ VCR.use_cassette 'client/post' do
42
+ f = File.open('test/data/images/sample1.jpg', 'r')
43
+ r = client.post(f, content_type: 'image/jpeg')
44
+ assert_equal(r['id'], 'bfdaf2b3-7ec5-41f4-9caa-d53247dd9666')
45
+ assert_equal(r['project_id'], Resizing.configure.project_id)
46
+ assert_equal(r['content_type'], 'image/jpeg')
47
+ assert(!r['latest_version_id'].nil?)
48
+ assert(!r['latest_etag'].nil?)
49
+ assert(!r['created_at'].nil?)
50
+ assert(!r['updated_at'].nil?)
51
+ assert_equal(r['public_id'], '/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/bfdaf2b3-7ec5-41f4-9caa-d53247dd9666/vAyWaxx96gLaAzB9Bq.VbX1_pxfXJ0Jcq')
52
+ end
53
+ end
54
+
55
+ def test_that_it_putable_file
56
+ Resizing.configure = @configuration_template
57
+
58
+ client = Resizing::Client.new
59
+ VCR.use_cassette 'client/put' do
60
+ f = File.open('test/data/images/sample1.jpg', 'r')
61
+ name = 'AWEaewfAreaweFAFASfwe'
62
+ r = client.put(name, f, content_type: 'image/jpeg')
63
+ assert_equal(r['id'], name)
64
+ assert_equal(r['project_id'], Resizing.configure.project_id)
65
+ assert_equal(r['content_type'], 'image/jpeg')
66
+ assert(!r['latest_version_id'].nil?)
67
+ assert(!r['latest_etag'].nil?)
68
+ assert(!r['created_at'].nil?)
69
+ assert(!r['updated_at'].nil?)
70
+ assert_equal(
71
+ r['public_id'],
72
+ "/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/#{name}/v6Ew3HmDAYfb3NMRdLxR45i_gXMbLlGyi"
73
+ )
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Resizing
6
+ class ConfigurationTest < Minitest::Test
7
+ def setup
8
+ @template = {
9
+ host: 'http://192.168.56.101:5000',
10
+ project_id: '098a2a0d-c387-4135-a071-1254d6d7e70a',
11
+ secret_token: '4g1cshg2lq8j93ufhvqrpjswxmtjz12yhfvq6w79jpwi7cr7nnknoqgwzkwerbs6',
12
+ open_timeout: 10,
13
+ response_timeout: 20
14
+ }.freeze
15
+ end
16
+
17
+ def teardown
18
+ # NOP
19
+ end
20
+
21
+ def test_that_it_has_default_host
22
+ template = @template.dup
23
+ template.delete(:host)
24
+ config = Resizing::Configuration.new template
25
+ assert_equal(config.host, Resizing::Configuration::DEFAULT_HOST)
26
+ end
27
+
28
+ def test_that_it_has_same_host_value
29
+ template = @template.dup
30
+ config = Resizing::Configuration.new template
31
+ assert_equal(config.host, template[:host])
32
+ end
33
+
34
+ def test_that_it_has_no_project_id
35
+ template = @template.dup
36
+ template.delete(:project_id)
37
+ assert_raises ConfigurationError do
38
+ Resizing::Configuration.new template
39
+ end
40
+ end
41
+
42
+ def test_that_it_has_same_project_id_value
43
+ template = @template.dup
44
+ config = Resizing::Configuration.new template
45
+ assert_equal(config.project_id, template[:project_id])
46
+ end
47
+
48
+ def test_that_it_has_no_secret_token
49
+ template = @template.dup
50
+ template.delete(:project_id)
51
+ assert_raises ConfigurationError do
52
+ Resizing::Configuration.new template
53
+ end
54
+ end
55
+
56
+ def test_that_it_has_same_secret_token_value
57
+ template = @template.dup
58
+ config = Resizing::Configuration.new template
59
+ assert_equal(config.secret_token, template[:secret_token])
60
+ end
61
+
62
+ def test_that_it_has_default_open_timeout
63
+ template = @template.dup
64
+ template.delete(:open_timeout)
65
+ config = Resizing::Configuration.new template
66
+ assert_equal(config.open_timeout, Resizing::Configuration::DEFAULT_OPEN_TIMEOUT)
67
+ end
68
+
69
+ def test_that_it_has_same_open_timeout
70
+ template = @template.dup
71
+ config = Resizing::Configuration.new template
72
+ assert_equal(config.open_timeout, template[:open_timeout])
73
+ end
74
+
75
+ def test_that_it_return_auth_header_token
76
+ Timecop.freeze(Time.parse('2020-05-29 05:40:00 +0900')) do
77
+ template = @template.dup
78
+ config = Resizing::Configuration.new template
79
+ assert_equal(
80
+ 'v1,1590698400,2b35ee78cd6ce32edb9b4d97b69306c678ce8dea871638ff6144b7be0d26173c',
81
+ config.generate_auth_header
82
+ )
83
+ end
84
+ end
85
+
86
+ def test_that_it_return_image_url
87
+ template = @template.dup
88
+ image_id = 'some-image-id'
89
+ config = Resizing::Configuration.new template
90
+ assert_equal(
91
+ 'http://192.168.56.101:5000/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/some-image-id',
92
+ config.generate_image_url(image_id)
93
+ )
94
+ end
95
+
96
+ def test_that_it_return_image_url_with_version_id
97
+ template = @template.dup
98
+ image_id = 'some-image-id'
99
+ version_id = 'version-id'
100
+ config = Resizing::Configuration.new template
101
+ assert_equal(
102
+ 'http://192.168.56.101:5000/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/some-image-id/vversion-id',
103
+ config.generate_image_url(image_id, version_id)
104
+ )
105
+ end
106
+
107
+ def test_that_it_return_transformation_path
108
+ data = [
109
+ { args: { w: 100 }, path: 'w_100' },
110
+ { args: { h: 100 }, path: 'h_100' },
111
+ { args: { f: 'webp' }, path: 'f_webp' },
112
+ { args: { c: 'fill' }, path: 'c_fill' }
113
+ ]
114
+ config = Resizing::Configuration.new @template
115
+ data.each do |v|
116
+ assert_equal(
117
+ v[:path],
118
+ config.transformation_path(v[:args])
119
+ )
120
+ end
121
+ end
122
+
123
+ def test_that_it_generated_identifier_path
124
+ config = Resizing::Configuration.new @template
125
+ assert_match %r{/projects/#{config.project_id}/upload/images/[\da-z-]}, config.generate_identifier
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class ResizingTest < Minitest::Test
6
+ def test_that_it_has_a_version_number
7
+ refute_nil ::Resizing::VERSION
8
+ end
9
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
+ require 'time'
5
+ require 'timecop'
6
+ require 'vcr'
7
+
8
+ require 'rails'
9
+ require 'active_record'
10
+ require 'fog-aws'
11
+ require 'carrierwave'
12
+ require 'resizing'
13
+ require 'pry-byebug'
14
+
15
+ require 'minitest/autorun'
16
+
17
+ VCR.configure do |c|
18
+ c.cassette_library_dir = 'test/vcr'
19
+ c.hook_into :faraday
20
+ c.allow_http_connections_when_no_cassette = false
21
+ end
22
+
23
+ ActiveRecord::Base.establish_connection(
24
+ adapter: 'mysql2',
25
+ host: '127.0.0.1',
26
+ port: 3306,
27
+ database: 'resizing_gem_test',
28
+ encoding: 'utf8',
29
+ username: 'root',
30
+ password: 'secret'
31
+ )
32
+
33
+ ActiveRecord::Schema.define do
34
+ self.verbose = false
35
+ connection.execute 'drop table if exists test_models'
36
+
37
+ create_table :test_models do |t|
38
+ t.string :resizing_picture, null: true, default: nil
39
+ end
40
+ end
41
+
42
+ class ResizingUploader < CarrierWave::Uploader::Base
43
+ include Resizing::CarrierWave
44
+
45
+ process resize_to_limit: [1000]
46
+ end
47
+
48
+ class TestModel < ::ActiveRecord::Base
49
+ extend CarrierWave::Mount
50
+
51
+ mount_uploader :resizing_picture, ResizingUploader
52
+ end