light_resizer 0.0.2 → 0.1.1

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
  SHA1:
3
- metadata.gz: c5e96f10cbc75a2c4f51fe257b25a9b33285d79a
4
- data.tar.gz: 5bcdb15b74a9af11c1b3f7ef8df6dff97ec7c177
3
+ metadata.gz: 48f8da7f8cc63574dad6a1a0a3ef30299f576535
4
+ data.tar.gz: 293fef11bdb1a33eb4e3b808a7155e3fb1d779af
5
5
  SHA512:
6
- metadata.gz: 6e0565d872d65fe21da6fbaab44208d9b5c347f64c58250acdc8a6a70fa264d1bd00606da62dac1fb763c9010be3b181e4c75f816bea22e5e4c6c9c21b267876
7
- data.tar.gz: 22bf31bc915d97b05fde9db94fa5d903860fbba7b95b2e931190be791d74787d9fd96e5f400a7f50c680b869892241f3621bcbaaea72df8007972daf8c8556dc
6
+ metadata.gz: 33743ca407ad1198d4ac922543778f8579c0559c2169cf81f14d63820900a4b57530fcf1c661a071b92831b45d19534e23eea5a8da8c2fa43cdd8464f9bda442
7
+ data.tar.gz: 2806c2280ec857358c7899f056442a5863385a1c2d5f1c5a8454a0dd19688443b806c4877310e0f1435910b27e2702b46fa5f86e91e79af0f0cb5d9576505f59
@@ -1,13 +1,24 @@
1
1
  module LightResizer
2
2
  module CarrierWaveResize
3
3
  def resize(width, height)
4
- start = "/resize_image/#{width}x#{height}"
5
- File.join(start, url)
4
+ filename_prefix = "#{width}x#{height}_"
5
+ resized_image_path(filename_prefix)
6
6
  end
7
7
 
8
8
  def resize_crop(width, height)
9
- start = "/resize_image_crop/#{width}x#{height}"
10
- File.join(start, url)
9
+ filename_prefix = "#{width}x#{height}_crop_"
10
+ resized_image_path(filename_prefix)
11
11
  end
12
+
13
+ private
14
+ def resized_image_path(prefix)
15
+ if url
16
+ path = File.dirname(url)
17
+ filename = File.basename(url)
18
+ new_filname = prefix + filename
19
+
20
+ File.join(path, 'light_resize', new_filname)
21
+ end
22
+ end
12
23
  end
13
24
  end
@@ -3,9 +3,10 @@ module LightResizer
3
3
  class ImageLoader
4
4
  class ResizeImage
5
5
 
6
- STORE_RESIZE_DIR = 'resize' #todo move to config option
6
+ STORE_RESIZE_DIR = 'light_resize' #todo move to config option
7
7
 
8
8
  attr_accessor :original_filename, :original_relative_dir, :resize_prefix
9
+ attr_reader :root_dir
9
10
 
10
11
  def initialize(root_dir)
11
12
  @root_dir = root_dir
@@ -16,9 +16,9 @@ module LightResizer
16
16
  def call(env)
17
17
  @path.request_path = env['PATH_INFO']
18
18
 
19
- env['PATH_INFO'] = resize_image_path if @path.image_path? and resize_request?
19
+ resize if (@path.image_path? and resize_request?)
20
20
 
21
- @app.call( env )
21
+ Rack::File.new(@image_loader.resized.root_dir).call(env)
22
22
  end
23
23
 
24
24
  def resize_request?
@@ -30,11 +30,10 @@ module LightResizer
30
30
 
31
31
  private
32
32
 
33
- def resize_image_path
33
+ def resize
34
34
  unless @image_loader.resized_image_exist?
35
35
  @resizer.resize(@path.dimensions, @image_loader.original_path, @image_loader.resize_path, @path.crop_path?)
36
36
  end
37
- @image_loader.resized_image_relative_path
38
37
  end
