light_resizer 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b714088c4264d928eb641b3606d016cea6aef7f
4
+ data.tar.gz: b6d370e74c2d727093afacb90c83cf1c0d10ba53
5
+ SHA512:
6
+ metadata.gz: 17ee6e40bdd572258ab8b8874697b96787ce44a09e2ddd988922f2c7aec9f9363d2aae7180836b3ce5d6af78a85ba858f0c29694cb2d8fd74a805fd0efbcde3d
7
+ data.tar.gz: d81e8d8a9a19099124ad5ac07ad5e45327e60987c5ac2508ffbd9e67a9e83767e31bed160da0ea75ef9d4386929d442597e3926a1ff8eb361dbcf7b3eee3727a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in light_resizer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Denis Sergienko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ light-resizer
2
+ =============
3
+
4
+ Rack middleware for image resizer. Can be integrated with CarrierWave.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ require "light_resizer/version"
2
+ require 'light_resizer/middleware'
3
+ require 'light_resizer/carrierwave_resize'
4
+ require 'mini_magick'
5
+
6
+ module LightResizer
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,8 @@
1
+ module LightResizer
2
+ module CarrierWaveResize
3
+ def resize(width, height)
4
+ start = "/image/#{width}x#{height}"
5
+ File.join(start, url)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,98 @@
1
+ module LightResizer
2
+ class Middleware
3
+ def initialize(app, root, folder='public')
4
+ @app_root = root
5
+ @public_folder = folder
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ @path = nil
11
+ @splited_path = nil
12
+ @env = env
13
+ handle
14
+
15
+ status, headers, response = @app.call(@env)
16
+
17
+ [status, headers, response]
18
+ end
19
+
20
+ private
21
+
22
+ # Handle request.
23
+ # resizes images and
24
+ # modificates env
25
+ def handle
26
+ if is_image_path?
27
+ resize! unless image_exist
28
+
29
+ modify_path!
30
+ end
31
+ end
32
+
33
+ # creates new resized verion of requested image
34
+ def resize!
35
+ image = MiniMagick::Image.open(original_path)
36
+ image.combine_options do |c|
37
+ c.resize dimensions+'^'
38
+ c.unsharp '0x1'
39
+ c.add_command('extent', dimensions)
40
+ c.gravity 'center'
41
+ end
42
+ image.write resized_image_filepath
43
+ end
44
+
45
+ # {Bool} returns true if image exist
46
+ def image_exist
47
+ File.exist?(resized_image_filepath)
48
+ end
49
+
50
+ # Modifies path to resized image
51
+ def modify_path!
52
+ @env['PATH_INFO'] = resized_image_path
53
+ end
54
+
55
+ # {String} returns site path to resized image
56
+ # /image/200x200/.../image.png => /.../200x200_image.png
57
+ def resized_image_path
58
+ dir = File.dirname(File.join(splited_path[3..-1]))
59
+ filename = File.basename(File.join(splited_path[3..-1]))
60
+
61
+ dir = '/' if dir == '.'
62
+ filename = dimensions + '_' + filename
63
+
64
+ File.join(dir, filename)
65
+ end
66
+
67
+ # {String} returns full path to resized image
68
+ # /image/200x200/.../image.png => {APP_ROOT}/{@public_folder}/.../200x200_image.png
69
+ def resized_image_filepath
70
+ File.join(@app_root, @public_folder, resized_image_path)
71
+ end
72
+
73
+ # {String} returns full path of image that will be resized
74
+ def original_path
75
+ File.join(@app_root, @public_folder, splited_path[3..-1])
76
+ end
77
+
78
+ # {String} requested path
79
+ def path
80
+ @path ||= @env['PATH_INFO']
81
+ end
82
+
83
+ # {Bool} returns true if request path begins with 'image'
84
+ def is_image_path?
85
+ splited_path[1] == 'image'
86
+ end
87
+
88
+ # {Array} returns required dimensions of image
89
+ # '/image/200x200/...'' => '200x200'
90
+ def dimensions
91
+ splited_path[2]
92
+ end
93
+
94
+ def splited_path
95
+ @splited_path ||= path.split('/')
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,3 @@
1
+ module LightResizer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'light_resizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "light_resizer"
8
+ spec.version = LightResizer::VERSION
9
+ spec.authors = ["Denis Sergienko"]
10
+ spec.email = ["olol.toor@gmail.com"]
11
+ spec.summary = %q{Tiny rack middleware for image resizing.}
12
+ spec.description = %q{}
13
+ spec.homepage = "http://github.com/Rademade/light_resizer"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "mini_magick", "~> 3.7.0"
22
+ spec.add_dependency "rack", "~> 1.5.2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
Binary file
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe LightResizer::Middleware do
4
+ let(:app) { AppEmulator.new }
5
+ let(:resizer) { described_class.new(app, ROOT, 'fixtures') }
6
+ let(:env) { { 'PATH_INFO' => '/image/20x20/avatar.png' } }
7
+ let(:path) { File.join(ROOT, 'fixtures', '20x20_avatar.png') }
8
+
9
+ after do
10
+ File.delete(path)
11
+ end
12
+
13
+ it 'should create resized image' do
14
+ resizer.call(env)
15
+
16
+ expect(File.exist?(path)).to eq(true)
17
+ end
18
+
19
+ it 'should return right path' do
20
+ resizer.call(env)
21
+
22
+ expect(app.env['PATH_INFO']).to eq('/20x20_avatar.png')
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ require 'light_resizer'
2
+ ROOT = File.dirname(__FILE__)
3
+
4
+ class AppEmulator
5
+ attr_reader :env
6
+ def call(env)
7
+ @env = env
8
+ end
9
+ end
10
+
11
+
12
+ # This file was generated by the `rspec --init` command. Conventionally, all
13
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
14
+ # Require this file using `require "spec_helper"` to ensure that it is only
15
+ # loaded once.
16
+ #
17
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+ RSpec.configure do |config|
19
+ config.treat_symbols_as_metadata_keys_with_true_values = true
20
+ config.run_all_when_everything_filtered = true
21
+ config.filter_run :focus
22
+
23
+ # Run specs in random order to surface order dependencies. If you find an
24
+ # order dependency and want to debug it, you can fix the order by providing
25
+ # the seed, which is printed after each run.
26
+ # --seed 1234
27
+ config.order = 'random'
28
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe LightResizer::Middleware do
4
+ let(:app) { AppEmulator.new }
5
+ let(:resizer) { described_class.new(app, ROOT, 'fixtures') }
6
+ let(:path) { File.join(ROOT, 'fixtures', '20x20_avatar.png') }
7
+
8
+ before(:each) do
9
+ resizer.call(env)
10
+ end
11
+
12
+ context 'path methods' do
13
+ after do
14
+ File.delete(path)
15
+ end
16
+
17
+ let(:env) { { 'PATH_INFO' => '/image/20x20/avatar.png' } }
18
+
19
+ it { expect(resizer.send(:splited_path)).to eq(['', 'image', '20x20', 'avatar.png']) }
20
+
21
+ it { expect(resizer.send(:dimensions)).to eq('20x20') }
22
+
23
+ it { expect(resizer.send(:is_image_path?)).to eq(true) }
24
+
25
+ it { expect(resizer.send(:path)).to eq('/image/20x20/avatar.png') }
26
+
27
+ it { expect(resizer.send(:original_path)).to eq(File.join(ROOT, 'fixtures', 'avatar.png')) }
28
+
29
+ it { expect(resizer.send(:resized_image_filepath)).to eq(path) }
30
+
31
+ it { expect(resizer.send(:resized_image_path)).to eq('/20x20_avatar.png') }
32
+
33
+ it { expect(resizer.send(:image_exist)).to eq(true) }
34
+ end
35
+
36
+ context 'with non image path' do
37
+
38
+ let(:env) { { 'PATH_INFO' => '/lalala/20x20/avatar.png' } }
39
+
40
+ it { expect(resizer.send(:is_image_path?)).to eq(false) }
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: light_resizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Denis Sergienko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mini_magick
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - olol.toor@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/light_resizer.rb
97
+ - lib/light_resizer/carrierwave_resize.rb
98
+ - lib/light_resizer/middleware.rb
99
+ - lib/light_resizer/version.rb
100
+ - light_resizer.gemspec
101
+ - spec/fixtures/avatar.png
102
+ - spec/integration/middleware_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/unit/middleware_spec.rb
105
+ homepage: http://github.com/Rademade/light_resizer
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Tiny rack middleware for image resizing.
129
+ test_files:
130
+ - spec/fixtures/avatar.png
131
+ - spec/integration/middleware_spec.rb
132
+ - spec/spec_helper.rb
133
+ - spec/unit/middleware_spec.rb
134
+ has_rdoc: