sinatra-respond_to 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,33 +1,37 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-respond_to (0.8.0)
4
+ sinatra-respond_to (0.9.0)
5
5
  sinatra (~> 1.3)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- builder (3.0.0)
10
+ builder (3.1.4)
11
11
  diff-lcs (1.1.3)
12
- haml (3.1.3)
13
- rack (1.3.5)
14
- rack-protection (1.1.4)
12
+ haml (3.1.7)
13
+ multi_json (1.5.0)
14
+ rack (1.5.1)
15
+ rack-protection (1.3.2)
15
16
  rack
16
- rack-test (0.5.7)
17
+ rack-test (0.6.2)
17
18
  rack (>= 1.0)
18
- rcov (0.9.11)
19
- rspec (2.5.0)
20
- rspec-core (~> 2.5.0)
21
- rspec-expectations (~> 2.5.0)
22
- rspec-mocks (~> 2.5.0)
23
- rspec-core (2.5.2)
24
- rspec-expectations (2.5.0)
25
- diff-lcs (~> 1.1.2)
26
- rspec-mocks (2.5.0)
27
- sass (3.1.10)
28
- sinatra (1.3.1)
29
- rack (~> 1.3, >= 1.3.4)
30
- rack-protection (~> 1.1, >= 1.1.2)
19
+ rspec (2.12.0)
20
+ rspec-core (~> 2.12.0)
21
+ rspec-expectations (~> 2.12.0)
22
+ rspec-mocks (~> 2.12.0)
23
+ rspec-core (2.12.2)
24
+ rspec-expectations (2.12.1)
25
+ diff-lcs (~> 1.1.3)
26
+ rspec-mocks (2.12.2)
27
+ sass (3.2.5)
28
+ simplecov (0.7.1)
29
+ multi_json (~> 1.0)
30
+ simplecov-html (~> 0.7.1)
31
+ simplecov-html (0.7.1)
32
+ sinatra (1.3.4)
33
+ rack (~> 1.4)
34
+ rack-protection (~> 1.3)
31
35
  tilt (~> 1.3, >= 1.3.3)
32
36
  tilt (1.3.3)
33
37
 
@@ -36,10 +40,10 @@ PLATFORMS
36
40
 
37
41
  DEPENDENCIES
38
42
  builder (>= 2.0)
39
- bundler (~> 1.1.rc)
43
+ bundler (~> 1.2)
40
44
  haml (>= 3.0)
41
- rack-test (~> 0.5.6)
42
- rcov (~> 0.9.8)
43
- rspec (~> 2.5.0)
45
+ rack-test (~> 0.6.2)
46
+ rspec (~> 2.12.0)
44
47
  sass (>= 3.0)
48
+ simplecov (~> 0.7.1)
45
49
  sinatra-respond_to!
data/README.md CHANGED
@@ -10,6 +10,8 @@ A respond\_to style Rails block for baked-in web service support in Sinatra
10
10
 
11
11
  * Handles setting the content type depending on what is being served
12
12
  * Automatically can adjust XMLHttpRequests to return Javascript
13
+ * Recognizes requests based on HTTP\_ACCEPT header parsing. Order of
14
+ priority is `format` parameter, extension, accept header, default content.
13
15
 
14
16
  ## SYNOPSIS:
15
17
 
@@ -17,7 +19,7 @@ Allows urls of the form **/posts**, **/posts.rss**, and **/posts?format=atom** t
17
19
 
18
20
  require 'sinatra'
19
21
  require 'sinatra/respond_to'
20
-
22
+
21
23
  Sinatra::Application.register Sinatra::RespondTo
22
24
 
23
25
  get '/posts' do
@@ -79,9 +81,9 @@ There a few options available for configuring the default behavior of respond\_t
79
81
 
80
82
  ## REQUIREMENTS:
81
83
 
82
- * sinatra 1.1
84
+ * sinatra 1.3
83
85
 
84
- If you would like to use Sinatra 1.0, use version `0.5.0`.
86
+ If you would like to use Sinatra 1.2, use version `0.7.0`.
85
87
 
