resizing 0.3.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -69,9 +69,11 @@ module Resizing
69
69
  # たぶんここにおくものではない
70
70
  # もしくはキャッシュしない
71
71
  def generate_identifier
72
- @image_id ||= ::SecureRandom.uuid
72
+ "/projects/#{project_id}/upload/images/#{generate_image_id}"
73
+ end
73
74
 
74
- "/projects/#{project_id}/upload/images/#{@image_id}"
75
+ def generate_image_id
76
+ ::SecureRandom.uuid
75
77
  end
76
78
 
77
79
  def ==(other)
@@ -3,17 +3,37 @@
3
3
  module Resizing
4
4
  class MockClient
5
5
  def post(file_or_binary, options = {})
6
- load_yaml('test/vcr/client/post.yml')
6
+ r = load_yaml('test/vcr/client/post.yml')
7
+ JSON.parse(r['string'])
7
8
  end
8
9
 
9
10
  def put(name, file_or_binary, options)
10
- load_yaml('test/vcr/client/put.yml')
11
+ r = load_yaml('test/vcr/client/put.yml')
12
+ result = JSON.parse(r['string'])
13
+ # replace name, public_id and version by name argument
14
+ result['id'] = name
15
+ result['public_id'].gsub!(/AWEaewfAreaweFAFASfwe/, name)
16
+ result['public_id'].gsub!(/v6Ew3HmDAYfb3NMRdLxR45i_gXMbLlGyi/, "v#{Time.now.to_f}")
17
+ result
11
18
  end
12
19
 
13
20
  def delete(name)
14
- load_yaml('test/vcr/client/delete.yml')
21
+ r = load_yaml('test/vcr/client/delete.yml')
22
+ result = JSON.parse(r['string'])
23
+ # replace name and public_id by name argument
24
+ result['id'] = name
25
+ result['public_id'].gsub!(/28c49144-c00d-4cb5-8619-98ce95977b9c/, name)
26
+ result
15
27
  end
16
28
 
29
+ def metadata(name)
30
+ r = load_yaml('test/vcr/client/metadata.yml')
31
+ result = JSON.parse(r['string'])
32
+ # replace name and public_id by name argument
33
+ result['id'] = name
34
+ result['public_id'].gsub!(/bfdaf2b3-7ec5-41f4-9caa-d53247dd9666/, name)
35
+ result
36
+ end
17
37
  private
18
38
 
19
39
  def load_yaml filename
@@ -22,7 +42,7 @@ module Resizing
22
42
  end
23
43
 
24
44
  def library_root
25
- File.join(File.dirname(__FILE__), '..', '..')
45
+ @library_root ||= File.expand_path('../../../', __FILE__)
26
46
  end
27
47
  end
28
48
  end
@@ -0,0 +1,50 @@
1
+ module Resizing
2
+ class PublicId
3
+ def initialize public_id
4
+ @public_id = public_id
5
+ parsed
6
+ end
7
+
8
+ def empty?
9
+ @public_id.to_s.empty?
10
+ end
11
+
12
+ def image_id
13
+ parsed[:image_id] if parsed
14
+ end
15
+
16
+ def project_id
17
+ parsed[:project_id] if parsed
18
+ end
19
+
20
+ def version
21
+ parsed[:version] if parsed
22
+ end
23
+
24
+ # temporary
25
+ def filename
26
+ image_id
27
+ end
28
+
29
+ def identifier
30
+ "/projects/#{project_id}/upload/images/#{image_id}"
31
+ end
32
+
33
+ def to_s
34
+ @public_id.to_s
35
+ end
36
+
37
+ private
38
+
39
+ def parsed
40
+ return nil if @public_id.nil?
41
+ unless defined? @parsed
42
+ @parsed = Resizing.separate_public_id(@public_id)
43
+ raise "type error #{@public_id}" if @parsed == nil
44
+ end
45
+ @parsed
46
+ end
47
+
48
+ private
49
+ end
50
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resizing
4
- VERSION = '0.3.2'
4
+ VERSION = '0.5.3'
5
5
  end
@@ -6,11 +6,12 @@ module Resizing
6
6
  class CarrierWaveTest < Minitest::Test
7
7
  def setup
8
8
  TestModel.delete_all
9
+ TestJPGModel.delete_all
9
10
 
