lorem-image-ware 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,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .rvmrc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lorem-image-ware.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brendon Murphy
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,62 @@
1
+ # Lorem::Image::Ware
2
+
3
+ Rack middleware for proxying requests to [lorempixel.com](http://lorempixel.com)
4
+
5
+ This mounts routing in your Rack stack so that paths like `/lorem/image/400/200` will
6
+ serve a 400x200 random image from lorempixel.com.
7
+
8
+ The middleware is primarily useful because it will cache responses from lorempixel so
9
+ that after a few page loads, images should load much faster. Without such local
10
+ caching you will be stuck hammering their server on every page load, and waiting a
11
+ few seconds for images to fill in.
12
+
13
+ It is recommended to only load the middleware in development environtments.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'lorem-image-ware'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install lorem-image-ware
28
+
29
+ ## Usage
30
+
31
+ Mount in your app stack like:
32
+
33
+ ```ruby
34
+ use LoremImageWare::Middleware
35
+ ```
36
+
37
+ If you wish to serve off a directory other than the default `/lorem`, pass the `:root` option:
38
+
39
+ ```ruby
40
+ use LoremImageWare::Middleware, :root => "/random_images"
41
+ ```
42
+
43
+ There is a small Helpers module you can include for generating lorem image tags for you. For example
44
+ in Sinatra classical:
45
+
46
+ ```ruby
47
+ helpers LoremImageWare::Helpers
48
+
49
+ # In a view
50
+ lorem_image_tag(:height => 400, :width => 200, :tag => "sports", :class => "some-class")
51
+ ```
52
+
53
+ The helper will add a class of `lorem-image` to tags for you, as well as populate the dimensions
54
+ and set inline height and width styling on the image.
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = "test/*_test.rb"
7
+ end
8
+
@@ -0,0 +1,90 @@
1
+ require "lorem-image-ware/version"
2
+ require "net/http"
3
+ require "uri"
4
+
5
+ module LoremImageWare
6
+ class LoremPixelProvider
7
+ def initialize(grayscale = false)
8
+ @grayscale = grayscale
9
+ end
10
+
11
+ def url(params)
12
+ tag = params[:tag] || "abstract"
13
+ url = ["http://lorempixel.com", @grayscale ? "g" : nil, params[:width], params[:height], tag].compact.join("/")
14
+ url += "/?r=#{rand(20)}"
15
+ url
16
+ end
17
+ end
18
+
19
+ module Helpers
20
+ def lorem_image_root
21
+ "/lorem"
22
+ end
23
+
24
+ def lorem_image_path(options = {})
25
+ height = options.fetch(:height, 200)
26
+ width = options.fetch(:width, 400)
27
+ tag = options.fetch(:tag, "abstract")
28
+
29
+ "#{lorem_image_root}/image/#{width}/#{height}/#{tag}?r=#{rand(99)}"
30
+ end
31
+
32
+ def lorem_image_tag(options = {})
33
+ height = options.fetch(:height, 200)
34
+ width = options.fetch(:width, 400)
35
+ html_class = [options[:class], "lorem-image"].compact.join(" ")
36
+
37
+ %Q{<img src="#{lorem_image_path(options)}" class="#{html_class}" width="#{width}" height="#{height}" style="height: #{height}px; width: #{width}px;" />}
38
+ end
39
+ end
40
+
41
+ class Middleware
42
+ def initialize(app, options = {})
43
+ @app = app
44
+ @options = options
45
+ end
46
+
47
+ def call(env)
48
+ self.dup._call(env)
49
+ end
50
+
51
+ def _call(env)
52
+ request = Rack::Request.new(env)
53
+
54
+ if request.fullpath =~ %r{^#{root}/image/(\d+)/(\d+)/?(/(\S+))?}i
55
+ url = provider.url(width: $1, height: $2, tag: $4)
56
+ serve_image(url)
57
+ else
58
+ @app.call(env)
59
+ end
60
+ end
61
+
62
+ def serve_image(url)
63
+ if image_data = fetch_image(url)
64
+ Rack::Response.new([], 200, {"Content-Type" => "image/jpeg"}).finish do |res|
65
+ res.write(image_data)
66
+ end
67
+ else
68
+ [404, {"Content-Type" => "test/plain"}, ["Not found"]]
69
+ end
70
+ end
71
+
72
+ def fetch_image(url)
73
+ @@image_cache ||= Hash.new do |h, url|
74
+ uri = URI(url)
75
+ res = Net::HTTP.get_response(uri)
76
+ h[url] = res.body if res.code == "200"
77
+ end
78
+
79
+ @@image_cache[url]
80
+ end
81
+
82
+ def root
83
+ @options.fetch(:root, "/lorem")
84
+ end
85
+
86
+ def provider
87
+ @provider ||= @options.fetch(:provider, LoremPixelProvider.new)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ module LoremImageWare
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/lorem-image-ware/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Brendon Murphy"]
6
+ gem.email = ["xternal1+github@gmail.com"]
7
+ gem.summary = %q{rack middleware for proxying requests to lorempixel.com}
8
+ gem.description = gem.summary
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^test/})
14
+ gem.name = "lorem-image-ware"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = LoremImageWare::VERSION
17
+
18
+ gem.add_dependency "rack"
19
+
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency "rack-test"
22
+ gem.add_development_dependency "minitest"
23
+ end
@@ -0,0 +1,24 @@
1
+ require_relative "./test_helper"
2
+
3
+ class TestHelpers < MiniTest::Unit::TestCase
4
+ include LoremImageWare::Helpers
5
+
6
+ def test_default_image_root
7
+ assert_equal "/lorem", lorem_image_root
8
+ end
9
+
10
+ def test_default_image_path
11
+ path = lorem_image_path
12
+ assert_match %r{^/lorem/image/400/200/abstract\?r=\d+$}, path
13
+ end
14
+
15
+ def test_image_path
16
+ path = lorem_image_path(:height => 50, :width => 100, :tag => "sports")
17
+ assert_match %r{^/lorem/image/100/50/sports\?r=\d+$}, path
18
+ end
19
+
20
+ def test_image_tag
21
+ image_tag = lorem_image_tag(:height => 50, :width => 100, :tag => "sports", :class => "klass")
22
+ assert_match %r{<img src="/lorem/image/100/50/sports\?r=\d+" class="klass lorem-image" width="100" height="50" style="height: 50px; width: 100px;" />}, image_tag
23
+ end
24
+ end
@@ -0,0 +1,61 @@
1
+ require_relative "./test_helper"
2
+ require "ostruct"
3
+
4
+ class TestCase < MiniTest::Unit::TestCase
5
+ include Rack::Test::Methods
6
+ end
7
+
8
+ class TestPassThru < TestCase
9
+ def app
10
+ Rack::Builder.new do
11
+ use LoremImageWare::Middleware
12
+ run lambda { |env| [200, { 'Content-Type' => "text/plain" }, ["path = #{env['PATH_INFO']}"]] }
13
+ end
14
+ end
15
+
16
+ def test_root
17
+ get "/"
18
+ assert_equal "path = /", last_response.body
19
+ end
20
+
21
+ def test_hello
22
+ get "/hello"
23
+ assert_equal "path = /hello", last_response.body
24
+ end
25
+ end
26
+
27
+ class TestSimpleLorem < TestCase
28
+ def setup
29
+ @provider = MiniTest::Mock.new
30
+ end
31
+
32
+ def app
33
+ Rack::Builder.new do
34
+ use LoremImageWare::Middleware
35
+ run lambda { |env| [200, { 'Content-Type' => "text/plain" }, ["path = #{env['PATH_INFO']}"]] }
36
+ end
37
+ end
38
+
39
+ def test_width_and_height
40
+ get "/lorem/image/400/200"
41
+ assert_equal 200, last_response.status
42
+ assert_equal "image/jpeg", last_response.headers["Content-Type"]
43
+ assert last_response.headers["Content-Length"]
44
+ assert last_response.headers["Content-Length"].to_i > 0
45
+ end
46
+
47
+ def test_tag_param
48
+ get "/lorem/image/400/200/sports"
49
+ assert_equal 200, last_response.status
50
+ assert_equal "image/jpeg", last_response.headers["Content-Type"]
51
+ end
52
+
53
+ def test_bad_url
54
+ response = OpenStruct.new(:code => 404, :body => "not found")
55
+
56
+ Net::HTTP.stub(:get_response, response) do # stub goes away once the block is done
57
+ get "/lorem/image/100/200"
58
+ assert_equal 404, last_response.status
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,21 @@
1
+ require_relative "./test_helper"
2
+
3
+ class TestLoremPixelProvider < MiniTest::Unit::TestCase
4
+ def test_basic_color_url
5
+ provider = LoremImageWare::LoremPixelProvider.new
6
+ url = provider.url(width: 400, height: 200)
7
+ assert_match %r{http://lorempixel.com/400/200/abstract/\?r=\d+$}, url
8
+ end
9
+
10
+ def test_optional_tag
11
+ provider = LoremImageWare::LoremPixelProvider.new
12
+ url = provider.url(width: 200, height: 100, tag: "sports")
13
+ assert_match %r{http://lorempixel.com/200/100/sports/\?r=\d+$}, url
14
+ end
15
+
16
+ def test_grayscale_url
17
+ provider = LoremImageWare::LoremPixelProvider.new(:grayscale)
18
+ url = provider.url(width: 200, height: 100)
19
+ assert_match %r{http://lorempixel.com/g/200/100/abstract/\?r=\d+$}, url
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ require "minitest/unit"
2
+ require "rack/test"
3
+ require "minitest/autorun"
4
+ require_relative "../lib/lorem-image-ware"
5
+
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lorem-image-ware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brendon Murphy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &2156488720 !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: *2156488720
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2156488300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2156488300
36
+ - !ruby/object:Gem::Dependency
37
+ name: rack-test
38
+ requirement: &2156487880 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2156487880
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: &2156487460 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2156487460
58
+ description: rack middleware for proxying requests to lorempixel.com
59
+ email:
60
+ - xternal1+github@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - Gemfile
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - lib/lorem-image-ware.rb
71
+ - lib/lorem-image-ware/version.rb
72
+ - lorem-image-ware.gemspec
73
+ - test/helpers_test.rb
74
+ - test/middleware_test.rb
75
+ - test/providers_test.rb
76
+ - test/test_helper.rb
77
+ homepage: ''
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ segments:
90
+ - 0
91
+ hash: 1682226714333622018
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ segments:
99
+ - 0
100
+ hash: 1682226714333622018
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.15
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: rack middleware for proxying requests to lorempixel.com
107
+ test_files:
108
+ - test/helpers_test.rb
109
+ - test/middleware_test.rb
110
+ - test/providers_test.rb
111
+ - test/test_helper.rb