86
88
  ## INSTALL:
87
89
 
@@ -123,7 +125,7 @@ This task will install any missing dependencies, run the tests/specs, and genera
123
125
 
124
126
  (The MIT License)
125
127
 
126
- Copyright (c) 2009-2010 Chris Hoffman
128
+ Copyright (c) 2009-2013 Chris Hoffman
127
129
 
128
130
  Permission is hereby granted, free of charge, to any person obtaining
129
131
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -9,6 +9,12 @@ begin
9
9
  t.rcov_opts = ['--sort coverage --text-summary --sort-reverse']
10
10
  t.rcov_opts << "--comments --exclude spec,pkg,#{ENV['GEM_HOME']}"
11
11
  end
12
+
13
+ desc 'Run tests with coverage'
14
+ task :coverage do
15
+ ENV['COVERAGE'] = 'true'
16
+ Rake::Task['spec'].invoke
17
+ end
12
18
  rescue LoadError
13
19
  puts 'RSpec not available, try a bundle install'
14
20
  end
@@ -1,21 +1,12 @@
1
1
  require 'sinatra/base'
2
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
3
  module Sinatra
13
4
  module RespondTo
14
5
  class UnhandledFormat < Sinatra::NotFound; end
15
6
  class MissingTemplate < Sinatra::NotFound
16
7
  def code; 404 end
17
8
  end
18
-
9
+
19
10
  def self.registered(app)
20
11
  app.helpers RespondTo::Helpers
21
12
 
@@ -39,14 +30,39 @@ module Sinatra
39
30
  unless settings.static? && settings.public_folder? && (request.get? || request.head?) && static_file?(request.path_info)
40
31
  if request.params.has_key? 'format'
41
32
  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
33
 
49
- format $1 || (request.xhr? && settings.assume_xhr_is_js? ? :js : settings.default_content)
34
+ # Rewrite the accept header with the determined format to allow
35
+ # downstream middleware to make use the the mime type
36
+ env['HTTP_ACCEPT'] = ::Sinatra::Base.mime_type(format)
37
+ request.accept.replace [env['HTTP_ACCEPT']]
38
+ else
39
+ # Consider first Accept type as default, otherwise
40
+ # fall back to settings.default_content
41
+ default_content = Rack::Mime::MIME_TYPES.invert[request.accept.first]
42
+ default_content = default_content ? default_content[1..-1] : settings.default_content
43
+
44
+ # Special case, as the specified default_content may use a different symbol than that
45
+ # found through lookup based on Content-Type
46
+ default_content = settings.default_content if
47
+ default_content != settings.default_content &&
48
+ ::Sinatra::Base.mime_type(default_content) == ::Sinatra::Base.mime_type(settings.default_content)
49
+
50
+ ext = $1 if request.path_info.match(%r{\.([^\./]+)$})
51
+ if ext
52
+ # Sinatra relies on a side-effect from path_info= to determine
53
+ # its routes. A direct string change (e.g., sub!) would bypass
54
+ # that and fail to have the effect we're looking for.
55
+ request.path_info = request.path_info[0..-(ext.length+2)]
56
+
57
+ format ext
58
+
59
+ # Rewrite the accept header with the determined format to allow
60
+ # downstream middleware to make use the the mime type
61
+ env['HTTP_ACCEPT'] = ::Sinatra::Base.mime_type(format)
62
+ request.accept.replace [env['HTTP_ACCEPT']]
63
+ else
64
+ format(request.xhr? && settings.assume_xhr_is_js? ? :js : default_content)
65
+ end
50
66
  end
51
67
  end
52
68
  end
@@ -192,6 +208,15 @@ module Sinatra
192
208
 
193
209
  yield wants
194
210
 
211
+ if wants[format].nil?
212
+ # Loop through request.accept in prioritized order looking for a Mime Type having a format
213
+ # that is recognized.
214
+ alt = nil
215
+ request.accept.each do |mime_type|
216
+ break if alt = wants.keys.detect {|k| ::Sinatra::Base.mime_type(k) == mime_type}
217
+ end
218
+ format alt if alt
219
+ end
195
220
  raise UnhandledFormat if wants[format].nil?
