resizing 0.1.0 → 0.3.1

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: 46b4c727294bdd92f33e9fa5cf32ee2cd320987a1b2d65dccba240a5a776f0b4
4
- data.tar.gz: c9c7b2a9b568c216804afb3f6108e9190644df0ed241eee73a600fe228982eca
3
+ metadata.gz: 9c99cc84265ede5a14f6045b73ac9cc06da7e8a71ddb4f632a4c2bcf05f12b82
4
+ data.tar.gz: e0ee502a4fe4d5b30942e50a11d2dff1c17806a3a8f2090e87f70823e3d69e5d
5
5
  SHA512:
6
- metadata.gz: d8fc3430aef73a2b20adbc4d6c649c62ff467944e03db06b977d1dc517205644573e5ab87eb891a3539ae9776e1f4adbb3b9fded9eb782b203c244892ad3e2be
7
- data.tar.gz: e5f0122b6a5d06f04b55ef79f9da102f603cd397941135787ff1d121afedcbc4d18f6d72f472e4552daa7f11099c9dd493a175ef1763145c0828b72073d215b3
6
+ metadata.gz: 75ac29e319053ea1ee9e4ec8c50955c972431ca1df986e720681defe3930fec8d688787902ad6e07d35564d364bb23b5e919b5e369029301e16a2a41f891211d
7
+ data.tar.gz: b2a8a478965f384b130de2dc9e757f5bf449d28d2990a8d153578bb9d5685b3f84fb98760494a33a72693455b085a3aed390ff0e6c93e56723c2ebb8fd89ce2d
@@ -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
 
@@ -33,7 +33,7 @@ module Resizing
33
33
  separted = Resizing.separate_public_id(public_id)
34
34
  image_id = separted[:image_id]
35
35
  resp = client.delete(image_id)
36
- if resp.nil? # 404 not found
36
+ if resp['error'] == 'ActiveRecord::RecordNotFound' # 404 not found
37
37
  model.send :write_attribute, serialization_column, nil
38
38
  return
39
39
  end
@@ -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",
@@ -28,6 +28,7 @@ module Resizing
28
28
  # to use standard constants
29
29
  HTTP_STATUS_OK = 200
30
30
  HTTP_STATUS_CREATED = 201
31
+ HTTP_STATUS_NOT_FOUND = 404
31
32
 
32
33
  attr_reader :config
33
34
  def initialize(*attrs)
@@ -58,7 +59,7 @@ module Resizing
58
59
  request.headers['X-ResizingToken'] = config.generate_auth_header
59
60
  end
60
61
 
61
- result = handle_response(response)
62
+ result = handle_create_response(response)
62
63
  result
63
64
  end
64
65
 
@@ -76,7 +77,7 @@ module Resizing
76
77
  request.headers['X-ResizingToken'] = config.generate_auth_header
77
78
  end
78
79
 
79
- result = handle_response(response)
80
+ result = handle_create_response(response)
80
81
  result
81
82
  end
82
83
 
@@ -87,7 +88,7 @@ module Resizing
87
88
  request.headers['X-ResizingToken'] = config.generate_auth_header
88
89
  end
89
90
 
90
- result = handle_response(response)
91
+ result = handle_delete_response(response)
91
92
  result
92
93
  end
93
94
 
@@ -134,14 +135,25 @@ module Resizing
134
135
  raise ArgumentError, "need options[:content_type] for #{options.inspect}" unless options[:content_type]
135
136
  end
136
137
 
137
- def handle_response(response)
138
+ def handle_create_response(response)
138
139
  raise APIError, "no response is returned" if response.nil?
139
140
 
140
141
  case response.status
141
142
  when HTTP_STATUS_OK, HTTP_STATUS_CREATED
142
143
  JSON.parse(response.body)
143
144
  else
144
- raise APIError, "invalid http status code #{resp.status}"
145
+ raise APIError, "invalid http status code #{response.status}"
146
+ end
147
+ end
148
+
149
+ def handle_delete_response(response)
150
+ raise APIError, "no response is returned" if response.nil?
151
+
152
+ case response.status
153
+ when HTTP_STATUS_OK, HTTP_STATUS_CREATED, HTTP_STATUS_NOT_FOUND
154
+ JSON.parse(response.body)
155
+ else
156
+ raise APIError, "invalid http status code #{response.status}"
145
157
  end
146
158
  end
147
159
  end
@@ -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.0'
4
+ VERSION = '0.3.1'
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,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