39
38
 
40
39
  end
@@ -3,6 +3,8 @@ module LightResizer
3
3
  class Middleware
4
4
  class Path
5
5
 
6
+ PREFIX_REGEXP = /^[0-9]+x[0-9]+(_crop)?_/
7
+
6
8
  attr_reader :request_path
7
9
 
8
10
  def request_path=(path)
@@ -12,35 +14,47 @@ module LightResizer
12
14
 
13
15
  # {Bool} returns true if request path begins with 'image'
14
16
  def image_path?
15
- segments[1].start_with?('resize_image')
17
+ request_dir.end_with?('light_resize')
16
18
  end
17
19
 
18
20
 
19
21
  # {Bool} returns true if image should be croped on resize
20
22
  def crop_path?
21
- segments[1].end_with?('crop')
23
+ prefix.end_with?('crop')
22
24
  end
23
25
 
24
26
  # {String} last part of request – relative path
25
27
  def image_path
26
- '/' + segments[3..-1].join('/')
28
+ dir = File.expand_path('..', request_dir)
29
+ File.join(dir, original_filename)
27
30
  end
28
31
 
29
- # {Array} returns required dimensions of image. Like a 200x200
30
- def dimensions
31
- #todo validate params!
32
- segments[2]
33
- end
34
32
 
35
33
  # {String} returns prefix of resized image name
36
- # image.png => 200x200_crop_image.png
34
+ # 200x200_crop_image.png => 200x200_crop
37
35
  def prefix
38
- crop_prefix = crop_path? ? '_crop' : ''
36
+ filename[PREFIX_REGEXP].chop if image_path?
37
+ end
38
+
39
+ def dimensions
40
+ prefix.split('_').first
41
+ end
42
+
43
+ private
44
+
45
+ def request_dir
46
+ File.dirname(request_path)
47
+ end
48
+
49
+ def original_filename
50
+ filename.gsub(PREFIX_REGEXP, '')
51
+ end
39
52
 
40
- dimensions + crop_prefix
53
+ def filename
54
+ File.basename(request_path)
41
55
  end
42
56
 
43
- def segments
57
+ def name_segments
44
58
  @segments ||= request_path.split('/')
45
59
  end
46
60
 
@@ -1,3 +1,3 @@
1
1
  module LightResizer
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -3,25 +3,25 @@ require 'spec_helper'
3
3
  describe LightResizer::Middleware do
4
4
  let(:app) { AppEmulator.new }
5
5
  let(:resizer) { described_class.new(app, ROOT, 'fixtures') }
6
- let(:env) { { 'PATH_INFO' => '/resize_image/100x160/avatar.png' } }
6
+ let(:env) { { 'PATH_INFO' => resize_relative_path} }
7
7
 
8
- let(:resize_relative_path) { '/resize/100x160_avatar.png' } #todo depends for many constant
8
+ let(:resize_relative_path) { '/light_resize/150x100_avatar.png' } #todo depends for many constant
9
9
  let(:resize_full_path) { File.join(ROOT, 'fixtures', resize_relative_path) }
10
10
 
11
- after do
12
- File.delete resize_full_path
11
+ after(:each) do
12
+ File.delete(resize_full_path)
13
13
  end
14
14
 
15
- it 'should create resized image' do
15
+ it 'should approve resize request' do
16
16
  resizer.call(env)
17
17
 
18
- expect(resizer.resize_request?).to eq(true)
18
+ expect(resizer.resize_request?).to be_true
19
19
  end
20
20
 
21
- it 'should return right path' do
21
+ it 'should create resized image' do
22
+ expect(File.exist?(resize_full_path)).to be_false
22
23
  resizer.call(env)
23
-
24
- expect(app.env['PATH_INFO']).to eq( resize_relative_path )
24
+ expect(File.exist?(resize_full_path)).to be_true
25
25
  end
26
26
 