196
221
  wants[format].call
197
222
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module RespondTo
3
- Version = '0.8.0'
3
+ Version = '0.9.0'
4
4
  end
5
5
  end
@@ -10,17 +10,15 @@ Gem::Specification.new do |s|
10
10
  s.homepage = 'http://github.com/cehoffman/sinatra-respond_to'
11
11
  s.summary = 'A respond_to style Rails block for baked-in web service support in Sinatra'
12
12
 
13
- s.required_rubygems_version = '>= 1.3.6'
14
-
15
13
  s.add_runtime_dependency 'sinatra', '~> 1.3'
16
14
 
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'
15
+ s.add_development_dependency 'rspec', '~> 2.12.0'
16
+ s.add_development_dependency 'rack-test', '~> 0.6.2'
17
+ s.add_development_dependency 'simplecov', '~> 0.7.1'
20
18
  s.add_development_dependency 'builder', '>= 2.0'
21
19
  s.add_development_dependency 'haml', '>= 3.0'
22
20
  s.add_development_dependency 'sass', '>= 3.0'
23
- s.add_development_dependency 'bundler', '~> 1.1.rc'
21
+ s.add_development_dependency 'bundler', '~> 1.2'
24
22
 
25
23
  s.files = `git ls-files`.split("\n")
26
24
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -48,6 +48,8 @@ describe Sinatra::RespondTo do
48
48
  it "should use a format parameter before sniffing out the extension" do
49
49
  get "/resource?format=xml"
50
50
  last_response.body.should =~ %r{\s*<root>Some XML</root>\s*}
51
+ last_response.content_type.should include(mime_type(:xml))
52
+ last_request.env['HTTP_ACCEPT'].should == mime_type(:xml)
51
53
  end
52
54
 
53
55
  it "breaks routes expecting an extension" do
@@ -61,24 +63,31 @@ describe Sinatra::RespondTo do
61
63
  get "/resource"
62
64
 
63
65
  last_response.body.should =~ %r{\s*<html>\s*<body>Hello from HTML</body>\s*</html>\s*}
66
+ last_response.content_type.should include(mime_type(:html))
64
67
  end
65
68
 
66
69
  it "should render for a template using builder" do
67
70
  get "/resource.xml"
68
71
 
69
72
  last_response.body.should =~ %r{\s*<root>Some XML</root>\s*}
73
+ last_response.content_type.should include(mime_type(:xml))
74
+ last_request.env['HTTP_ACCEPT'].should == mime_type(:xml)
70
75
  end
71
76
 
72
77
  it "should render for a template using erb" do
73
78
  get "/resource.js"
74
79
 
75
80
  last_response.body.should =~ %r{'Hiya from javascript'}
81
+ last_response.content_type.should include(mime_type(:js))
82
+ last_request.env['HTTP_ACCEPT'].should == mime_type(:js)
76
83
  end
77
84
 
78
85
  it "should return string literals in block" do
79
86
  get "/resource.json"
80
87
 
81
88
  last_response.body.should =~ %r{We got some json}
89
+ last_response.content_type.should include(mime_type(:json))
90
+ last_request.env['HTTP_ACCEPT'].should == mime_type(:json)
82
91
  end
83
92
 
84
93
  # This will fail if the above is failing
@@ -134,6 +143,51 @@ describe Sinatra::RespondTo do
134
143
  end
135
144
  end
136
145
 
