resizing 0.1.3 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc0185b74fd4488a798b0a5da33379e54b9d95081ac7e1160a3d6e41aadc306e
4
- data.tar.gz: b21eaae81ea5fc991ddec3fdefca7d73ec35b9c008057407547552c045b12c22
3
+ metadata.gz: a68f3747dc45de527ea32a40bb2586783a0ea14d76717bd85186aae178ef18bd
4
+ data.tar.gz: 5ecebffc8b0eb31c22e3bb408eea3d99db21b90bcdba5178e74f1f0704261ed5
5
5
  SHA512:
6
- metadata.gz: 6e13ddf3fdb99165d35d73bd580fd45c316e93bc1e25769fc9cb7c6f28ed98dfdce57b22c15ee12dabbaeff27bc0156c3833adb79857853f41974ec0773b5ca5
7
- data.tar.gz: e4b7d41e6b4e58fba560a73876913564c532101dc54c777ea994adf529fbc76088bb5ecd13f7dfa178fc198553abc504b8451b91141dc900b335737169438d13
6
+ metadata.gz: 233c87e03b19be35e89d373a4456aa81cdd82c110eea4cff47f2d8498a852ed203c0443567f0afb8b6d85bd419da49b6accb5e52038e823beddf40d88f84e94d
7
+ data.tar.gz: 66f18b1648e93a2cbdf73a2a457b9e2591262b795dfbebd232cd0388ca7787a755068a85c84f763f1c28fa7e10c61a4169915aff35184d1f602c6498cca8f63e
@@ -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
@@ -109,12 +109,21 @@ module Resizing
109
109
 
110
110
  # rubocop:disable Metrics/AbcSize
111
111
  def transform_string_from(processor)
112
- case processor.first
112
+ action = processor.first
113
+ value = processor.second
114
+
115
+ case action
113
116
  when :resize_to_fill, :resize_to_limit, :resize_to_fit
114
- name = processor.first.to_s.gsub(/resize_to_/, '')
115
- { c: name, w: processor.second.first, h: processor.second.second }
117
+ name = action.to_s.gsub(/resize_to_/, '')
118
+ { c: name, w: value.first, h: value.second }
119
+ when :transformation
120
+ result = {}
121
+ result[:q] = value[:quality] if value[:quality]
122
+ result[:f] = value[:fetch_format] if value[:fetch_format]
123
+ result[:f] = value[:format] if value[:format]
124
+ result
116
125
  else
117
- raise NotImplementedError, "#{processor.first} is not supported. #{processor.inspect}"
126
+ raise NotImplementedError, "#{action} is not supported. #{processor.inspect}"
118
127
  end.map do |key, value|
119
128
  next nil if value.nil?
120
129
 
@@ -28,6 +28,8 @@ module Resizing
28
28
 
29
29
  def delete
30
30
  public_id = model.send :read_attribute, serialization_column
31
+ return if public_id.nil? # do nothing
32
+
31
33
  # TODO:
32
34
  # refactoring
33
35
  separted = Resizing.separate_public_id(public_id)
@@ -11,7 +11,7 @@ module Resizing
11
11
  # }
12
12
  # client = Resizing::Client.new(options)
13
13
  # file = File.open('sample.jpg', 'r')
14
- # response = client.post(file)
14
+ # response = client.post(file, content_type: 'image/jpeg')
15
15
  # {
16
16
  # "id"=>"fde443bb-0b29-4be2-a04e-2da8f19716ac",
17
17
  # "project_id"=>"098a2a0d-0000-0000-0000-000000000000",
@@ -16,7 +16,7 @@ module Resizing
16
16
  #++
17
17
  class Configuration
18
18
  attr_reader :host, :project_id, :secret_token, :open_timeout, :response_timeout, :enable_mock
19
- DEFAULT_HOST = 'https://www.resizing.net'
19
+ DEFAULT_HOST = 'https://img.resizing.net'
20
20
  DEFAULT_OPEN_TIMEOUT = 2
21
21
  DEFAULT_RESPONSE_TIMEOUT = 10
22
22
 
@@ -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.1.3'
4
+ VERSION = '0.3.2'
5
5
  end
@@ -23,12 +23,12 @@ 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) }
30
30
  spec.require_paths = ['lib']
31
- spec.add_runtime_dependency 'faraday', '~> 0.13'
31
+ spec.add_runtime_dependency 'faraday', '~> 1.0.1'
32
32
  spec.add_development_dependency 'byebug'
33
33
  spec.add_development_dependency 'carrierwave'
34
34
  spec.add_development_dependency 'fog-aws'
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency 'minitest-ci'
37
37
  spec.add_development_dependency 'mysql2'
38
38
  spec.add_development_dependency 'pry-byebug'
39
- spec.add_development_dependency 'rails', '= 5.2.3'
39
+ spec.add_development_dependency 'rails', '~> 5.2.3'
40
40
  spec.add_development_dependency 'rubocop'
41
41
  spec.add_development_dependency 'timecop'
42
42
  spec.add_development_dependency 'vcr'
@@ -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
@@ -0,0 +1,75 @@
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_remove_resizing_picture
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_do_not_raise_if_empty_column_is_removed
35
+ model = prepare_model
36
+
37
+ VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
38
+ SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
39
+ model.resizing_picture.remove!
40
+ end
41
+ end
42
+ end
43
+
44
+ def test_picture_url_return_correct_value_and_when_model_reloaded
45
+ model = prepare_model
46
+ model.save!
47
+ assert_equal(expect_url, model.resizing_picture_url)
48
+
49
+ model.reload
50
+ assert_equal(expect_url, model.resizing_picture_url)
51
+ end
52
+
53
+ 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'
56
+ end
57
+
58
+ def prepare_model
59
+ VCR.use_cassette 'carrier_wave_test/save' do
60
+ SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
61
+ model = TestModel.new
62
+ file = File.open('test/data/images/sample1.jpg', 'r')
63
+ uploaded_file = ActionDispatch::Http::UploadedFile.new(
64
+ filename: File.basename(file.path),
65
+ type: 'image/jpeg',
66
+ tempfile: file
67
+ )
68
+
69
+ model.resizing_picture = uploaded_file
70
+ return model
71
+ end
72
+ end
73
+ end
74
+ end
75
+ 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