rack-jsonp 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Cyril Rohr
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,9 @@
1
+ = rack-jsonp
2
+
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
+ 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
+ == Copyright
8
+
9
+ Copyright (c) 2009 Cyril Rohr. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-jsonp"
8
+ gem.summary = %Q{A Rack middleware for providing JSON-P support.}
9
+ gem.description = %Q{A Rack middleware for providing JSON-P support.}
10
+ gem.email = "cyril.rohr@gmail.com"
11
+ gem.homepage = "http://github.com/crohr/rack-jsonp"
12
+ gem.authors = ["Cyril Rohr"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ if File.exist?('VERSION')
40
+ version = File.read('VERSION')
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "rack-jsonp #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/rack/jsonp.rb ADDED
@@ -0,0 +1,56 @@
1
+ module Rack
2
+ # A Rack middleware for providing JSON-P support.
3
+ #
4
+ # Adapted from Flinn Mueller (http://actsasflinn.com/).
5
+ #
6
+ class JSONP
7
+
8
+ def initialize(app, options = {})
9
+ @app = app
10
+ @carriage_return = options[:carriage_return] || false
11
+ @callback_param = options[:callback_param] || 'callback'
12
+ end
13
+
14
+ # Proxies the request to the application, stripping out the JSON-P callback
15
+ # method and padding the response with the appropriate callback format.
16
+ #
17
+ # Changes nothing if no <tt>callback</tt> param is specified.
18
+ #
19
+ def call(env)
20
+ # remove the callback and _ parameters BEFORE calling the backend,
21
+ # so that caching middleware does not store a copy for each value of the callback parameter
22
+ request = Rack::Request.new(env)
23
+ callback = request.params.delete(@callback_param)
24
+ env['QUERY_STRING'] = env['QUERY_STRING'].split("&").delete_if{|param| param =~ /^(_|#{@callback_param})/}.join("&")
25
+
26
+ status, headers, response = @app.call(env)
27
+ if callback
28
+ response = pad(callback, response)
29
+ headers['Content-Length'] = response.first.length.to_s
30
+ elsif @carriage_return && headers['Content-Type'] =~ /json/i
31
+ # add a \n after the response if this is a json (not JSONP) response
32
+ response = carriage_return(response)
33
+ headers['Content-Length'] = response.first.length.to_s
34
+ end
35
+ [status, headers, response]
36
+ end
37
+
38
+ # Pads the response with the appropriate callback format according to the
39
+ # JSON-P spec/requirements.
40
+ #
41
+ # The Rack response spec indicates that it should be enumerable. The method
42
+ # of combining all of the data into a single string makes sense since JSON
43
+ # is returned as a full string.
44
+ #
45
+ def pad(callback, response, body = "")
46
+ response.each{ |s| body << s.to_s }
47
+ ["#{callback}(#{body})"]
48
+ end
49
+
50
+ def carriage_return(response, body = "")
51
+ response.each{ |s| body << s.to_s }
52
+ ["#{body}\n"]
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rack-jsonp}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Cyril Rohr"]
12
+ s.date = %q{2009-10-21}
13
+ s.description = %q{A Rack middleware for providing JSON-P support.}
14
+ s.email = %q{cyril.rohr@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rack/jsonp.rb",
27
+ "rack-jsonp.gemspec",
28
+ "spec/rack_jsonp_spec.rb",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/crohr/rack-jsonp}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{A Rack middleware for providing JSON-P support.}
36
+ s.test_files = [
37
+ "spec/rack_jsonp_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+
4
+ describe Rack::JSONP do
5
+
6
+ describe "when a callback parameter is provided" do
7
+ it "should wrap the response body in the Javascript callback [default callback param]" do
8
+ test_body = '{"bar":"foo"}'
9
+ callback = 'foo'
10
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [test_body]] }
11
+ request = Rack::MockRequest.env_for("/", :input => "foo=bar&callback=#{callback}")
12
+ body = Rack::JSONP.new(app).call(request).last
13
+ body.should == ["#{callback}(#{test_body})"]
14
+ end
15
+
16
+ it "should wrap the response body in the Javascript callback [custom callback param]" do
17
+ test_body = '{"bar":"foo"}'
18
+ callback = 'foo'
19
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [test_body]] }
20
+ request = Rack::MockRequest.env_for("/", :input => "foo=bar&whatever=#{callback}")
21
+ body = Rack::JSONP.new(app, :callback_param => 'whatever').call(request).last
22
+ body.should == ["#{callback}(#{test_body})"]
23
+ end
24
+
25
+ it "should modify the content length to the correct value" do
26
+ test_body = '{"bar":"foo"}'
27
+ callback = 'foo'
28
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [test_body]] }
29
+ request = Rack::MockRequest.env_for("/", :input => "foo=bar&callback=#{callback}")
30
+ headers = Rack::JSONP.new(app).call(request)[1]
31
+ headers['Content-Length'].should == ((test_body.length + callback.length + 2).to_s) # 2 parentheses
32
+ end
33
+ end
34
+
35
+ describe "when json content is returned" do
36
+ it "should do nothing if no carriage return has been requested" do
37
+ test_body = '{"bar":"foo"}'
38
+ app = lambda { |env| [200, {'Content-Type' => 'application/vnd.com.example.Object+json'}, [test_body]] }
39
+ request = Rack::MockRequest.env_for("/", :input => "foo=bar")
40
+ body = Rack::JSONP.new(app).call(request).last
41
+ body.should == ['{"bar":"foo"}']
42
+ end
43
+ it "should add a carriage return if requested" do
44
+ test_body = '{"bar":"foo"}'
45
+ app = lambda { |env| [200, {'Content-Type' => 'application/vnd.com.example.Object+json'}, [test_body]] }
46
+ request = Rack::MockRequest.env_for("/", :input => "foo=bar")
47
+ body = Rack::JSONP.new(app, :carriage_return => true).call(request).last
48
+ body.should == ["{\"bar\":\"foo\"}\n"]
49
+ end
50
+ it "should not add a carriage return for jsonp content" do
51
+ test_body = '{"bar":"foo"}'
52
+ callback = 'foo'
53
+ app = lambda { |env| [200, {'Content-Type' => 'application/vnd.com.example.Object+json'}, [test_body]] }
54
+ request = Rack::MockRequest.env_for("/", :input => "foo=bar&callback=#{callback}")
55
+ body = Rack::JSONP.new(app, :carriage_return => true).call(request).last
56
+ body.should == ["#{callback}(#{test_body})"]
57
+ end
58
+ end
59
+
60
+ it "should not change anything if no callback param is provided" do
61
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['{"bar":"foo"}']] }
62
+ request = Rack::MockRequest.env_for("/", :input => "foo=bar")
63
+ body = Rack::JSONP.new(app).call(request).last
64
+ body.join.should == '{"bar":"foo"}'
65
+ end
66
+ end
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rack/jsonp'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'rack'
7
+
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-jsonp
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cyril Rohr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-21 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Rack middleware for providing JSON-P support.
17
+ email: cyril.rohr@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/rack/jsonp.rb
33
+ - rack-jsonp.gemspec
34
+ - spec/rack_jsonp_spec.rb
35
+ - spec/spec_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/crohr/rack-jsonp
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: A Rack middleware for providing JSON-P support.
64
+ test_files:
65
+ - spec/rack_jsonp_spec.rb
66
+ - spec/spec_helper.rb