ajsharp-sinatra-respond_to 0.7.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,5 @@
1
+ .bundle
2
+ .DS_Store
3
+ pkg/
4
+ coverage/
5
+ coverage.info
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --profile
data/Changelog.rdoc ADDED
@@ -0,0 +1,91 @@
1
+ === 0.7.0 / 2011-03-20
2
+
3
+ * Compatibility with Sinatra 1.2
4
+ * Allows xhr requests to use content type of extension if given
5
+
6
+ === 0.6.0 / 2010-11-04
7
+
8
+ * Remove default charset settings because Sinatra 1.1 includes this
9
+ * Correct problem when Content-Type starts out as nil
10
+
11
+ === 0.5.0 / 2010-08-22
12
+
13
+ * Requires Sinatra 1.x
14
+ * Allow specifying format through a format param to the url
15
+
16
+ === 0.4.0 / 2010-01-30
17
+
18
+ * Specify that when running into unknown template languages the error message defaults to a generic template example
19
+ * Update code example
20
+ * Update supported sinatra versions in README
21
+ * Remove issues section because I believe it has been corrected
22
+ * Update development error message for new builder syntax
23
+ * Remove redundant hoe dependency and update rcov opts
24
+ * Fix the media_type -> mime_type conversion on Sinatra 0.9
25
+ * Fix rendering with extensions, e.g. resource.html.haml
26
+ * Add spec and passing code for content_type usage
27
+ * Changed how development error images are shown
28
+ * Update media_type to mime_type for Sinatra 1.0 while maintaining backward compat
29
+
30
+ === 0.3.8 / 2010-01-30
31
+
32
+ * Need to bump because of wrong gem name
33
+ * Fix some issues in the README
34
+
35
+ === 0.3.7 / 2010-01-30
36
+
37
+ * Update builder template for how sinatra handles this now
38
+ * Update spec tests for 1.9
39
+ * Update XMLHttpResponse test for new rack-test
40
+ * Regenerated gemspec for version 0.3.6
41
+
42
+ === 0.3.6 / 2009-07-31
43
+
44
+ * Version bump to 0.3.6
45
+ * solving incompatibility between 1.9 to fully qualify TEXT_MIME_TYPES check
46
+
47
+ === 0.3.5 / 2009-05-14
48
+
49
+ * Regenerated gemspec for version 0.3.5
50
+ * Version bump to 0.3.5
51
+ * better testing of environment expectations, workarounds for sinatra environment switching
52
+ * problem in spec, wasn't checking for right thing
53
+ * spec for reaching the /__sinatra__/*.png images
54
+ * couple more tests for charset and format helpers
55
+ * reduce regex use and simplify others
56
+ * some more code cleaning
57
+ * simplification and readability
58
+ * add some more specification for respond_to
59
+
60
+ === 0.3.4 / 2009-05-13
61
+
62
+ * updated gemspec
63
+ * Version bump to 0.3.4
64
+ * updated readme
65
+ * passing code for tests
66
+ * tests and tests
67
+ * typo in readme
68
+ * allow for content_type setting automatically when not using respond_to
69
+ * make it a bit clearer in readme that outside of respond_to content_type is not set
70
+ * bumped gem version in gemspec
71
+ * noted the change in classic applications, the gem now works around this but not following extension writing guidelines
72
+
73
+ === 0.3.3 / 2009-05-12
74
+
75
+ * Version bump to 0.3.3
76
+ * Version bump to 0.3.2
77
+ * spec referenced file not in repository
78
+
79
+ === 0.3.1 / 2009-05-11
80
+
81
+ * Version bump to 0.3.1
82
+ * add sinatra as a dependency
83
+ * allow overriding default charset in respond_to
84
+ * install instructions
85
+ * first gem
86
+ * Version bump to 0.0.0
87
+ * missing end in example
88
+ * add caveats about existing routes
89
+ * fix a little error in example
90
+ * updated readme and license
91
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sinatra-respond_to (0.7.0)
5
+ sinatra (~> 1.2)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ builder (2.1.2)
11
+ diff-lcs (1.1.2)
12
+ haml (3.0.22)
13
+ rack (1.2.1)
14
+ rack-test (0.5.6)
15
+ rack (>= 1.0)
16
+ rcov (0.9.9)
17
+ rspec (2.5.0)
18
+ rspec-core (~> 2.5.0)
19
+ rspec-expectations (~> 2.5.0)
20
+ rspec-mocks (~> 2.5.0)
21
+ rspec-core (2.5.1)
22
+ rspec-expectations (2.5.0)
23
+ diff-lcs (~> 1.1.2)
24
+ rspec-mocks (2.5.0)
25
+ sinatra (1.2.1)
26
+ rack (~> 1.1)
27
+ tilt (< 2.0, >= 1.2.2)
28
+ tilt (1.2.2)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ builder (>= 2.0)
35
+ bundler (~> 1.0.10)
36
+ haml (>= 3.0)
37
+ rack-test (~> 0.5.6)
38
+ rcov (~> 0.9.8)
39
+ rspec (~> 2.5.0)
40
+ sinatra-respond_to!
data/README.md ADDED
@@ -0,0 +1,145 @@
1
+ # sinatra-respond\_to
2
+
3
+ * http://www.github.com/cehoffman/sinatra-respond\_to
4
+
5
+ ## DESCRIPTION:
6
+
7
+ A respond\_to style Rails block for baked-in web service support in Sinatra
8
+
9
+ ## FEATURES/PROBLEMS:
10
+
11
+ * Handles setting the content type depending on what is being served
12
+ * Automatically can adjust XMLHttpRequests to return Javascript
13
+
14
+ ## SYNOPSIS:
15
+
16
+ Allows urls of the form **/posts**, **/posts.rss**, and **/posts?format=atom** to route to the same Sinatra block and format specific respond\_to block.
17
+
18
+ require 'sinatra'
19
+ require 'sinatra/respond_to'
20
+
21
+ Sinatra::Application.register Sinatra::RespondTo
22
+
23
+ get '/posts' do
24
+ @posts = Post.recent
25
+
26
+ respond_to do |wants|
27
+ wants.html { haml :posts } # => views/posts.html.haml, also sets content_type to text/html
28
+ wants.rss { haml :posts } # => views/posts.rss.haml, also sets content_type to application/rss+xml
29
+ wants.atom { haml :posts } # => views/posts.atom.haml, also sets content_type to appliation/atom+xml
30
+ end
31
+ end
32
+
33
+ get '/post/:id' do
34
+ @post = Post.find(params[:id])
35
+
36
+ respond_to do |wants|
37
+ wants.html { haml :post } # => views/post.html.haml, also sets content_type to text/html
38
+ wants.xhtml { haml :post } # => views/post.xhtml.haml, also sets content_type to application/xhtml+xml
39
+ wants.xml { @post.to_xml } # => sets content_type to application/xml
40
+ wants.js { erb :post } # => views/post.js.erb, also sets content_type to application/javascript
41
+ end
42
+ end
43
+
44
+ get '/comments/:id' do
45
+ @comment = Comment.find(params[:id])
46
+
47
+ respond_to do |wants|
48
+ wants.html { haml :comment } # => views/comment.html.haml, also sets content_type to text/html
49
+ wants.json { @comment.to_json } # => sets content_type to application/json
50
+ wants.js { erb :comment } # => views/comment.js.erb, also sets content_type to application/javascript
51
+ end
52
+ end
53
+
54
+ To change the character set of the response, there is a `charset` helper. For example
55
+
56
+ get '/iso-8859-1' do
57
+ charset 'iso-8859-1'
58
+ "This is now sent using iso-8859-1 character set"
59
+ end
60
+
61
+ get '/respond_to-mixed' do
62
+ respond_to do |wants|
63
+ wants.html { charset 'iso-8859-1'; "Some html in iso-8859-1" }
64
+ wants.xml { builder :utf-8-xml } # => this is returned in the default character set
65
+ end
66
+ end
67
+
68
+ ## CONFIGURATION:
69
+
70
+ There a few options available for configuring the default behavior of respond\_to using Sinatra's `set` utility.
71
+
72
+ * **default\_content - :html**
73
+ When a user vists a url without an extension, for example /post this will be
74
+ the assumed content to serve first. Expects a symbol as used in setting content_type.
75
+ * **assume\_xhr\_is\_js - true**
76
+ To avoid headaches with accept headers, and appending .js to urls, this will
77
+ cause the default format for all XmlHttpRequests to be classified as wanting Javascript
78
+ in the response.
79
+
80
+ ## REQUIREMENTS:
81
+
82
+ * sinatra 1.1
83
+
84
+ If you would like to use Sinatra 1.0, use version `0.5.0`.
85
+
86
+ ## INSTALL:
87
+
88
+ $ gem install sinatra-respond_to
89
+
90
+ ## CAVEATS:
91
+ Due to the way respond\_to works, all incoming requests have the extension striped from the request.path\_info. This causes routes like the following to fail.
92
+
93
+ get '/style.css' do
94
+ sass :style # => renders views/style.sass
95
+ end
96
+
97
+ They need to be changed to the following.
98
+
99
+ get '/style' do
100
+ sass :style # => renders views/style.css.sass
101
+ end
102
+
103
+ If you want to ensure the route only gets called for css requests try this. This will use sinatra's built in accept header matching.
104
+
105
+ get '/style', :provides => :css do
106
+ sass :style
107
+ end
108
+
109
+ ## DEVELOPERS:
110
+
111
+ After checking out the source, run:
112
+
113
+ $ bundle install
114
+ $ rake spec
115
+
116
+ This task will install any missing dependencies, run the tests/specs, and generate the RDoc.
117
+
118
+ ## Contributors
119
+
120
+ [Contributors](https://github.com/cehoffman/sinatra-respond_to/contributors)
121
+
122
+ ## LICENSE:
123
+
124
+ (The MIT License)
125
+
126
+ Copyright (c) 2009-2010 Chris Hoffman
127
+
128
+ Permission is hereby granted, free of charge, to any person obtaining
129
+ a copy of this software and associated documentation files (the
130
+ 'Software'), to deal in the Software without restriction, including
131
+ without limitation the rights to use, copy, modify, merge, publish,
132
+ distribute, sublicense, and/or sell copies of the Software, and to
133
+ permit persons to whom the Software is furnished to do so, subject to
134
+ the following conditions:
135
+
136
+ The above copyright notice and this permission notice shall be
137
+ included in all copies or substantial portions of the Software.
138
+
139
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
140
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
141
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
142
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
143
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
144
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
145
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ begin
6
+ require 'rspec/core/rake_task'
7
+ desc 'Run specs'
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.rcov = true
10
+ t.rcov_opts = ['--sort coverage --text-summary --sort-reverse']
11
+ t.rcov_opts << "--comments --exclude spec,pkg,#{ENV['GEM_HOME']}"
12
+ end
13
+ rescue LoadError
14
+ puts 'RSpec not available, try a bundle install'
15
+ end
@@ -0,0 +1,207 @@
1
+ require 'sinatra/base'
2
+
3
+ # Accept header parsing was looked at but deemed
4
+ # too much of an irregularity to deal with. Problems with the header
5
+ # differences from IE, Firefox, Safari, and every other UA causes
6
+ # problems with the expected output. The general expected behavior
7
+ # would be serve html when no extension provided, but most UAs say
8
+ # they will accept application/xml with out a quality indicator, meaning
9
+ # you'd get the xml block served insead. Just plain retarded, use the
10
+ # extension and you'll never be suprised.
11
+
12
+ module Sinatra
13
+ module RespondTo
14
+ class UnhandledFormat < Sinatra::NotFound; end
15
+ class MissingTemplate < Sinatra::NotFound
16
+ def code; 500 end
17
+ end
18
+
19
+ def self.registered(app)
20
+ app.helpers RespondTo::Helpers
21
+
22
+ app.set :default_content, :html
23
+ app.set :assume_xhr_is_js, true
24
+
25
+ # We remove the trailing extension so routes
26
+ # don't have to be of the style
27
+ #
28
+ # get '/resouce.:format'
29
+ #
30
+ # They can instead be of the style
31
+ #
32
+ # get '/resource'
33
+ #
34
+ # and the format will automatically be available in <tt>format</tt>
35
+ app.before do
36
+ # Let through sinatra image urls in development
37
+ next if self.class.development? && request.path_info =~ %r{/__sinatra__/.*?.png}
38
+
39
+ unless options.static? && options.public? && (request.get? || request.head?) && static_file?(request.path_info)
40
+ if request.params.has_key? 'format'
41
+ format params['format']
42
+ else
43
+ # Sinatra relies on a side-effect from path_info= to
44
+ # determine its routes. A direct string change (e.g., sub!)
45
+ # would bypass that -- and fail to have the effect we're looking
46
+ # for.
47
+ request.path_info = request.path_info.sub %r{\.([^\./]+)$}, ''
48
+
49
+ format $1 || (request.xhr? && options.assume_xhr_is_js? ? :js : options.default_content)
50
+ end
51
+ end
52
+ end
53
+
54
+ app.configure :development do |dev|
55
+ dev.error UnhandledFormat do
56
+ content_type :html, :charset => 'utf-8'
57
+
58
+ (<<-HTML).gsub(/^ {10}/, '')
59
+ <!DOCTYPE html>
60
+ <html>
61
+ <head>
62
+ <style type="text/css">
63
+ body { text-align:center;font-family:helvetica,arial;font-size:22px;
64
+ color:#888;margin:20px}
65
+ #c {margin:0 auto;width:500px;text-align:left}
66
+ </style>
67
+ </head>
68
+ <body>
69
+ <h2>Sinatra doesn't know this ditty.</h2>
70
+ <img src='/__sinatra__/404.png'>
71
+ <div id="c">
72
+ Try this:
73
+ <pre>#{request.request_method.downcase} '#{request.path_info}' do\n respond_to do |wants|\n wants.#{format} { "Hello World" }\n end\nend</pre>
74
+ </div>
75
+ </body>
76
+ </html>
77
+ HTML
78
+ end
79
+
80
+ dev.error MissingTemplate do
81
+ content_type :html, :charset => 'utf-8'
82
+ response.status = request.env['sinatra.error'].code
83
+
84
+ engine = request.env['sinatra.error'].message.split('.').last
85
+ engine = 'haml' unless ['haml', 'builder', 'erb'].include? engine
86
+
87
+ path = File.basename(request.path_info)
88
+ path = "root" if path.nil? || path.empty?
89
+
90
+ format = engine == 'builder' ? 'xml' : 'html'
91
+
92
+ layout = case engine
93
+ when 'haml' then "!!!\n%html\n %body= yield"
94
+ when 'erb' then "<html>\n <body>\n <%= yield %>\n </body>\n</html>"
95
+ when 'builder' then "xml << yield"
96
+ end
97
+
98
+ layout = "<small>app.#{format}.#{engine}</small>\n<pre>#{escape_html(layout)}</pre>"
99
+
100
+ (<<-HTML).gsub(/^ {10}/, '')
101
+ <!DOCTYPE html>
102
+ <html>
103
+ <head>
104
+ <style type="text/css">
105
+ body { text-align:center;font-family:helvetica,arial;font-size:22px;
106
+ color:#888;margin:20px}
107
+ #c {margin:0 auto;width:500px;text-align:left;}
108
+ small {float:right;clear:both;}
109
+ pre {clear:both;}
110
+ </style>
111
+ </head>
112
+ <body>
113
+ <h2>Sinatra can't find #{request.env['sinatra.error'].message}</h2>
114
+ <img src='/__sinatra__/500.png'>
115
+ <div id="c">
116
+ Try this:<br />
117
+ #{layout}
118
+ <small>#{path}.#{format}.#{engine}</small>
119
+ <pre>Hello World!</pre>
120
+ <small>application.rb</small>
121
+ <pre>#{request.request_method.downcase} '#{request.path_info}' do\n respond_to do |wants|\n wants.#{engine == 'builder' ? 'xml' : 'html'} { #{engine} :#{path}#{",\n#{' '*32}layout => :app" if layout} }\n end\nend</pre>
122
+ </div>
123
+ </body>
124
+ </html>
125
+ HTML
126
+ end
127
+
128
+ end
129
+
130
+ app.class_eval do
131
+ include RenderExtension
132
+ end
133
+ end
134
+
135
+ module RenderExtension
136
+ # Changes in 1.0 Sinatra reuse render for layout so we store the
137
+ # original value to tell us if this is an automatic attempt to do a
138
+ # layout call. If it is, it might fail with Errno::ENOENT and we want
139
+ # to pass that back to sinatra since it isn't a MissingTemplate error
140
+ def render(*args, &block)
141
+ assumed_layout = args[1] == :layout
142
+ args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
143
+ super(*args, &block)
144
+ rescue Errno::ENOENT => e
145
+ raise MissingTemplate, "#{args[1]}.#{args[0]}" unless assumed_layout
146
+ raise e
147
+ end
148
+ private :render
149
+ end
150
+
151
+ module Helpers
152
+ # Patch the content_type function to remember the set type
153
+ # This helps cut down on time in the format helper so it
154
+ # doesn't have to do a reverse lookup on the header
155
+ def content_type(*args)
156
+ @_format = args.first.to_sym
157
+ super(*args)
158
+ end
159
+
160
+ def format(val=nil)
161
+ unless val.nil?
162
+ mime_type = ::Sinatra::Base.mime_type(val)
163
+ fail "Unknown media type #{val}\nTry registering the extension with a mime type" if mime_type.nil?
164
+
165
+ @_format = val.to_sym
166
+ response['Content-Type'] ? response['Content-Type'].sub!(/^[^;]+/, mime_type) : content_type(@_format)
167
+ end
168
+
169
+ @_format
170
+ end
171
+
172
+ # This is mostly just a helper so request.path_info isn't changed when
173
+ # serving files from the public directory
174
+ def static_file?(path)
175
+ public_dir = File.expand_path(options.public)
176
+ path = File.expand_path(File.join(public_dir, unescape(path)))
177
+
178
+ path[0, public_dir.length] == public_dir && File.file?(path)
179
+ end
180
+
181
+ def charset(val=nil)
182
+ fail "Content-Type must be set in order to specify a charset" if response['Content-Type'].nil?
183
+
184
+ if response['Content-Type'] =~ /charset=[^;]+/
185
+ response['Content-Type'].sub!(/charset=[^;]+/, (val == '' && '') || "charset=#{val}")
186
+ else
187
+ response['Content-Type'] += ";charset=#{val}"
188
+ end unless val.nil?
189
+
190
+ response['Content-Type'][/charset=([^;]+)/, 1]
191
+ end
192
+
193
+ def respond_to(&block)
194
+ wants = {}
195
+ def wants.method_missing(type, *args, &handler)
196
+ ::Sinatra::Base.send(:fail, "Unknown media type for respond_to: #{type}\nTry registering the extension with a mime type") if ::Sinatra::Base.mime_type(type).nil?
197
+ self[type] = handler
198
+ end
199
+
200
+ yield wants
201
+
202
+ raise UnhandledFormat if wants[format].nil?
203
+ wants[format].call
204
+ end
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module RespondTo
3
+ Version = '0.7.1'
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sinatra/respond_to/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'ajsharp-sinatra-respond_to'
6
+ s.version = Sinatra::RespondTo::Version
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ['Chris Hoffman']
9
+ s.email = ['cehoffman@gmail.com']
10
+ s.homepage = 'http://github.com/cehoffman/sinatra-respond_to'
11
+ s.summary = 'A respond_to style Rails block for baked-in web service support in Sinatra'
12
+
13
+ s.required_rubygems_version = '>= 1.3.6'
14
+
15
+ s.add_runtime_dependency 'sinatra', '~> 1.2'
16
+
17
+ s.add_development_dependency 'rspec', '~> 2.5.0'
18
+ s.add_development_dependency 'rack-test', '~> 0.5.6'
19
+ s.add_development_dependency 'rcov', '~> 0.9.8'
20
+ s.add_development_dependency 'builder', '>= 2.0'
21
+ s.add_development_dependency 'haml', '>= 3.0'
22
+ s.add_development_dependency 'bundler', '~> 1.0.10'
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
27
+ s.require_path = 'lib'
28
+ end
@@ -0,0 +1,9 @@
1
+ class ProductionErrorApp < Sinatra::Base
2
+ set :environment, :production
3
+ register Sinatra::RespondTo
4
+ get '/missing-template' do
5
+ respond_to do |wants|
6
+ wants.html { haml :missing }
7
+ end
8
+ end
9
+ end
File without changes
@@ -0,0 +1 @@
1
+ A static file
@@ -0,0 +1,55 @@
1
+ require 'sinatra/base'
2
+ require 'erb'
3
+ require 'haml'
4
+ require 'sass'
5
+ require 'builder'
6
+
7
+ class TestApp < Sinatra::Base
8
+ register Sinatra::RespondTo
9
+
10
+ set :views, File.join(File.dirname(__FILE__), 'views')
11
+ set :public, File.join(File.dirname(__FILE__), 'public')
12
+
13
+ get '/resource' do
14
+ respond_to do |wants|
15
+ wants.html { haml :resource }
16
+ wants.json { "We got some json" }
17
+ wants.xml { builder :resource }
18
+ wants.js { erb :resource }
19
+ wants.png { }
20
+ end
21
+ end
22
+
23
+ get '/default_charset' do
24
+ respond_to do |wants|
25
+ wants.html { "Should set charcter set to default_charset" }
26
+ end
27
+ end
28
+
29
+ get '/iso-8859-1' do
30
+ respond_to do |wants|
31
+ wants.html { charset 'iso-8859-1'; "Should have character set of iso-8859-1" }
32
+ end
33
+ end
34
+
35
+ get '/normal-no-respond_to' do
36
+ "Just some plain old text"
37
+ end
38
+
39
+ get '/style.css' do
40
+ "This route should fail"
41
+ end
42
+
43
+ get '/style-no-extension', :provides => :css do
44
+ "Should succeed only when browser accepts text/css"
45
+ end
46
+
47
+ get '/missing-template' do
48
+ respond_to do |wants|
49
+ wants.html { haml :missing }
50
+ wants.xml { builder :missing }
51
+ wants.js { erb :missing }
52
+ wants.css { sass :missing }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1 @@
1
+ Unreachable static file
@@ -0,0 +1,2 @@
1
+ %html
2
+ %body= yield
@@ -0,0 +1 @@
1
+ Hello from HTML
@@ -0,0 +1,3 @@
1
+ $(function () {
2
+ return '<%= "Hiya from javascript" %>'
3
+ });
@@ -0,0 +1 @@
1
+ xml.root "Some XML"
@@ -0,0 +1,389 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe Sinatra::RespondTo do
4
+ def mime_type(sym)
5
+ ::Sinatra::Base.mime_type(sym)
6
+ end
7
+
8
+ describe "options" do
9
+ it "should initialize with :default_content set to :html" do
10
+ TestApp.default_content.should == :html
11
+ end
12
+
13
+ it "should initialize with :assume_xhr_is_js set to true" do
14
+ TestApp.assume_xhr_is_js == true
15
+ end
16
+ end
17
+
18
+ describe "assume_xhr_is_js" do
19
+ it "should set the content type to application/javascript for an XMLHttpRequest" do
20
+ header 'X_REQUESTED_WITH', 'XMLHttpRequest'
21
+
22
+ get '/resource'
23
+
24
+ last_response['Content-Type'].should =~ %r{#{mime_type(:js)}}
25
+ end
26
+
27
+ it "should not set the content type to application/javascript for an XMLHttpRequest when assume_xhr_is_js is false" do
28
+ TestApp.disable :assume_xhr_is_js
29
+ header 'X_REQUESTED_WITH', 'XMLHttpRequest'
30
+ get '/resource'
31
+
32
+ last_response['Content-Type'].should_not =~ %r{#{mime_type(:js)}}
33
+
34
+ # Put back the option, no side effects here
35
+ TestApp.enable :assume_xhr_is_js
36
+ end
37
+
38
+ it "should not set the content type to application/javascript for an XMLHttpRequest to an explicit extension" do
39
+ header 'X_REQUESTED_WITH', 'XMLHttpRequest'
40
+
41
+ get '/resource.json'
42
+
43
+ last_response['Content-Type'].should =~ %r{#{mime_type(:json)}}
44
+ end
45
+ end
46
+
47
+ describe "extension routing" do
48
+ it "should use a format parameter before sniffing out the extension" do
49
+ get "/resource?format=xml"
50
+ last_response.body.should =~ %r{\s*<root>Some XML</root>\s*}
51
+ end
52
+
53
+ it "breaks routes expecting an extension" do
54
+ # In test_app.rb the route is defined as get '/style.css' instead of get '/style'
55
+ get "/style.css"
56
+
57
+ last_response.should_not be_ok
58
+ end
59
+
60
+ it "should pick the default content option for routes with out an extension, and render haml templates" do
61
+ get "/resource"
62
+
63
+ last_response.body.should =~ %r{\s*<html>\s*<body>Hello from HTML</body>\s*</html>\s*}
64
+ end
65
+
66
+ it "should render for a template using builder" do
67
+ get "/resource.xml"
68
+
69
+ last_response.body.should =~ %r{\s*<root>Some XML</root>\s*}
70
+ end
71
+
72
+ it "should render for a template using erb" do
73
+ get "/resource.js"
74
+
75
+ last_response.body.should =~ %r{'Hiya from javascript'}
76
+ end
77
+
78
+ it "should return string literals in block" do
79
+ get "/resource.json"
80
+
81
+ last_response.body.should =~ %r{We got some json}
82
+ end
83
+
84
+ # This will fail if the above is failing
85
+ it "should set the appropriate content-type for route with an extension" do
86
+ get "/resource.xml"
87
+
88
+ last_response['Content-Type'].should =~ %r{#{mime_type(:xml)}}
89
+ end
90
+
91
+ it "should honor a change in character set in block" do
92
+ get "/iso-8859-1"
93
+
94
+ last_response['Content-Type'].should =~ %r{charset=iso-8859-1}
95
+ end
96
+
97
+ it "should return not found when path does not exist" do
98
+ get "/nonexistant-path.txt"
99
+
100
+ last_response.status.should == 404
101
+ end
102
+
103
+ describe "for static files" do
104
+ before(:all) do
105
+ TestApp.enable :static
106
+ end
107
+
108
+ after(:all) do
109
+ TestApp.disable :static
110
+ end
111
+
112
+ it "should allow serving static files from public directory" do
113
+ get '/static.txt'
114
+
115
+ last_response.body.should == "A static file"
116
+ end
117
+
118
+ it "should only serve files when static routing is enabled" do
119
+ TestApp.disable :static
120
+ get '/static.txt'
121
+
122
+ last_response.should_not be_ok
123
+ last_response.body.should_not == "A static file"
124
+
125
+ TestApp.enable :static
126
+ end
127
+
128
+ it "should not allow serving static files from outside the public directory" do
129
+ get '/../unreachable_static.txt'
130
+
131
+ last_response.should_not be_ok
132
+ last_response.body.should_not == "Unreachable static file"
133
+ end
134
+ end
135
+ end
136
+
137
+ describe "routes not using respond_to" do
138
+ it "should set the default content type when no extension" do
139
+ get "/normal-no-respond_to"
140
+
141
+ last_response['Content-Type'].should =~ %r{#{mime_type(TestApp.default_content)}}
142
+ end
143
+
144
+ it "should set the appropriate content type when given an extension" do
145
+ get "/normal-no-respond_to.css"
146
+
147
+ last_response['Content-Type'].should =~ %r{#{mime_type(:css)}}
148
+ end
149
+ end
150
+
151
+ describe "error pages in production" do
152
+ before(:each) do
153
+ @app = Rack::Builder.new { run ::ProductionErrorApp }
154
+ end
155
+
156
+ describe Sinatra::RespondTo::MissingTemplate do
157
+ it "should return 404 status when looking for a missing template in production" do
158
+ get '/missing-template'
159
+
160
+ last_response.status.should == 404
161
+ last_response.body.should_not =~ /Sinatra can't find/
162
+ end
163
+ end
164
+
165
+ describe Sinatra::RespondTo::UnhandledFormat do
166
+ it "should return with a 404 when an extension is not supported in production" do
167
+ get '/missing-template.txt'
168
+
169
+ last_response.status.should == 404
170
+ last_response.body.should_not =~ /respond_to/
171
+ end
172
+ end
173
+ end
174
+
175
+ describe "error pages in development:" do
176
+
177
+ it "should allow access to the /__sinatra__/*.png images" do
178
+ get '/__sinatra__/404.png'
179
+
180
+ last_response.should be_ok
181
+ end
182
+
183
+ describe Sinatra::RespondTo::MissingTemplate do
184
+ it "should return 500 status when looking for a missing template" do
185
+ get '/missing-template'
186
+
187
+ last_response.status.should == 500
188
+ end
189
+
190
+ it "should provide a helpful generic error message for a missing template when in development" do
191
+ get '/missing-template.css'
192
+
193
+ last_response.body.should =~ /missing-template\.html\.haml/
194
+ last_response.body.should =~ %r{get '/missing-template' do respond_to do |wants| wants.html \{ haml :missing-template, layout => :app \} end end}
195
+ end
196
+
197
+ it "should show the /__sinatra__/500.png" do
198
+ get '/missing-template'
199
+
200
+ last_response.body.should =~ %r{src='/__sinatra__/500.png'}
201
+ end
202
+
203
+ it "should provide a contextual code example for the template engine" do
204
+ # Haml
205
+ get '/missing-template'
206
+
207
+ last_response.body.should =~ %r{app.html.haml}
208
+ last_response.body.should =~ %r{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}
210
+
211
+ # ERB
212
+ get '/missing-template.js'
213
+
214
+ last_response.body.should =~ %r{app.html.erb}
215
+ last_response.body.should =~ %r{missing-template.html.erb}
216
+ last_response.body.should =~ %r{get '/missing-template' do respond_to do |wants| wants.html \{ erb :missing-template, layout => :app \} end end}
217
+
218
+ # Builder
219
+ get '/missing-template.xml'
220
+
221
+ last_response.body.should =~ %r{app.xml.builder}
222
+ last_response.body.should =~ %r{missing-template.xml.builder}
223
+ last_response.body.should =~ %r{get '/missing-template' do respond_to do |wants| wants.xml \{ builder :missing-template, layout => :app \} end end}
224
+ end
225
+ end
226
+
227
+ describe Sinatra::RespondTo::UnhandledFormat do
228
+ it "should return with a 404 when an extension is not supported" do
229
+ get '/missing-template.txt'
230
+
231
+ last_response.status.should == 404
232
+ end
233
+
234
+ it "should provide a helpful error message for an unhandled format" do
235
+ get '/missing-template.txt'
236
+
237
+ last_response.body.should =~ %r{get '/missing-template' do respond_to do |wants| wants.txt \{ "Hello World" \} end end}
238
+ end
239
+
240
+ it "should show the /__sinatra__/404.png" do
241
+ get '/missing-template.txt'
242
+
243
+ last_response.body.should =~ %r{src='/__sinatra__/404.png'}
244
+ end
245
+ end
246
+ end
247
+
248
+ describe "helpers:" do
249
+ include Sinatra::Helpers
250
+ include Sinatra::RespondTo::Helpers
251
+
252
+ before(:each) do
253
+ stub!(:response).and_return({'Content-Type' => 'text/html'})
254
+ end
255
+
256
+ describe "charset" do
257
+ it "should set the working charset when called with a non blank string" do
258
+ response['Content-Type'].should_not =~ /charset/
259
+
260
+ charset 'utf-8'
261
+
262
+ response['Content-Type'].split(';').should include("charset=utf-8")
263
+ end
264
+
265
+ it "should remove the charset when called with a blank string" do
266
+ charset 'utf-8'
267
+ charset ''
268
+
269
+ response['Content-Type'].should_not =~ /charset/
270
+ end
271
+
272
+ it "should return the current charset when called with nothing" do
273
+ charset 'utf-8'
274
+
275
+ charset.should == 'utf-8'
276
+ end
277
+
278
+ it "should fail when the response does not have a Content-Type" do
279
+ response.delete('Content-Type')
280
+
281
+ lambda { charset }.should raise_error
282
+ end
283
+
284
+ it "should not modify the Content-Type when given no argument" do
285
+ response['Content-Type'] = "text/html;charset=iso-8859-1"
286
+
287
+ charset
288
+
289
+ response['Content-Type'].should == "text/html;charset=iso-8859-1"
290
+ end
291
+ end
292
+
293
+ describe "format" do
294
+ before(:each) do
295
+ stub!(:request).and_return(Sinatra::Request.new({}))
296
+ end
297
+
298
+ it "should set the correct mime type when given an extension" do
299
+ format :xml
300
+
301
+ response['Content-Type'].split(';').should include(mime_type(:xml))
302
+ end
303
+
304
+ it "should fail when set to an unknown extension type" do
305
+ lambda { format :bogus }.should raise_error
306
+ end
307
+
308
+ it "should return the current mime type extension" do
309
+ format :js
310
+
311
+ format.should == :js
312
+ end
313
+
314
+ it "should not modify the Content-Type when given no argument" do
315
+ response['Content-Type'] = "application/xml;charset=utf-8"
316
+
317
+ format
318
+
319
+ response['Content-Type'].should == "application/xml;charset=utf-8"
320
+ end
321
+
322
+ it "should not return nil when only content_type sets headers" do
323
+ settings = mock('settings').as_null_object
324
+ stub!(:settings).and_return(settings)
325
+
326
+ content_type :xhtml
327
+
328
+ format.should == :xhtml
329
+ end
330
+ end
331
+
332
+ describe "static_file?" do
333
+ before(:all) do
334
+ TestApp.enable :static
335
+ @static_folder = "/static folder/"
336
+ @reachable_static_file = "/static.txt"
337
+ @unreachable_static_file = "/../unreachable_static.txt"
338
+ end
339
+
340
+ after(:all) do
341
+ TestApp.disable :static
342
+ end
343
+
344
+ def options
345
+ TestApp
346
+ end
347
+
348
+ def unescape(path)
349
+ Rack::Utils.unescape(path)
350
+ end
351
+
352
+ it "should return true if the request path points to a file in the public directory" do
353
+ static_file?(@reachable_static_file).should be_true
354
+ end
355
+
356
+ it "should return false when pointing to files outside of the public directory" do
357
+ static_file?(@unreachable_static_file).should be_false
358
+ end
359
+
360
+ it "should return false when the path is for a folder" do
361
+ static_file?(@static_folder).should be_false
362
+ end
363
+ end
364
+
365
+ describe "respond_to" do
366
+ before(:each) do
367
+ stub!(:request).and_return(Sinatra::Request.new({}))
368
+ end
369
+
370
+ it "should fail for an unknown extension" do
371
+ lambda do
372
+ respond_to do |wants|
373
+ wants.bogus
374
+ end
375
+ end.should raise_error
376
+ end
377
+
378
+ it "should call the block corresponding to the current format" do
379
+ format :html
380
+
381
+ respond_to do |wants|
382
+ wants.js { "Some JS" }
383
+ wants.html { "Some HTML" }
384
+ wants.xml { "Some XML" }
385
+ end.should == "Some HTML"
386
+ end
387
+ end
388
+ end
389
+ end
@@ -0,0 +1,20 @@
1
+ #$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ require 'rspec'
6
+ require 'rack/test'
7
+
8
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'sinatra', 'respond_to')
9
+ require File.join(File.dirname(__FILE__), 'app', 'test_app')
10
+ require File.join(File.dirname(__FILE__), 'app', 'production_error_app')
11
+
12
+ RSpec.configure do |config|
13
+ def app
14
+ @app ||= ::Rack::Builder.new do
15
+ run ::TestApp
16
+ end
17
+ end
18
+
19
+ config.include ::Rack::Test::Methods
20
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ajsharp-sinatra-respond_to
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.7.1
6
+ platform: ruby
7
+ authors:
8
+ - Chris Hoffman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-16 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: sinatra
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: "1.2"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 2.5.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rack-test
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 0.5.6
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 0.9.8
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: builder
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "2.0"
69
+ type: :development
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: haml
73
+ prerelease: false
74
+ requirement: &id006 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "3.0"
80
+ type: :development
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: bundler
84
+ prerelease: false
85
+ requirement: &id007 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ~>
89
+ - !ruby/object:Gem::Version
90
+ version: 1.0.10
91
+ type: :development
92
+ version_requirements: *id007
93
+ description:
94
+ email:
95
+ - cehoffman@gmail.com
96
+ executables: []
97
+
98
+ extensions: []
99
+
100
+ extra_rdoc_files: []
101
+
102
+ files:
103
+ - .gitignore
104
+ - .rspec
105
+ - Changelog.rdoc
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - README.md
109
+ - Rakefile
110
+ - lib/sinatra/respond_to.rb
111
+ - lib/sinatra/respond_to/version.rb
112
+ - sinatra-respond_to.gemspec
113
+ - spec/app/production_error_app.rb
114
+ - spec/app/public/static folder/.keep
115
+ - spec/app/public/static.txt
116
+ - spec/app/test_app.rb
117
+ - spec/app/unreachable_static.txt
118
+ - spec/app/views/layout.html.haml
119
+ - spec/app/views/resource.html.haml
120
+ - spec/app/views/resource.js.erb
121
+ - spec/app/views/resource.xml.builder
122
+ - spec/extension_spec.rb
123
+ - spec/spec_helper.rb
124
+ has_rdoc: true
125
+ homepage: http://github.com/cehoffman/sinatra-respond_to
126
+ licenses: []
127
+
128
+ post_install_message:
129
+ rdoc_options: []
130
+
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: "0"
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 1.3.6
145
+ requirements: []
146
+
147
+ rubyforge_project:
148
+ rubygems_version: 1.6.2
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: A respond_to style Rails block for baked-in web service support in Sinatra
152
+ test_files:
153
+ - spec/app/production_error_app.rb
154
+ - spec/app/public/static folder/.keep
155
+ - spec/app/public/static.txt
156
+ - spec/app/test_app.rb
157
+ - spec/app/unreachable_static.txt
158
+ - spec/app/views/layout.html.haml
159
+ - spec/app/views/resource.html.haml
160
+ - spec/app/views/resource.js.erb
161
+ - spec/app/views/resource.xml.builder
162
+ - spec/extension_spec.rb
163
+ - spec/spec_helper.rb