146
+ describe "accept routing" do
147
+ it "should use a format parameter before sniffing out the accept header" do
148
+ get "/resource?format=xml", {}, {'HTTP_ACCEPT' => "text/html"}
149
+ last_response.body.should =~ %r{\s*<root>Some XML</root>\s*}
150
+ last_response.content_type.should include(mime_type(:xml))
151
+ last_request.env['HTTP_ACCEPT'].should == mime_type(:xml)
152
+ end
153
+
154
+ it "should use an extension before sniffing out the accept header" do
155
+ get "/resource.xml", {}, {'HTTP_ACCEPT' => "text/html"}
156
+
157
+ last_response.body.should =~ %r{\s*<root>Some XML</root>\s*}
158
+ last_response.content_type.should include(mime_type(:xml))
159
+ last_request.env['HTTP_ACCEPT'].should == mime_type(:xml)
160
+ end
161
+
162
+ it "should render for a template using builder" do
163
+ get "/resource", {}, {'HTTP_ACCEPT' => "application/xml"}
164
+
165
+ last_response.body.should =~ %r{\s*<root>Some XML</root>\s*}
166
+ last_response.content_type.should include(mime_type(:xml))
167
+ end
168
+
169
+ it "should render for a template using haml" do
170
+ get "/resource", {}, {'HTTP_ACCEPT' => "text/html"}
171
+
172
+ last_response.body.should =~ %r{\s*<html>\s*<body>Hello from HTML</body>\s*</html>\s*}
173
+ last_response.content_type.should include(mime_type(:html))
174
+ end
175
+
176
+ it "should render for a template using json" do
177
+ get "/resource", {}, {'HTTP_ACCEPT' => "application/json"}
178
+
179
+ last_response.body.should =~ %r{We got some json}
180
+ last_response.content_type.should include(mime_type(:json))
181
+ end
182
+
183
+ it "should render for a template using erb" do
184
+ get "/resource", {}, {'HTTP_ACCEPT' => "application/javascript"}
185
+
186
+ last_response.body.should =~ %r{'Hiya from javascript'}
187
+ last_response.content_type.should include(mime_type(:js))
188
+ end
189
+ end
190
+
137
191
  describe "routes not using respond_to" do
138
192
  it "should set the default content type when no extension" do
139
193
  get "/normal-no-respond_to"
@@ -4,6 +4,11 @@ require 'rubygems'
4
4
  require 'bundler/setup'
5
5
  require 'rspec'
6
6
  require 'rack/test'
7
+ require 'simplecov'
8
+
9
+ SimpleCov.start do
10
+ add_filter 'vendor'
11
+ end if ENV['COVERAGE']
7
12
 
8
13
  require File.join(File.dirname(__FILE__), '..', 'lib', 'sinatra', 'respond_to')
9
14
  require File.join(File.dirname(__FILE__), 'app', 'test_app')
metadata CHANGED
@@ -1,104 +1,144 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-respond_to
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
5
4
  prerelease:
5
+ version: 0.9.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Chris Hoffman
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-17 00:00:00.000000000 Z
12
+ date: 2013-02-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: sinatra
16
- requirement: &70288457045960 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.3'
20
+ none: false
22
21
  type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ none: false
23
28
  prerelease: false
24
- version_requirements: *70288457045960
29
+ name: sinatra
25
30
  - !ruby/object:Gem::Dependency
26
- name: rspec
27
- requirement: &70288457044660 !ruby/object:Gem::Requirement
28
- none: false
31
+ requirement: !ruby/object:Gem::Requirement
29
32
  requirements:
30
33
  - - ~>
31
34
  - !ruby/object:Gem::Version
32
- version: 2.5.0
35
+ version: 2.12.0
36
+ none: false
33
37
  type: :development
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: 2.12.0
43
+ none: false
34
44
  prerelease: false
35
- version_requirements: *70288457044660
45
+ name: rspec
36
46
  - !ruby/object:Gem::Dependency
37
- name: rack-test
38
- requirement: &70288457043920 !ruby/object:Gem::Requirement
39
- none: false
47
+ requirement: !ruby/object:Gem::Requirement
40
48
  requirements:
41
49
  - - ~>
42
50
  - !ruby/object:Gem::Version
43
- version: 0.5.6
51
+ version: 0.6.2
52
+ none: false
44
53
  type: :development
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ version: 0.6.2
59
+ none: false
45
60
  prerelease: false
46
- version_requirements: *70288457043920
61
+ name: rack-test
47
62
  - !ruby/object:Gem::Dependency