10
11
  @configuration_template = {
11
12
  host: 'http://192.168.56.101:5000',
12
- project_id: '098a2a0d-c387-4135-a071-1254d6d7e70a',
13
- secret_token: '4g1cshg2lq8j93ufhvqrpjswxmtjz12yhfvq6w79jpwi7cr7nnknoqgwzkwerbs6',
13
+ project_id: 'e06e710d-f026-4dcf-b2c0-eab0de8bb83f',
14
+ secret_token: 'ewbym2r1pk49x1d2lxdbiiavnqp25j2kh00hsg3koy0ppm620x5mhlmgl3rq5ci8',
14
15
  open_timeout: 10,
15
16
  response_timeout: 20
16
17
  }
@@ -20,7 +21,7 @@ module Resizing
20
21
  def teardown; end
21
22
 
22
23
  def test_remove_resizing_picture
23
- model = prepare_model
24
+ model = prepare_model TestModel
24
25
 
25
26
  VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
26
27
  SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
@@ -32,7 +33,7 @@ module Resizing
32
33
  end
33
34
 
34
35
  def test_do_not_raise_if_empty_column_is_removed
35
- model = prepare_model
36
+ model = prepare_model TestModel
36
37
 
37
38
  VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
38
39
  SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
@@ -42,23 +43,60 @@ module Resizing
42
43
  end
43
44
 
44
45
  def test_picture_url_return_correct_value_and_when_model_reloaded
45
- model = prepare_model
46
+ model = prepare_model TestModel
46
47
  model.save!
47
- assert_equal(expect_url, model.resizing_picture_url)
48
+ assert_equal("#{expect_url}/", model.resizing_picture_url)
48
49
 
49
50
  model.reload
50
- assert_equal(expect_url, model.resizing_picture_url)
51
+ assert_equal("#{expect_url}/", model.resizing_picture_url)
52
+ end
53
+
54
+ def test_picture_url_return_with_transform_strings
55
+ model = prepare_model TestModel
56
+ model.save!
57
+ assert_equal("#{expect_url}/c_fill,w_40,h_40", model.resizing_picture_url(:small))
58
+ end
59
+
60
+ def test_format_method_is_callable
61
+ model = prepare_model TestModel
62
+ model.save!
63
+ assert_nil model.resizing_picture.format
64
+ end
65
+
66
+ def test_format_method_is_return_jpg_with_overriden
67
+ model = prepare_model TestJPGModel
68
+ model.save!
69
+ model.resizing_picture.format
70
+ assert_equal('jpg', model.resizing_picture.format)
71
+ end
72
+
73
+ def test_url_is_return_default_url_as_nil
74
+ model = TestModel.new
75
+ model.save!
76
+ assert_nil model.resizing_picture_url
77
+ end
78
+
79
+ def test_url_is_return_default_url
80
+ model = TestModelWithDefaultURL.new
81
+ model.save!
82
+ assert_equal('http://example.com/test.jpg', model.resizing_picture_url)
83
+ end
84
+
85
+ def test_is_successful
86
+ model = prepare_model_with_tempfile TestModel
87
+ model.save!
88
+ # assert_equal('http://192.168.56.101:5000/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/28c49144-c00d-4cb5-8619-98ce95977b9c/v1Id850__tqNsnoGWWUibtIBZ5NgjV45M/', model.resizing_picture_url)
51
89
  end
52
90
 
53
91
  def expect_url
54
- 'http://192.168.56.101:5000/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/'+
55
- 'upload/images/28c49144-c00d-4cb5-8619-98ce95977b9c/v1Id850__tqNsnoGWWUibtIBZ5NgjV45M/c_limit,w_1000'
92
+ 'http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/'+
93
+ 'upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
56
94
  end
57
95
 
58
- def prepare_model
59
- VCR.use_cassette 'carrier_wave_test/save' do
96
+ def prepare_model model
97
+ VCR.use_cassette 'carrier_wave_test/save', record: :once do
60
98
  SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
61
- model = TestModel.new
99
+ model = model.new
62
100
  file = File.open('test/data/images/sample1.jpg', 'r')
