rack-gridfs-thumb 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Rack::GridFSThumb
2
2
  =================
3
3
 
4
- Rack::GridFS is Rack middleware for resizing images served via [Rack::GridFS][https://github.com/skinandbones/rack-gridfs]. Resizing is done via [MiniMagick] [https://github.com/probablycorey/mini_magick] and all the usual geometry strings are accepted. See http://www.imagemagick.org/script/command-line-processing.php#geometry for more details.
4
+ Rack::GridFSThumb is Rack middleware for resizing images served via [Rack::GridFS](https://github.com/skinandbones/rack-gridfs). Resizing is done via [MiniMagick](https://github.com/probablycorey/mini_magick) and all the usual [geometry arguments](http://www.imagemagick.org/script/command-line-processing.php#geometry) are accepted.
5
5
 
6
6
  For example:
7
7
 
@@ -15,10 +15,8 @@ Installation
15
15
  Usage
16
16
  -----
17
17
 
18
- require 'rack/gridfs'
19
18
  require 'rack/gridfs-thumb'
20
19
 
21
- prefix = 'gridfs'
20
+ use Rack::GridFSThumb, :prefix => 'gridfs', :hostname => 'localhost', :port => 27017, :database => 'test'
22
21
 
23
- use Rack::GridFS, :prefix => prefix, :hostname => 'localhost', :port => 27017, :database => 'test'
24
- use Rack::GridFSThumb, :prefix => prefix
22
+ See the Rack::GridFS README for all available options.
@@ -1,11 +1,12 @@
1
1
  require 'mini_magick'
2
+ require 'rack/gridfs'
2
3
 
3
4
  module Rack
4
5
  class GridFSThumb
5
6
  def initialize(app, options = {})
6
7
  @app = app
7
- @options = options
8
- @options[:prefix] ||= 'gridfs'
8
+ @options = default_options.merge(options)
9
+ @gridfs = @options[:gridfs] || Rack::GridFS::Endpoint.new(@options)
9
10
  end
10
11
 
11
12
  def call(env)
@@ -14,8 +15,12 @@ module Rack
14
15
  response = catch(:respond) do
15
16
  match(@request.path_info) do |path, geometry|
16
17
  status, @headers, image = request_image(path)
17
- thumb = thumbnail(image, geometry)
18
18
 
19
+ if geometry.nil?
20
+ throw :respond, [status, @headers, image]
21
+ end
22
+
23
+ thumb = thumbnail(image, geometry)
19
24
  response_for(thumb)
20
25
  end
21
26
  end
@@ -23,10 +28,17 @@ module Rack
23
28
  response || @app.call(env)
24
29
  end
25
30
 
31
+ def route
32
+ %r{(/#{@options[:prefix]}/[^/]+)/?([^/]+)?/?$}
33
+ end
34
+
26
35
  private
27
36
 
28
- def route
29
- %r{(/#{@options[:prefix]}/[^/]+)/([^/]+)/?$}
37
+ def default_options
38
+ {
39
+ :prefix => 'gridfs',
40
+ :mapper => lambda { |path| %r!^/#{@options[:prefix]}/([^/]+)/?!.match(path)[1] }
41
+ }
30
42
  end
31
43
 
32
44
  def match(path_info)
@@ -47,7 +59,7 @@ module Rack
47
59
 
48
60
  def request_image(path_info)
49
61
  env = @request.env.merge('PATH_INFO' => path_info)
50
- status, headers, body = @app.call(env)
62
+ status, headers, body = @gridfs.call(env)
51
63
 
52
64
  unless status >= 200 && status < 300
53
65
  throw :respond, [status, headers, body]
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class GridFSThumb
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.summary = "rack-gridfs-thumb-#{s.version}"
13
13
  s.description = 'Rack middleware for resizing images served via Rack::GridFS'
14
14
 
15
- s.add_dependency('rack')
15
+ s.add_dependency('rack-gridfs', '>= 0.4.1')
16
16
  s.add_dependency('mini_magick', '>= 3.3')
17
17
 
18
18
  s.add_development_dependency('rack-test')
@@ -13,8 +13,30 @@ class Rack::GridFSThumbTest < Test::Unit::TestCase
13
13
  end
14
14
 
15
15
  def app
16
- @app ||= image_app
17
- Rack::GridFSThumb.new(@app, :prefix => 'gridfs')
16
+ @app ||= gridfs_stub
17
+ Rack::GridFSThumb.new(@app, :prefix => 'gridfs', :gridfs => @app)
18
+ end
19
+
20
+ def test_route_regex
21
+ route = app.route
22
+
23
+ match = "/gridfs/#{@id}".match(route)
24
+ assert_equal "/gridfs/#{@id}", match[1]
25
+ assert_nil match[2]
26
+
27
+ match = "/gridfs/#{@id}/".match(route)
28
+ assert_equal "/gridfs/#{@id}", match[1]
29
+ assert_nil match[2]
30
+
31
+ match = "/gridfs/#{@id}/100x100".match(route)
32
+ assert_equal "/gridfs/#{@id}", match[1]
33
+ assert_equal '100x100', match[2]
34
+
35
+ match = "/gridfs/#{@id}/100x100/".match(route)
36
+ assert_equal "/gridfs/#{@id}", match[1]
37
+ assert_equal '100x100', match[2]
38
+
39
+ assert_nil "/gridfs/#{@id}/100x100/extra".match(route)
18
40
  end
19
41
 
20
42
  def test_not_called_without_geometry
@@ -26,7 +48,7 @@ class Rack::GridFSThumbTest < Test::Unit::TestCase
26
48
  end
27
49
 
28
50
  def test_not_called_when_not_found
29
- @app = not_found_app
51
+ @app = notfound_stub
30
52
 
31
53
  get "/gridfs/#{@id}/100x100"
32
54
 
@@ -58,7 +80,7 @@ class Rack::GridFSThumbTest < Test::Unit::TestCase
58
80
 
59
81
  private
60
82
 
61
- def image_app
83
+ def gridfs_stub
62
84
  image = File.new(@image_path, 'rb')
63
85
  headers = { 'Content-Length' => image.size.to_s }
64
86
  Proc.new do |env|
@@ -66,7 +88,7 @@ class Rack::GridFSThumbTest < Test::Unit::TestCase
66
88
  end
67
89
  end
68
90
 
69
- def not_found_app
91
+ def notfound_stub
70
92
  Proc.new do |env|
71
93
  [404, { 'Content-Length' => 9 }, ['Not Found']]
72
94
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-gridfs-thumb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-14 00:00:00.000000000Z
12
+ date: 2011-10-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rack
16
- requirement: &2152191760 !ruby/object:Gem::Requirement
15
+ name: rack-gridfs
16
+ requirement: &2152258380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 0.4.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152191760
24
+ version_requirements: *2152258380
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mini_magick
27
- requirement: &2152190700 !ruby/object:Gem::Requirement
27
+ requirement: &2152257420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.3'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2152190700
35
+ version_requirements: *2152257420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rack-test
38
- requirement: &2152189560 !ruby/object:Gem::Requirement
38
+ requirement: &2152256820 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152189560
46
+ version_requirements: *2152256820
47
47
  description: Rack middleware for resizing images served via Rack::GridFS
48
48
  email:
49
49
  - scottbnel@gmail.com
@@ -82,7 +82,7 @@ rubyforge_project:
82
82
  rubygems_version: 1.8.10
83
83
  signing_key:
84
84
  specification_version: 3
85
- summary: rack-gridfs-thumb-0.1.0
85
+ summary: rack-gridfs-thumb-0.2.0
86
86
  test_files:
87
87
  - test/image.png
88
88
  - test/test_gridfs_thumb.rb