mercury 0.9.3 → 0.9.4

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.
@@ -1,33 +1,27 @@
1
1
  module Sinatra
2
2
  module Images
3
3
  def self.registered(app)
4
- app.get '/*.gif' do
5
- stream_image([params["splat"][0],'gif'].join('.'),"image/gif")
6
- end
7
-
8
- app.get '/*.png' do
9
- stream_image([params["splat"][0],'png'].join('.'),"image/png")
10
- end
11
-
12
- app.get '/*.jpg' do
13
- stream_image([params["splat"][0],'jpg'].join('.'),"image/jpg")
14
- end
15
-
16
- app.get '/*.jpeg' do
17
- stream_image([params["splat"][0],'jpeg'].join('.'),"image/jpeg")
4
+ # stream images
5
+ app.get %r{[gif|jpg|png|jpeg]$} do
6
+ content_type get_image_type(request.path_info)
7
+ File.open(options.views + request.path_info, 'rb') do |file|
8
+ file.read
9
+ end
18
10
  end
19
11
  end
20
12
 
21
- private
22
- def get_image(filename)
23
- open(File.join(options.views, filename), 'rb') { |file| file.read }
24
- end
25
-
26
- def stream_image(filename, ct)
27
- content_type(ct)
28
- get_image(filename)
13
+ def get_image_type(image_name)
14
+ if image_name =~ /.gif/
15
+ "image/gif"
16
+ elsif image_name =~ /.jpg/
17
+ "image/jpg"
18
+ elsif image_name =~ /.png/
19
+ "image/png"
20
+ elsif image_name =~ /.jpeg/
21
+ "image/jpeg"
22
+ end
29
23
  end
30
-
24
+
31
25
  end
32
26
  register Images
33
27
  end
data/lib/mercury.rb CHANGED
@@ -8,7 +8,7 @@ require File.dirname(__FILE__) + '/mercury/images'
8
8
  # Core Sinatra application to run mercury apps
9
9
  class Mercury < Sinatra::Application
10
10
  helpers Sinatra::Helpers
11
- helpers Sinatra::Images
11
+ register Sinatra::Images
12
12
 
13
13
  set :root, FileUtils.pwd.gsub("\n",'')
14
14
  set :public, File.dirname(__FILE__) + '/public'
@@ -23,8 +23,9 @@ class Mercury < Sinatra::Application
23
23
  private
24
24
 
25
25
  def get_view(filename)
26
- if File.exists?(File.join(options.views, filename))
27
- view_file = File.join(options.views,filename)
26
+ full_file = File.join(options.views, filename)
27
+ if File.exists?(full_file)
28
+ view_file = full_file
28
29
  else
29
30
  view_directory = File.join(File.dirname(__FILE__),'views')
30
31
  view_file = File.join(view_directory,filename)
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
2
+
3
+ describe "images" do
4
+
5
+ include Rack::Test::Methods
6
+
7
+ def app
8
+ Mercury
9
+ end
10
+
11
+ it "should return gif" do
12
+ File.stub!(:open).and_return('foobar')
13
+ get "/hello.gif"
14
+ puts last_response.body.should == 'foobar'
15
+ end
16
+
17
+ it "should return correct file content type for gif" do
18
+ app.get_image_type('/hello.gif').should == "image/gif"
19
+ end
20
+
21
+ it "should return correct file content type for jpg" do
22
+ app.get_image_type('/hello.jpg').should == "image/jpg"
23
+ end
24
+
25
+ it "should return correct file content type for png" do
26
+ app.get_image_type('/hello.png').should == "image/png"
27
+ end
28
+
29
+ it "should return correct file content type for jpeg" do
30
+ app.get_image_type('/hello.jpeg').should == "image/jpeg"
31
+ end
32
+
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mercury
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Wilson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-23 00:00:00 -04:00
12
+ date: 2010-03-24 00:00:00 -04:00
13
13
  default_executable: mercury
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -132,5 +132,6 @@ signing_key:
132
132
  specification_version: 3
133
133
  summary: Easy Hacking with Haml, Sass, JQuery, CoffeeScript
134
134
  test_files:
135
+ - spec/lib/mercury/images_spec.rb
135
136
  - spec/mercury_spec.rb
136
137
  - spec/spec_helper.rb