sinatra-respond_to 0.3.8 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.rdoc CHANGED
@@ -1,3 +1,17 @@
1
+ === 0.4.0 / 2010-01-30
2
+
3
+ * Specify that when running into unknown template languages the error message defaults to a generic template example
4
+ * Update code example
5
+ * Update supported sinatra versions in README
6
+ * Remove issues section because I believe it has been corrected
7
+ * Update development error message for new builder syntax
8
+ * Remove redundant hoe dependency and update rcov opts
9
+ * Fix the media_type -> mime_type conversion on Sinatra 0.9
10
+ * Fix rendering with extensions, e.g. resource.html.haml
11
+ * Add spec and passing code for content_type usage
12
+ * Changed how development error images are shown
13
+ * Update media_type to mime_type for Sinatra 1.0 while maintaining backward compat
14
+
1
15
  === 0.3.8 / 2010-01-30
2
16
 
3
17
  * Need to bump because of wrong gem name
data/README.rdoc CHANGED
@@ -16,6 +16,8 @@ A respond_to style Rails block for baked-in web service support in Sinatra
16
16
 
17
17
  require 'sinatra'
18
18
  require 'sinatra/respond_to'
19
+
20
+ register Sinatra::RespondTo
19
21
 
20
22
  get '/posts' do
21
23
  @posts = Post.recent
@@ -80,7 +82,7 @@ There a few options available for configuring the default behavior of respond_to
80
82
 
81
83
  == REQUIREMENTS:
82
84
 
83
- * sinatra 0.9.4 - 0.10
85
+ * sinatra 0.9.4 - 1.0
84
86
 
85
87
  == INSTALL:
86
88
 
@@ -116,15 +118,6 @@ After checking out the source, run:
116
118
  This task will install any missing dependencies, run the tests/specs,
117
119
  and generate the RDoc.
118
120
 