63
101
  uploaded_file = ActionDispatch::Http::UploadedFile.new(
64
102
  filename: File.basename(file.path),
@@ -71,5 +109,16 @@ module Resizing
71
109
  end
72
110
  end
73
111
  end
112
+
113
+ def prepare_model_with_tempfile model
114
+ VCR.use_cassette 'carrier_wave_test/save', record: :once do
115
+ SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
116
+ model = model.new
117
+ file = File.open('test/data/images/sample1.jpg', 'r')
118
+ model.resizing_picture = file
119
+ return model
120
+ end
121
+ end
122
+ end
74
123
  end
75
124
  end
@@ -8,8 +8,8 @@ module Resizing
8
8
  # NOP
9
9
  @configuration_template = {
10
10
  host: 'http://192.168.56.101:5000',
11
- project_id: '098a2a0d-c387-4135-a071-1254d6d7e70a',
12
- secret_token: '4g1cshg2lq8j93ufhvqrpjswxmtjz12yhfvq6w79jpwi7cr7nnknoqgwzkwerbs6',
11
+ project_id: 'e06e710d-f026-4dcf-b2c0-eab0de8bb83f',
12
+ secret_token: 'ewbym2r1pk49x1d2lxdbiiavnqp25j2kh00hsg3koy0ppm620x5mhlmgl3rq5ci8',
13
13
  open_timeout: 10,
14
14
  response_timeout: 20
15
15
  }
@@ -19,7 +19,7 @@ module Resizing
19
19
  # NOP
20
20
  end
21
21
 
22
- def test_that_it_is_initialized
22
+ def test_is_initialized
23
23
  Resizing.configure = @configuration_template
24
24
 
25
25
  client = Resizing::Client.new
@@ -27,36 +27,37 @@ module Resizing
27
27
  assert_equal(client.config, Resizing.configure)
28
28
  end
29
29
 
30
- def test_that_it_is_initialized_with_configuration
30
+ def test_is_initialized_with_configuration
31
31
  config = Resizing::Configuration.new(@configuration_template)
32
32
  client = Resizing::Client.new(config)
33
33
  assert(!client.config.nil?)
34
34
  assert_equal(client.config, config)
35
35
  end
36
36
 
37
- def test_that_it_postable_file
37
+ def test_is_postable_file
38
38
  Resizing.configure = @configuration_template
39
39
 
40
40
  client = Resizing::Client.new
41
- VCR.use_cassette 'client/post' do
41
+ VCR.use_cassette 'client/post', record: :once do
42
42
  f = File.open('test/data/images/sample1.jpg', 'r')
43
43
  r = client.post(f, content_type: 'image/jpeg')
44
- assert_equal(r['id'], 'bfdaf2b3-7ec5-41f4-9caa-d53247dd9666')
44
+ assert_equal(r['id'], '87263920-2081-498e-a107-9625f4fde01b')
45
45
  assert_equal(r['project_id'], Resizing.configure.project_id)
46
46
  assert_equal(r['content_type'], 'image/jpeg')
47
47
  assert(!r['latest_version_id'].nil?)
48
48
  assert(!r['latest_etag'].nil?)
49
49
  assert(!r['created_at'].nil?)
50
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')
51
+ assert_equal(r['public_id'], '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo')
52
+ assert_equal(r['filename'], 'sample1.jpg')
52
53
  end
53
54
  end
54
55
 
55
- def test_that_it_putable_file
56
+ def test_is_putable_file
56
57
  Resizing.configure = @configuration_template
57
58
 
58
59
  client = Resizing::Client.new
59
- VCR.use_cassette 'client/put' do
60
+ VCR.use_cassette 'client/put', record: :once do
60
61
  f = File.open('test/data/images/sample1.jpg', 'r')
61
62
  name = 'AWEaewfAreaweFAFASfwe'
62
63
  r = client.put(name, f, content_type: 'image/jpeg')
@@ -69,7 +70,32 @@ module Resizing
69
70
  assert(!r['updated_at'].nil?)
70
71
  assert_equal(
71
72
  r['public_id'],
72
- "/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/#{name}/v6Ew3HmDAYfb3NMRdLxR45i_gXMbLlGyi"
73
+ "/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/#{name}/vfztekhN_WoeXo8ZkCZ4i5jcQvmPpZewR"
74
+ )
75
+ end
76
+ end
77
+
78
+ def test_get_the_metadata
79
+ # TODO
80
+
81
+ Resizing.configure = @configuration_template
82
+
83
+ client = Resizing::Client.new
84
+ VCR.use_cassette 'client/metadata', record: :once do
85
+ name = '87263920-2081-498e-a107-9625f4fde01b'
86
+ r = client.metadata(name)
87
+ assert_equal(r['id'], name)
88
+ assert_equal(r['project_id'], Resizing.configure.project_id)
89
+ assert_equal(r['content_type'], 'image/jpeg')
90
+ assert(!r['latest_version_id'].nil?)
91
+ assert(!r['latest_etag'].nil?)
92
+ assert(!r['created_at'].nil?)
93
+ assert(!r['updated_at'].nil?)
94
+ assert(!r['height'].nil?)
95
+ assert(!r['width'].nil?)
96
+ assert_equal(
97
+ r['public_id'],
98
+ "/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo"
73
99
  )
74
100
  end
75
101
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Resizing
6
+ class PublicIdTest < Minitest::Test
7
+ def setup
8
+ @project_id = '098a2a0d-c387-4135-a071-1254d6d7e70a'
9
+ @image_id = '28c49144-c00d-4cb5-8619-98ce95977b9c'
10
+ @version = '1Id850q34fgsaer23w'
11
+ @public_id_as_string = "/projects/#{@project_id}/upload/images/#{@image_id}/v#{@version}"
12
+ end
13
+
14
+ def teardown; end
15
+
16
+ def test_expect_equal_project_id
17
+ public_id = Resizing::PublicId.new @public_id_as_string
18
+ assert_equal @project_id, public_id.project_id
19
+ end
20
+
21
+ def test_expect_equal_image_id
22
+ public_id = Resizing::PublicId.new @public_id_as_string
23
+ assert_equal @image_id, public_id.image_id
24
+ end
25
+
26
+ def test_expect_equal_version
27
+ public_id = Resizing::PublicId.new @public_id_as_string
28
+ assert_equal @version, public_id.version
29
+ end
30
+
31
+ def test_expect_equal_identifier
32
+ public_id = Resizing::PublicId.new @public_id_as_string
33
+ assert_equal @public_id_as_string.gsub(/\/v.*$/, ''), public_id.identifier
34
+ end
35
+
36
+ def test_expect_equal_public_id
37
+ public_id = Resizing::PublicId.new @public_id_as_string
38
+ assert_equal @public_id_as_string, public_id.to_s
39
+ end
40
+ end
41
+ end
@@ -32,17 +32,49 @@ ActiveRecord::Base.establish_connection(
32
32
 
33
33
  ActiveRecord::Schema.define do
34
34
  self.verbose = false
35
- connection.execute 'drop table if exists test_models'
36
35
 
37
- create_table :test_models do |t|
38
- t.string :resizing_picture, null: true, default: nil
36
+ %i(test_models test_jpg_models test_model_with_default_urls).each do |model_name|
37
+ connection.execute "drop table if exists #{model_name}"
38
+
39
+ create_table model_name do |t|
40
+ t.string :resizing_picture, null: true, default: nil
41
+ end
39
42
  end
40
43
  end
41
44
 
42
45
  class ResizingUploader < CarrierWave::Uploader::Base
43
46
  include Resizing::CarrierWave
44
47
 
48
+ version :small do
49
+ process resize_to_fill: [40, 40]
50
+ end
51
+
52
+ process resize_to_limit: [1000]
53
+ end
54
+
55
+ class ResizingJPGUploader < CarrierWave::Uploader::Base
56
+ include Resizing::CarrierWave
57
+
45
58
  process resize_to_limit: [1000]
59
+
60
+ # override Resizing::CarrierWave#default_format
61
+ def default_format
62
+ 'jpg'
63
+ end
64
+
65
+ def default_url
66
+ 'http://example.com/test.jpg'
67
+ end
68
+ end
69
+
70
+ class ResizingUploaderWithDefaultURL < CarrierWave::Uploader::Base
71
+ include Resizing::CarrierWave
72
+
73
+ process resize_to_limit: [1000]
74
+
75
+ def default_url
76
+ 'http://example.com/test.jpg'
77
+ end
46
78
  end
47
79
 
48
80
  class TestModel < ::ActiveRecord::Base
@@ -50,3 +82,15 @@ class TestModel < ::ActiveRecord::Base
50
82
 
51
83
  mount_uploader :resizing_picture, ResizingUploader
52
84
  end
85
+
86
+ class TestJPGModel < ::ActiveRecord::Base
87
+ extend CarrierWave::Mount
88
+
89
+ mount_uploader :resizing_picture, ResizingJPGUploader
90
+ end
91
+
92
+ class TestModelWithDefaultURL < ::ActiveRecord::Base
93
+ extend CarrierWave::Mount
94
+
95
+ mount_uploader :resizing_picture, ResizingUploaderWithDefaultURL
96
+ end