48
- name: rcov
49
- requirement: &70288457043240 !ruby/object:Gem::Requirement
50
- none: false
63
+ requirement: !ruby/object:Gem::Requirement
51
64
  requirements:
52
65
  - - ~>
53
66
  - !ruby/object:Gem::Version
54
- version: 0.9.8
67
+ version: 0.7.1
68
+ none: false
55
69
  type: :development
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: 0.7.1
75
+ none: false
56
76
  prerelease: false
57
- version_requirements: *70288457043240
77
+ name: simplecov
58
78
  - !ruby/object:Gem::Dependency
59
- name: builder
60
- requirement: &70288457042560 !ruby/object:Gem::Requirement
61
- none: false
79
+ requirement: !ruby/object:Gem::Requirement
62
80
  requirements:
63
81
  - - ! '>='
64
82
  - !ruby/object:Gem::Version
65
83
  version: '2.0'
84
+ none: false
66
85
  type: :development
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '2.0'
91
+ none: false
67
92
  prerelease: false
68
- version_requirements: *70288457042560
93
+ name: builder
69
94
  - !ruby/object:Gem::Dependency
70
- name: haml
71
- requirement: &70288457041720 !ruby/object:Gem::Requirement
72
- none: false
95
+ requirement: !ruby/object:Gem::Requirement
73
96
  requirements:
74
97
  - - ! '>='
75
98
  - !ruby/object:Gem::Version
76
99
  version: '3.0'
100
+ none: false
77
101
  type: :development
102
+ version_requirements: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '3.0'
107
+ none: false
78
108
  prerelease: false
79
- version_requirements: *70288457041720
109
+ name: haml
80
110
  - !ruby/object:Gem::Dependency
81
- name: sass
82
- requirement: &70288457040280 !ruby/object:Gem::Requirement
83
- none: false
111
+ requirement: !ruby/object:Gem::Requirement
84
112
  requirements:
85
113
  - - ! '>='
86
114
  - !ruby/object:Gem::Version
87
115
  version: '3.0'
116
+ none: false
88
117
  type: :development
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '3.0'
123
+ none: false
89
124
  prerelease: false
90
- version_requirements: *70288457040280
125
+ name: sass
91
126
  - !ruby/object:Gem::Dependency
92
- name: bundler
93
- requirement: &70288457038780 !ruby/object:Gem::Requirement
94
- none: false
127
+ requirement: !ruby/object:Gem::Requirement
95
128
  requirements:
96
129
  - - ~>
97
130
  - !ruby/object:Gem::Version
98
- version: 1.1.rc
131
+ version: '1.2'
132
+ none: false
99
133
  type: :development
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ version: '1.2'
139
+ none: false
100
140
  prerelease: false
101
- version_requirements: *70288457038780
141
+ name: bundler
102
142
  description:
103
143
  email:
104
144
  - cehoffman@gmail.com
@@ -134,23 +174,26 @@ rdoc_options: []
134
174
  require_paths:
135
175
  - lib
136
176
  required_ruby_version: !ruby/object:Gem::Requirement
137
- none: false
138
177
  requirements:
139
178
  - - ! '>='
140
179
  - !ruby/object:Gem::Version
180
+ hash: 1859816800588862554
141
181
  version: '0'
142
182
  segments:
143
183
  - 0
144
- hash: 890127812341403240
145
- required_rubygems_version: !ruby/object:Gem::Requirement
146
184
  none: false
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
186
  requirements:
148
187
  - - ! '>='
149
188
  - !ruby/object:Gem::Version
150
- version: 1.3.6
189
+ hash: 1859816800588862554
190
+ version: '0'
191
+ segments:
192
+ - 0
193
+ none: false
151
194
  requirements: []
152
195
  rubyforge_project:
153
- rubygems_version: 1.8.11
196
+ rubygems_version: 1.8.23
154
197
  signing_key:
155
198
  specification_version: 3
156
199
  summary: A respond_to style Rails block for baked-in web service support in Sinatra