119
- == ISSUES
120
-
121
- Sinatra has a bug that affects Classic style applications and extensions see 215[https://sinatra.lighthouseapp.com/projects/9779/tickets/215-extensions-cannot-define-before-filters-for-classic-apps] and 180[https://sinatra.lighthouseapp.com/projects/9779/tickets/180-better-route-inheritence].
122
- I have worked around this by explicitly registering RespondTo with the top level Sinatra::Application when
123
- requiring sinatra/respond\_to.
124
-
125
- * {Extensions cannot define before filters for classic apps}[https://sinatra.lighthouseapp.com/projects/9779/tickets/215-extensions-cannot-define-before-filters-for-classic-apps]
126
- * {Better route inheritence}[https://sinatra.lighthouseapp.com/projects/9779/tickets/180-better-route-inheritence]
127
-
128
121
  == LICENSE:
129
122
 
130
123
  (The MIT License)
data/Rakefile CHANGED
@@ -11,7 +11,6 @@ Hoe.plugin :git
11
11
  Hoe.spec 'sinatra-respond_to' do
12
12
  developer('Chris Hoffman', 'cehoffman@gmail.com')
13
13
  extra_deps << ['sinatra', '>= 0.9.4']
14
- extra_dev_deps << ['hoe', '>= 2.5.0']
15
14
  extra_dev_deps << ['rspec', '>= 1.3.0']
16
15
  extra_dev_deps << ['rcov', '>= 0.9.7.1']
17
16
  extra_dev_deps << ['rack-test', '>= 0.5.3']
@@ -33,7 +32,7 @@ begin
33
32
  t.rcov = true
34
33
  t.rcov_opts << '--text-summary'
35
34
  t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
36
- t.rcov_opts << '--exclude' << 'pkg,spec'
35
+ t.rcov_opts << '--comments' << '--exclude' << 'pkg,gems'
37
36
  end
38
37
  rescue LoadError
39
38
  puts "RSpec not available. Install it with sudo gem install rspec."
@@ -40,6 +40,9 @@ module Sinatra
40
40
  #
41
41
  # and the format will automatically be available in <tt>format</tt>
42
42
  app.before do
43
+ # Let through sinatra image urls in development
44
+ next if self.class.development? && request.path_info =~ %r{/__sinatra__/.*?.png}
45
+
43
46
  unless options.static? && options.public? && (request.get? || request.head?) && static_file?(request.path_info)
44
47
  request.path_info.sub! %r{\.([^\./]+)$}, ''
45
48
 
@@ -50,12 +53,6 @@ module Sinatra
50
53
  end
51
54
 
52
55
  app.configure :development do |dev|
53
- # Very, very, very hackish but only for development at least
54
- # Modifies the regex matching /__sinatra__/:image.png to not have the extension
55
- ["GET", "HEAD"].each do |verb|
56
- dev.routes[verb][1][0] = Regexp.new(dev.routes[verb][1][0].source.gsub(/\\\.[^\.\/]+\$$/, '$'))
57
- end
58
-
59
56
  dev.error UnhandledFormat do
60
57
  content_type :html, :charset => 'utf-8'
61
58
 
@@ -96,7 +93,7 @@ module Sinatra
96
93
  layout = case engine
97
94
  when 'haml' then "!!!\n%html\n %body= yield"
98
95
  when 'erb' then "<html>\n <body>\n <%= yield %>\n </body>\n</html>"
99
- when 'builder' then "builder do |xml|\n xml << yield\nend"
96
+ when 'builder' then ::Sinatra::VERSION =~ /^1.0/ ? "xml << yield" : "builder do |xml|\n xml << yield\nend"
100
97
  end
101
98
 
102
99
  layout = "<small>app.#{format}.#{engine}</small>\n<pre>#{escape_html(layout)}</pre>"
@@ -133,28 +130,56 @@ module Sinatra
133
130
 
134
131
  app.class_eval do
135
132
  private
136
- def render_with_format(*args)
133
+ # Changes in 1.0 Sinatra reuse render for layout so we store
134
+ # the original value to tell us if this is an automatic attempt
135
+ # to do a layout call. If it is, it might fail with Errno::ENOENT
136
+ # and we want to pass that back to sinatra since it isn't a MissingTemplate
137
+ # error
138
+ def render_with_format(*args, &block)
139
+ assumed_layout = args[1] == :layout
137
140
  args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
138
- render_without_format *args
139
- rescue Errno::ENOENT
140
- raise MissingTemplate, "#{args[1]}.#{args[0]}"
141
+ render_without_format *args, &block
142
+ rescue Errno::ENOENT => e
143
+ raise MissingTemplate, "#{args[1]}.#{args[0]}" unless assumed_layout
144
+ raise e
141
145
  end
142
146
  alias_method :render_without_format, :render
143
147
  alias_method :render, :render_with_format
144
148
 
145
- def lookup_layout_with_format(*args)
146
- args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
147
- lookup_layout_without_format *args
149
+ if ::Sinatra::VERSION =~ /^0\.9/
150
+ def lookup_layout_with_format(*args)
151
+ args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
152
+ lookup_layout_without_format *args
153
+ end
154
+ alias_method :lookup_layout_without_format, :lookup_layout
155
+ alias_method :lookup_layout, :lookup_layout_with_format
148
156
  end
149
- alias_method :lookup_layout_without_format, :lookup_layout
150
- alias_method :lookup_layout, :lookup_layout_with_format
151
157
  end
152
158
  end
153
159
 
154
160
  module Helpers
161
+ # Patch the content_type function to remember the set type
162
+ # This helps cut down on time in the format helper so it
163
+ # doesn't have to do a reverse lookup on the header
164
+ def self.included(klass)
165
+ klass.class_eval do
166
+ def content_type_with_save(*args)
167
+ content_type_without_save *args
168
+ @format = args.first.to_sym
169
+ response['Content-Type']
170
+ end
171
+ alias_method :content_type_without_save, :content_type
172
+ alias_method :content_type, :content_type_with_save
173
+ end if ::Sinatra::VERSION =~ /^1.0/
174
+ end
175
+
176
+ def self.mime_type(sym)
177
+ ::Sinatra::Base.respond_to?(:media_type) && ::Sinatra::Base.media_type(sym) || ::Sinatra::Base.mime_type(sym)
178
+ end
179
+
155
180
  def format(val=nil)
156
181
  unless val.nil?
157
- mime_type = media_type(val)
182
+ mime_type = ::Sinatra::RespondTo::Helpers.mime_type(val)
158
183
  fail "Unknown media type #{val}\nTry registering the extension with a mime type" if mime_type.nil?
159
184
 
160
185
  @format = val.to_sym
@@ -188,7 +213,7 @@ module Sinatra
188
213
  def respond_to(&block)
189
214
  wants = {}
190
215
  def wants.method_missing(type, *args, &handler)
191
- Sinatra::Base.send(:fail, "Unknown media type for respond_to: #{type}\nTry registering the extension with a mime type") if Sinatra::Base.media_type(type).nil?
216
+ ::Sinatra::Base.send(:fail, "Unknown media type for respond_to: #{type}\nTry registering the extension with a mime type") if ::Sinatra::RespondTo::Helpers.mime_type(type).nil?
192
217
  self[type] = handler
193
218
  end
194
219
 
@@ -199,8 +224,4 @@ module Sinatra
199
224
  end
200
225
  end
201
226
  end
202
-
203
- # Get around before filter problem for classic applications by registering
204
- # with the context they are run in explicitly instead of Sinatra::Default
205
- Sinatra::Application.register RespondTo
206
227
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module RespondTo
3
- Version = '0.3.8'
3
+ Version = '0.4.0'
4
4
  end
5
5
  end
data/spec/app/test_app.rb CHANGED
@@ -48,6 +48,7 @@ class TestApp < Sinatra::Base
48
48
  wants.html { haml :missing }
49
49
  wants.xml { builder :missing }
50
50
  wants.js { erb :missing }
51
+ wants.css { sass :missing }
51
52
  end
52
53
  end
53
54
  end
@@ -1,8 +1,8 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
3
  describe Sinatra::RespondTo do
4
- def media_type(sym)
5
- Sinatra::Base.media_type(sym)
4
+ def mime_type(sym)
5
+ ::Sinatra::Base.respond_to?(:media_type) && ::Sinatra::Base.media_type(sym) || ::Sinatra::Base.mime_type(sym)
6
6
  end
7
7
 
8
8
  describe "options" do
@@ -25,7 +25,7 @@ describe Sinatra::RespondTo do
25
25
 
26
26
  get '/resource'
27
27
 
28
- last_response['Content-Type'].should =~ %r{#{media_type(:js)}}
28
+ last_response['Content-Type'].should =~ %r{#{mime_type(:js)}}
29
29
  end
30
30
 
31
31
  it "should not set the content type to application/javascript for an XMLHttpRequest when assume_xhr_is_js is false" do
@@ -33,7 +33,7 @@ describe Sinatra::RespondTo do
33
33
  header 'X_REQUESTED_WITH', 'XMLHttpRequest'
34
34
  get '/resource'
35
35
 
36
- last_response['Content-Type'].should_not =~ %r{#{media_type(:js)}}
36
+ last_response['Content-Type'].should_not =~ %r{#{mime_type(:js)}}
37
37
 
38
38
  # Put back the option, no side effects here
39
39
  TestApp.enable :assume_xhr_is_js
@@ -76,7 +76,7 @@ describe Sinatra::RespondTo do
76
76
  it "should set the appropriate content-type for route with an extension" do
77
77
  get "/resource.xml"
78
78
 
79
- last_response['Content-Type'].should =~ %r{#{media_type(:xml)}}
79
+ last_response['Content-Type'].should =~ %r{#{mime_type(:xml)}}
80
80
  end
81
81
 
82
82
  it "should set the character set to the default character set" do
@@ -141,7 +141,7 @@ describe Sinatra::RespondTo do
141
141
  it "should set the default content type when no extension" do
142
142
  get "/normal-no-respond_to"
143
143
 
144
- last_response['Content-Type'].should =~ %r{#{media_type(TestApp.default_content)}}
144
+ last_response['Content-Type'].should =~ %r{#{mime_type(TestApp.default_content)}}
145
145
  end
146
146
 
147
147
  it "should set the default character when no extension" do
@@ -153,7 +153,7 @@ describe Sinatra::RespondTo do
153
153
  it "should set the appropriate content type when given an extension" do
154
154
  get "/normal-no-respond_to.css"
155
155
 
156
- last_response['Content-Type'].should =~ %r{#{media_type(:css)}}
156
+ last_response['Content-Type'].should =~ %r{#{mime_type(:css)}}
157
157
  end
158
158
 
159
159
  it "should set the default charset when given an extension" do
@@ -202,10 +202,11 @@ describe Sinatra::RespondTo do
202
202
  last_response.status.should == 500
203
203
  end
204
204
 
205
- it "should provide a helpful error message for a missing template when in development" do
206
- get '/missing-template'
205
+ it "should provide a helpful generic error message for a missing template when in development" do
206
+ get '/missing-template.css'
207
207
 
208
- last_response.body.should =~ /missing\.html\.haml/
208
+ last_response.body.should =~ /missing-template\.html\.haml/
209
+ last_response.body.should =~ %r{get '/missing-template' do respond_to do |wants| wants.html \{ haml :missing-template, layout => :app \} end end}
209
210
  end
210
211
 
211
212
  it "should show the /__sinatra__/500.png" do
@@ -260,6 +261,7 @@ describe Sinatra::RespondTo do
260
261
  end
261
262
 
262
263
  describe "helpers:" do
264
+ include Sinatra::Helpers
263
265
  include Sinatra::RespondTo::Helpers
264
266
 
265
267
  before(:each) do
@@ -311,7 +313,7 @@ describe Sinatra::RespondTo do
311
313
  it "should set the correct mime type when given an extension" do
312
314
  format :xml
313
315
 
314
- response['Content-Type'].split(';').should include(media_type(:xml))
316
+ response['Content-Type'].split(';').should include(mime_type(:xml))
315
317
  end
316
318
 
317
319
  it "should fail when set to an unknown extension type" do
@@ -331,6 +333,12 @@ describe Sinatra::RespondTo do
331
333
 
332
334
  response['Content-Type'].should == "application/xml;charset=utf-8"
333
335
  end
336
+
337
+ it "should not return nil when only content_type sets headers" do
338
+ content_type :xhtml
339
+
340
+ format.should == :xhtml
341
+ end if Sinatra::VERSION =~ /^1.0/
334
342
  end
335
343
 
336
344
  describe "static_file?" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-respond_to
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hoffman
@@ -42,16 +42,6 @@ dependencies:
42
42
  - !ruby/object:Gem::Version
43
43
  version: 0.3.0
44
44
  version:
45
- - !ruby/object:Gem::Dependency
46
- name: hoe
47
- type: :development
48
- version_requirement:
49
- version_requirements: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: 2.5.0
54
- version:
55
45
  - !ruby/object:Gem::Dependency
56
46
  name: rspec
57
47
  type: :development