rack-jsonp-utils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2d788ba01785cdad556e08582359d38e8830fad
4
+ data.tar.gz: 3652f66191ea6a6f14affec08bb2b2139eec785b
5
+ SHA512:
6
+ metadata.gz: a16a26e176502397afba3c3c5027c0b5e21d729b379d6810d81d042004f3ef311f65da8499fc2ad43d95906565d07b123e2d87a320a335e28e150e2205a39216
7
+ data.tar.gz: 2900007addca9ffa78325b7a179f51136a41bb2df6407f8b672ae81e7581cba4d78df4794384f21c13b87e52b45ba528ce1faa3b874cf9740f18c52386ae64f7
@@ -0,0 +1,15 @@
1
+ *.gem
2
+ .bundle
3
+ .config
4
+ .yardoc
5
+ Gemfile.lock
6
+ _yardoc
7
+ coverage
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+ .idea
@@ -0,0 +1,4 @@
1
+ v0.0.1
2
+ ======
3
+
4
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-jsonp-utils.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Luis Mayoral
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ [![Gem Version](https://badge.fury.io/rb/rack-jsonp-utils.svg)](http://badge.fury.io/rb/rack-jsonp-utils)
2
+ [![Code Climate](https://codeclimate.com/github/mayoral/rack-jsonp-utils/badges/gpa.svg)](https://codeclimate.com/github/mayoral/rack-jsonp-utils)
3
+ [![Inline docs](http://inch-ci.org/github/mayoral/rack-jsonp-utils.svg?branch=master)](http://inch-ci.org/github/mayoral/rack-jsonp-utils)
4
+ [![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/mayoral/rack-jsonp-utils?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5
+
6
+ # Rack::JSONP::Utils
7
+
8
+ Some Rack middlewares to make easier for your app to support JSONP.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'rack-jsonp-utils'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ Simply add these lines to your `config/application.rb` file:
25
+
26
+ ```ruby
27
+ config.middleware.use Rack::JSONP::Callback
28
+ config.middleware.use Rack::JSONP::MethodOverride
29
+ ```
30
+
31
+ ### Rack::JSONP::Callback
32
+
33
+ This middleware will transform your JSON output into a valid JSONP output, using the callback name specified in the URL.
34
+
35
+ By default, this middleware will use `callback` as the callback parameter in the query string. You can customize the name for the callback parameter:
36
+
37
+ ```ruby
38
+ config.middleware.use Rack::JSONP::Callback, 'my_callback_param'
39
+ ```
40
+
41
+ ### Rack::JSONP::MethodOverride
42
+
43
+ This middleware will help you to transform internally the request to the HTTP method you want, since all the requests in JSONP must be GET requests. It will also force the HTTP response code of your request to be 200.
44
+
45
+ As an example, `http://example.com/customers&callback=myfunc&_method=POST` will be seen by your app as a `POST /customers` request.
46
+
47
+ By default, this middleware will use `callback` as the callback parameter in the query string, and `_method`. It will not activate if these two params are not in the query string.
48
+
49
+ You can customize the name of these params:
50
+
51
+ ```ruby
52
+ # To only change the method override param name
53
+ config.middleware.use Rack::JSONP::MethodOverride, 'my_method_override_param'
54
+
55
+ # To also change the callback param name
56
+ config.middleware.use Rack::JSONP::MethodOverride, 'my_method_override_param', 'my_callback_param'
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it ( https://github.com/mayoral/rack-jsonp-utils/fork )
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,49 @@
1
+ module Rack
2
+ module JSONP
3
+ class Callback
4
+ def initialize(app, callback_param = 'callback')
5
+ @app = app
6
+ @callback_param = callback_param
7
+ end
8
+
9
+ def call(env)
10
+ request = Rack::Request.new(env)
11
+
12
+ callback = request.params[@callback_param]
13
+
14
+ if callback
15
+ env['jsonp.utils.callback'] = callback
16
+
17
+ status, headers, response = @app.call(env)
18
+
19
+ if headers['Content-Type'].to_s.start_with?('application/json')
20
+ response = pad(callback, response)
21
+
22
+ headers['Content-Length'] = response.first.bytesize.to_s
23
+ headers['Content-Type'] = 'application/javascript'
24
+
25
+ [200, headers, response]
26
+ else
27
+ [status, headers, response]
28
+ end
29
+ else
30
+ @app.call(env)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def pad(callback, response)
37
+ body = ''
38
+
39
+ response.each do |s|
40
+ body << s.to_s.gsub("\u2028", '\u2028').gsub("\u2029", '\u2029')
41
+ end
42
+
43
+ response.close if response.respond_to?(:close)
44
+
45
+ ["/**/#{callback}(#{body})"]
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,31 @@
1
+ module Rack
2
+ module JSONP
3
+ class MethodOverride
4
+ HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS PATCH)
5
+
6
+ def initialize(app, method_override_param = '_method', callback_param = 'callback')
7
+ @app = app
8
+ @method_override_param = method_override_param
9
+ @callback_param = callback_param
10
+ end
11
+
12
+ def call(env)
13
+ request = Rack::Request.new(env)
14
+ method_override = request.params[@method_override_param]
15
+
16
+ if jsonp_callback_enabled?(request) && method_override && HTTP_METHODS.include?(method_override)
17
+ env['jsonp.utils.method_override'] = method_override
18
+ env['REQUEST_METHOD'] = method_override
19
+ end
20
+
21
+ @app.call(env)
22
+ end
23
+
24
+ private
25
+
26
+ def jsonp_callback_enabled?(request)
27
+ @callback_param && request.params[@callback_param]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ require 'rack/jsonp/utils/version'
2
+
3
+ require 'rack/jsonp/callback'
4
+ require 'rack/jsonp/method_override'
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ module JSONP
3
+ module Utils
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/jsonp/utils/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rack-jsonp-utils'
8
+ spec.version = Rack::JSONP::Utils::VERSION
9
+ spec.authors = ['Luis Mayoral']
10
+ spec.email = ['luis@luismayoral.com']
11
+ spec.summary = 'Some Rack middlewares to make easier for your app to support JSONP'
12
+ spec.description = 'Some Rack middlewares to make easier for your app to support JSONP'
13
+ spec.homepage = 'http://github.com/mayoral/rack-jsonp-utils'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'rack', '>= 0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-jsonp-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Luis Mayoral
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Some Rack middlewares to make easier for your app to support JSONP
56
+ email:
57
+ - luis@luismayoral.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CHANGELOG.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/rack/jsonp/callback.rb
69
+ - lib/rack/jsonp/method_override.rb
70
+ - lib/rack/jsonp/utils.rb
71
+ - lib/rack/jsonp/utils/version.rb
72
+ - rack-jsonp-utils.gemspec
73
+ homepage: http://github.com/mayoral/rack-jsonp-utils
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Some Rack middlewares to make easier for your app to support JSONP
97
+ test_files: []