rack-jsonp 1.0.0 → 1.1.0
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 +3 -0
- data/Gemfile +2 -0
- data/LICENSE +1 -1
- data/README.rdoc +4 -1
- data/Rakefile +10 -5
- data/VERSION +1 -1
- data/lib/rack/jsonp.rb +2 -1
- data/rack-jsonp.gemspec +20 -6
- data/spec/rack_jsonp_spec.rb +29 -11
- data/spec/spec_helper.rb +1 -7
- metadata +71 -7
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
= rack-jsonp
|
2
2
|
|
3
3
|
A Rack middleware for providing JSON-P support. Most of it is taken from the original Rack::JSONP middleware present in rack-contrib.
|
4
|
+
|
4
5
|
Since I don't want to include the complete rack-contrib gem when all I need is the JSONP middleware, I created this gem.
|
5
6
|
|
7
|
+
== Contributions
|
8
|
+
* Patches by Sebastián Gamboa (sagmor) and Garrett Bjerkhoel (dewski).
|
6
9
|
|
7
10
|
== Copyright
|
8
11
|
|
9
|
-
Copyright (c) 2009 Cyril Rohr. See LICENSE for details.
|
12
|
+
Copyright (c) 2009-2010 Cyril Rohr. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require "bundler"
|
2
|
+
Bundler.setup
|
3
|
+
|
2
4
|
require 'rake'
|
3
5
|
|
4
6
|
begin
|
@@ -10,6 +12,12 @@ begin
|
|
10
12
|
gem.email = "cyril.rohr@gmail.com"
|
11
13
|
gem.homepage = "http://github.com/crohr/rack-jsonp"
|
12
14
|
gem.authors = ["Cyril Rohr"]
|
15
|
+
|
16
|
+
gem.add_dependency('rack')
|
17
|
+
gem.add_development_dependency('rake')
|
18
|
+
gem.add_development_dependency('jeweler')
|
19
|
+
gem.add_development_dependency('rspec', '< 2.0.0')
|
20
|
+
|
13
21
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
22
|
end
|
15
23
|
|
@@ -18,7 +26,7 @@ rescue LoadError
|
|
18
26
|
end
|
19
27
|
|
20
28
|
require 'spec/rake/spectask'
|
21
|
-
Spec::Rake::SpecTask.new(:spec) do |spec|
|
29
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
30
|
spec.libs << 'lib' << 'spec'
|
23
31
|
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
32
|
end
|
@@ -29,9 +37,6 @@ Spec::Rake::SpecTask.new(:rcov) do |spec|
|
|
29
37
|
spec.rcov = true
|
30
38
|
end
|
31
39
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
40
|
task :default => :spec
|
36
41
|
|
37
42
|
require 'rake/rdoctask'
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.1.0
|
data/lib/rack/jsonp.rb
CHANGED
@@ -24,9 +24,10 @@ module Rack
|
|
24
24
|
env['QUERY_STRING'] = env['QUERY_STRING'].split("&").delete_if{|param| param =~ /^(_|#{@callback_param})/}.join("&")
|
25
25
|
|
26
26
|
status, headers, response = @app.call(env)
|
27
|
-
if callback
|
27
|
+
if callback && headers['Content-Type'] =~ /json/i
|
28
28
|
response = pad(callback, response)
|
29
29
|
headers['Content-Length'] = response.first.length.to_s
|
30
|
+
headers['Content-Type'] = 'application/javascript'
|
30
31
|
elsif @carriage_return && headers['Content-Type'] =~ /json/i
|
31
32
|
# add a \n after the response if this is a json (not JSONP) response
|
32
33
|
response = carriage_return(response)
|
data/rack-jsonp.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rack-jsonp}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Cyril Rohr"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-09-07}
|
13
13
|
s.description = %q{A Rack middleware for providing JSON-P support.}
|
14
14
|
s.email = %q{cyril.rohr@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
|
+
"Gemfile",
|
22
23
|
"LICENSE",
|
23
24
|
"README.rdoc",
|
24
25
|
"Rakefile",
|
@@ -31,7 +32,7 @@ Gem::Specification.new do |s|
|
|
31
32
|
s.homepage = %q{http://github.com/crohr/rack-jsonp}
|
32
33
|
s.rdoc_options = ["--charset=UTF-8"]
|
33
34
|
s.require_paths = ["lib"]
|
34
|
-
s.rubygems_version = %q{1.3.
|
35
|
+
s.rubygems_version = %q{1.3.7}
|
35
36
|
s.summary = %q{A Rack middleware for providing JSON-P support.}
|
36
37
|
s.test_files = [
|
37
38
|
"spec/rack_jsonp_spec.rb",
|
@@ -42,9 +43,22 @@ Gem::Specification.new do |s|
|
|
42
43
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
44
|
s.specification_version = 3
|
44
45
|
|
45
|
-
if Gem::Version.new(Gem::
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_runtime_dependency(%q<rack>, [">= 0"])
|
48
|
+
s.add_development_dependency(%q<rake>, [">= 0"])
|
49
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
50
|
+
s.add_development_dependency(%q<rspec>, ["< 2.0.0"])
|
46
51
|
else
|
52
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
53
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
54
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
55
|
+
s.add_dependency(%q<rspec>, ["< 2.0.0"])
|
47
56
|
end
|
48
57
|
else
|
58
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
59
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
60
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
61
|
+
s.add_dependency(%q<rspec>, ["< 2.0.0"])
|
49
62
|
end
|
50
63
|
end
|
64
|
+
|
data/spec/rack_jsonp_spec.rb
CHANGED
@@ -7,8 +7,8 @@ describe Rack::JSONP do
|
|
7
7
|
it "should wrap the response body in the Javascript callback [default callback param]" do
|
8
8
|
test_body = '{"bar":"foo"}'
|
9
9
|
callback = 'foo'
|
10
|
-
app = lambda { |env| [200, {'Content-Type' => '
|
11
|
-
request = Rack::MockRequest.env_for("/", :
|
10
|
+
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] }
|
11
|
+
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
|
12
12
|
body = Rack::JSONP.new(app).call(request).last
|
13
13
|
body.should == ["#{callback}(#{test_body})"]
|
14
14
|
end
|
@@ -16,8 +16,8 @@ describe Rack::JSONP do
|
|
16
16
|
it "should wrap the response body in the Javascript callback [custom callback param]" do
|
17
17
|
test_body = '{"bar":"foo"}'
|
18
18
|
callback = 'foo'
|
19
|
-
app = lambda { |env| [200, {'Content-Type' => '
|
20
|
-
request = Rack::MockRequest.env_for("/", :
|
19
|
+
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] }
|
20
|
+
request = Rack::MockRequest.env_for("/", :params => "foo=bar&whatever=#{callback}")
|
21
21
|
body = Rack::JSONP.new(app, :callback_param => 'whatever').call(request).last
|
22
22
|
body.should == ["#{callback}(#{test_body})"]
|
23
23
|
end
|
@@ -25,25 +25,43 @@ describe Rack::JSONP do
|
|
25
25
|
it "should modify the content length to the correct value" do
|
26
26
|
test_body = '{"bar":"foo"}'
|
27
27
|
callback = 'foo'
|
28
|
-
app = lambda { |env| [200, {'Content-Type' => '
|
29
|
-
request = Rack::MockRequest.env_for("/", :
|
28
|
+
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] }
|
29
|
+
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
|
30
30
|
headers = Rack::JSONP.new(app).call(request)[1]
|
31
31
|
headers['Content-Length'].should == ((test_body.length + callback.length + 2).to_s) # 2 parentheses
|
32
32
|
end
|
33
|
+
|
34
|
+
it "should change the response Content-Type to application/javascript" do
|
35
|
+
test_body = '{"bar":"foo"}'
|
36
|
+
callback = 'foo'
|
37
|
+
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] }
|
38
|
+
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
|
39
|
+
headers = Rack::JSONP.new(app).call(request)[1]
|
40
|
+
headers['Content-Type'].should == "application/javascript"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not wrap content unless response is json" do
|
44
|
+
test_body = '<html><body>Hello, World!</body></html>'
|
45
|
+
callback = 'foo'
|
46
|
+
app = lambda { |env| [200, {'Content-Type' => 'text/html'}, [test_body]] }
|
47
|
+
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
|
48
|
+
body = Rack::JSONP.new(app).call(request).last
|
49
|
+
body.should == [test_body]
|
50
|
+
end
|
33
51
|
end
|
34
52
|
|
35
53
|
describe "when json content is returned" do
|
36
54
|
it "should do nothing if no carriage return has been requested" do
|
37
55
|
test_body = '{"bar":"foo"}'
|
38
56
|
app = lambda { |env| [200, {'Content-Type' => 'application/vnd.com.example.Object+json'}, [test_body]] }
|
39
|
-
request = Rack::MockRequest.env_for("/", :
|
57
|
+
request = Rack::MockRequest.env_for("/", :params => "foo=bar")
|
40
58
|
body = Rack::JSONP.new(app).call(request).last
|
41
59
|
body.should == ['{"bar":"foo"}']
|
42
60
|
end
|
43
61
|
it "should add a carriage return if requested" do
|
44
62
|
test_body = '{"bar":"foo"}'
|
45
63
|
app = lambda { |env| [200, {'Content-Type' => 'application/vnd.com.example.Object+json'}, [test_body]] }
|
46
|
-
request = Rack::MockRequest.env_for("/", :
|
64
|
+
request = Rack::MockRequest.env_for("/", :params => "foo=bar")
|
47
65
|
body = Rack::JSONP.new(app, :carriage_return => true).call(request).last
|
48
66
|
body.should == ["{\"bar\":\"foo\"}\n"]
|
49
67
|
end
|
@@ -51,15 +69,15 @@ describe Rack::JSONP do
|
|
51
69
|
test_body = '{"bar":"foo"}'
|
52
70
|
callback = 'foo'
|
53
71
|
app = lambda { |env| [200, {'Content-Type' => 'application/vnd.com.example.Object+json'}, [test_body]] }
|
54
|
-
request = Rack::MockRequest.env_for("/", :
|
72
|
+
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
|
55
73
|
body = Rack::JSONP.new(app, :carriage_return => true).call(request).last
|
56
74
|
body.should == ["#{callback}(#{test_body})"]
|
57
75
|
end
|
58
76
|
end
|
59
77
|
|
60
78
|
it "should not change anything if no callback param is provided" do
|
61
|
-
app = lambda { |env| [200, {'Content-Type' => '
|
62
|
-
request = Rack::MockRequest.env_for("/", :
|
79
|
+
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, ['{"bar":"foo"}']] }
|
80
|
+
request = Rack::MockRequest.env_for("/", :params => "foo=bar")
|
63
81
|
body = Rack::JSONP.new(app).call(request).last
|
64
82
|
body.join.should == '{"bar":"foo"}'
|
65
83
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,5 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rack'
|
3
4
|
require 'rack/jsonp'
|
4
5
|
require 'spec'
|
5
|
-
require 'spec/autorun'
|
6
|
-
require 'rack'
|
7
|
-
|
8
|
-
|
9
|
-
Spec::Runner.configure do |config|
|
10
|
-
|
11
|
-
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-jsonp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 1.1.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Cyril Rohr
|
@@ -9,10 +14,63 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-09-07 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rack
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
prerelease: false
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 0
|
42
|
+
version: "0"
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *id003
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rspec
|
61
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - <
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 2
|
68
|
+
- 0
|
69
|
+
- 0
|
70
|
+
version: 2.0.0
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: *id004
|
16
74
|
description: A Rack middleware for providing JSON-P support.
|
17
75
|
email: cyril.rohr@gmail.com
|
18
76
|
executables: []
|
@@ -25,6 +83,7 @@ extra_rdoc_files:
|
|
25
83
|
files:
|
26
84
|
- .document
|
27
85
|
- .gitignore
|
86
|
+
- Gemfile
|
28
87
|
- LICENSE
|
29
88
|
- README.rdoc
|
30
89
|
- Rakefile
|
@@ -43,21 +102,26 @@ rdoc_options:
|
|
43
102
|
require_paths:
|
44
103
|
- lib
|
45
104
|
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
46
106
|
requirements:
|
47
107
|
- - ">="
|
48
108
|
- !ruby/object:Gem::Version
|
109
|
+
hash: -3568420703856981526
|
110
|
+
segments:
|
111
|
+
- 0
|
49
112
|
version: "0"
|
50
|
-
version:
|
51
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
52
115
|
requirements:
|
53
116
|
- - ">="
|
54
117
|
- !ruby/object:Gem::Version
|
118
|
+
segments:
|
119
|
+
- 0
|
55
120
|
version: "0"
|
56
|
-
version:
|
57
121
|
requirements: []
|
58
122
|
|
59
123
|
rubyforge_project:
|
60
|
-
rubygems_version: 1.3.
|
124
|
+
rubygems_version: 1.3.7
|
61
125
|
signing_key:
|
62
126
|
specification_version: 3
|
63
127
|
summary: A Rack middleware for providing JSON-P support.
|