27
27
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'coveralls'
2
2
  Coveralls.wear!
3
3
 
4
+ require 'rack'
4
5
  require 'light_resizer'
5
6
  ROOT = File.dirname(__FILE__)
6
7
 
@@ -22,7 +22,7 @@ describe LightResizer::ImageLoader::ResizeImage do
22
22
  }
23
23
 
24
24
  it {
25
- expected_path = File.join(store_dir, resized_image.resize_prefix_dir, resized_image.filename)
25
+ expected_path = File.join(store_dir, 'light_resize', resized_image.filename)
26
26
  expect(resized_image.send(:relative_path)).to eq( expected_path )
27
27
  }
28
28
 
@@ -32,7 +32,7 @@ describe LightResizer::ImageLoader::ResizeImage do
32
32
  }
33
33
 
34
34
  it {
35
- expected_path = File.join(root_dir, store_dir, resized_image.resize_prefix_dir )
35
+ expected_path = File.join(root_dir, store_dir, 'light_resize' )
36
36
  expect(resized_image.send(:dir_path)).to eq( expected_path )
37
37
  }
38
38
 
@@ -14,7 +14,7 @@ describe LightResizer::ImageLoader do
14
14
 
15
15
  it { expect(image_loader.send(:original_image_exist?)).to eq(false) }
16
16
 
17
- it { expect(image_loader.send(:resized_image_relative_path)).to eq('/some/dir/resize/20x20_image.png') }
17
+ it { expect(image_loader.send(:resized_image_relative_path)).to eq('/some/dir/light_resize/20x20_image.png') }
18
18
 
19
19
  it { expect(image_loader.send(:resized_image_exist?)).to eq(false) }
20
20
 
@@ -7,10 +7,10 @@ describe LightResizer::Middleware::Path do
7
7
  context 'valid path methods' do
8
8
 
9
9
  before(:each) do
10
- path.request_path = '/resize_image/150x150/some_dir/image.png'
10
+ path.request_path = '/some_dir/light_resize/150x150_image.png'
11
11
  end
12
12
 
13
- it { expect(path.image_path?).to eq(true) }
13
+ it { expect(path.image_path?).to be_true }
14
14
 
15
15
  it { expect(path.image_path).to eq('/some_dir/image.png') }
16
16
 
@@ -22,11 +22,13 @@ describe LightResizer::Middleware::Path do
22
22
 
23
23
  context 'resize with crop' do
24
24
  before(:each) do
25
- path.request_path = '/resize_image_crop/150x150/some_dir/image.png'
25
+ path.request_path = '/some_dir/light_resize/150x150_crop_image.png'
26
26
  end
27
27
 
28
28
  it { expect(path.crop_path?).to be_true }
29
29
 
30
+ it { expect(path.image_path).to eq('/some_dir/image.png') }
31
+
30
32
  it { expect(path.dimensions).to eq('150x150') }
31
33
 
32
34
  it { expect(path.prefix).to eq('150x150_crop') }
@@ -38,7 +40,7 @@ describe LightResizer::Middleware::Path do
38
40
  path.request_path = '/asdada/123acad/some_dir/imdage.png'
39
41
  end
40
42
 
41
- it { expect(path.image_path?).to eq(false) }
43
+ it { expect(path.image_path?).to be_false }
42
44
 
43
45
  end
44
46
 
@@ -4,7 +4,7 @@ describe LightResizer::Middleware::Resizer do
4
4
 
5
5
  let(:resizer) { described_class.new }
6
6
  let(:image_path) { File.join(ROOT, 'fixtures', 'avatar.png') }
7
- let(:resize_image_path) { File.join(ROOT, 'fixtures', 'resize', image_name) }
7
+ let(:resize_image_path) { File.join(ROOT, 'fixtures', 'light_resize', image_name) }
8
8
 
9
9
  context 'resize' do
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light_resizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Sergienko