rack-img-sizes 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.
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/.travis.yml ADDED
@@ -0,0 +1 @@
1
+ language: ruby
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-img-sizes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Piotr Usewicz
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,29 @@
1
+ # Rack::Img::Sizes
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-img-sizes'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-img-sizes
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1 @@
1
+ require 'rack/img_sizes'
@@ -0,0 +1,56 @@
1
+ require 'rack'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require File.expand_path("../img_sizes/uri", __FILE__)
5
+
6
+ module Rack
7
+ class ImgSizes
8
+ IDENTIFY_COMMAND = `which identify`.chomp
9
+
10
+ def initialize(app)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ @env = env
16
+ status, headers, response = @app.call(env)
17
+
18
+ new_response = response.map do |part|
19
+ process_part(part)
20
+ end
21
+
22
+ [status, headers, new_response]
23
+ end
24
+
25
+ private
26
+ def process_part(part)
27
+ doc = Nokogiri::HTML.parse(part)
28
+ doc.css("img").each do |img|
29
+ old_img = img.dup.to_s
30
+ if src = img["src"]
31
+ uri = ::Rack::ImgSizes::URI.new(@env, src)
32
+ width, height = *dimensions(uri.to_s)
33
+ img.set_attribute("width" , width.to_s)
34
+ img.set_attribute("height" , height.to_s)
35
+
36
+ part.sub!(old_img, img.to_s)
37
+ end
38
+ end
39
+ part
40
+ end
41
+
42
+ def dimensions(src)
43
+ sizes = []
44
+ Tempfile.open("img") do |f|
45
+ open(src) do |img|
46
+ f.write img.read
47
+ end
48
+ f.rewind
49
+ specs = `#{IDENTIFY_COMMAND} #{f.path}`
50
+ sizes = specs.split(/\s+/, 4)[2].split('x').collect(&:to_i) if $? == 0
51
+ end
52
+
53
+ sizes
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,32 @@
1
+ module Rack
2
+ class ImgSizes
3
+ class URI
4
+ def initialize(env, uri)
5
+ @env = env
6
+ @uri = uri
7
+ urify!
8
+ end
9
+
10
+ def urify!
11
+ return if full_uri?
12
+ request = ::Rack::Request.new @env
13
+ @uri = ::File.join ::File.dirname(@env['PATH_INFO']), @uri if relative_uri?
14
+ @uri = "#{request.scheme}://#{request.host_with_port}#{@uri}"
15
+ end
16
+
17
+ def to_s
18
+ @uri
19
+ end
20
+
21
+ private
22
+
23
+ def relative_uri?
24
+ !full_uri? && @uri !~ /^\//
25
+ end
26
+
27
+ def full_uri?
28
+ @uri =~ /^\w+\:\/\//
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module ImgSizes
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require "rack/img_sizes/version.rb"
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "rack-img-sizes"
9
+ gem.version = Rack::ImgSizes::VERSION
10
+ gem.authors = ["Piotr Usewicz"]
11
+ gem.email = ["piotr@layer22.com"]
12
+ gem.description = %q{Appends image sizes to all img tags}
13
+ gem.summary = %q{Appends image sizes to all img tags}
14
+ gem.homepage = ""
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency('nokogiri')
22
+ end
@@ -0,0 +1,70 @@
1
+ require 'rack/builder'
2
+ require 'rack/mock'
3
+ require File.expand_path '../../lib/rack/img_sizes', __FILE__
4
+
5
+ require 'test/unit'
6
+ class MiddlewareTest < Test::Unit::TestCase
7
+ def test_call
8
+ app = Rack::Builder.new do
9
+ use Rack::ImgSizes
10
+ run proc {|env| [200, {}, [%q{
11
+ <html>
12
+ <head>
13
+ <meta charset="utf-8" />
14
+ <meta http-equiv='X-UA-Compatible' content='IE=edge;chrome=1' />
15
+ <meta name="viewport" content="width=device-width">
16
+ <meta name="generated" content="2012-10-13 17:06:11 UTC">
17
+ <link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
18
+ <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
19
+ <script src="/javascripts/application.js" type="text/javascript"></script>
20
+ <title>Magpul Executive Field Case For iPhone 4/4S</title>
21
+ </head>
22
+ <body class="articles articles_2012 articles_2012_10 articles_2012_10_13 articles_2012_10_13_magpul-executive-field-case-for-iphone-4-4s">
23
+ <div id="container">
24
+ <header>
25
+ <h1><a id="branding" href="/">Tactical<span>Two</span></a></h1>
26
+ </header>
27
+ <article id="main" role="main">
28
+ <header>
29
+ <img src="logo.png">
30
+ <img src="/images/icons/adwords-24.gif">
31
+ <img src="http://www.google.com/images/logos/ps_logo.png">
32
+ </header>
33
+ </article>
34
+ </div>
35
+ </body>
36
+ </html>
37
+ }]]}
38
+ end.to_app
39
+
40
+ mock = Rack::MockRequest.new app
41
+ assert_equal %q{
42
+ <html>
43
+ <head>
44
+ <meta charset="utf-8" />
45
+ <meta http-equiv='X-UA-Compatible' content='IE=edge;chrome=1' />
46
+ <meta name="viewport" content="width=device-width">
47
+ <meta name="generated" content="2012-10-13 17:06:11 UTC">
48
+ <link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
49
+ <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
50
+ <script src="/javascripts/application.js" type="text/javascript"></script>
51
+ <title>Magpul Executive Field Case For iPhone 4/4S</title>
52
+ </head>
53
+ <body class="articles articles_2012 articles_2012_10 articles_2012_10_13 articles_2012_10_13_magpul-executive-field-case-for-iphone-4-4s">
54
+ <div id="container">
55
+ <header>
56
+ <h1><a id="branding" href="/">Tactical<span>Two</span></a></h1>
57
+ </header>
58
+ <article id="main" role="main">
59
+ <header>
60
+ <img src="logo.png" width="103" height="40">
61
+ <img src="/images/icons/adwords-24.gif" width="24" height="24">
62
+ <img src="http://www.google.com/images/logos/ps_logo.png" width="411" height="142">
63
+ </header>
64
+ </article>
65
+ </div>
66
+ </body>
67
+ </html>
68
+ }.strip, mock.get('http://www.google.com/images/logos/index.html').body.strip
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-img-sizes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Piotr Usewicz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Appends image sizes to all img tags
31
+ email:
32
+ - piotr@layer22.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .travis.yml
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/rack-img-sizes.rb
44
+ - lib/rack/img_sizes.rb
45
+ - lib/rack/img_sizes/uri.rb
46
+ - lib/rack/img_sizes/version.rb
47
+ - rack-img-sizes.gemspec
48
+ - test/integration_test.rb
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Appends image sizes to all img tags
73
+ test_files:
74
+ - test/integration_test.rb
75
+ has_rdoc: