restful_jsonp 1.0.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 +2 -0
- data/LICENSE +19 -0
- data/README.markdown +38 -0
- data/lib/restful_jsonp.rb +13 -0
- data/lib/restful_jsonp/jsonp_responder.rb +33 -0
- data/lib/restful_jsonp/method_override.rb +18 -0
- data/restful_jsonp.gemspec +15 -0
- metadata +101 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2011 by University of Toronto
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
RESTful JSONP
|
2
|
+
============
|
3
|
+
|
4
|
+
This is a Rails plugin (Railtie) that allows your existing RESTful controllers to work over JSONP.
|
5
|
+
|
6
|
+
REST is designed around the four HTTP request methods (`GET`, `POST`, `PUT`, and `DELETE`), but
|
7
|
+
JSONP requests are always `GET`s. To get around this restriction, this Railtie lets you specify the
|
8
|
+
desired method in a special `_method` parameter.
|
9
|
+
|
10
|
+
For example, to make a `PUT` request to `/users/1.json`, you would make a JSONP (`GET`) request to
|
11
|
+
`/users/1.json?_method=PUT`.
|
12
|
+
|
13
|
+
|
14
|
+
Installation
|
15
|
+
------------
|
16
|
+
|
17
|
+
First, install the gem:
|
18
|
+
|
19
|
+
`gem install restful_jsonp`
|
20
|
+
|
21
|
+
Then add this to your Rails app's `Gemfile`:
|
22
|
+
|
23
|
+
`gem 'restful_jsonp', :require => 'restful_jsonp/railtie'`
|
24
|
+
|
25
|
+
Note that this only works for Rails 3. In principle you could try to swap in the RestfulJSONP::MethodOverride
|
26
|
+
middleware into a Rails 2.3+ app, but this has not been tested.
|
27
|
+
|
28
|
+
|
29
|
+
How it Works
|
30
|
+
------------
|
31
|
+
|
32
|
+
The `_method` functionality is built in to Rails (via Rack), but is normally only available for
|
33
|
+
`POST` requests. This Railtie replaces the default `Rack::MethodOverride` middleware with a slightly
|
34
|
+
altered version that checks for the `_method` parameter regardless of whether it's in a `POST`
|
35
|
+
or `GET` request.
|
36
|
+
|
37
|
+
Note that this functionality is enabled for all requests, regardless of whether they are done
|
38
|
+
via JSONP or otherwise.
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'action_controller/base'
|
3
|
+
require 'restful_jsonp/method_override'
|
4
|
+
require 'restful_jsonp/jsonp_responder'
|
5
|
+
|
6
|
+
module RestfulJSONP
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
initializer "restful_jsonp.replace_rack_methodoverride" do |app|
|
9
|
+
Rails.logger.debug "Swapping in RestfulJSONP Middleware"
|
10
|
+
app.middleware.swap Rack::MethodOverride, RestfulJSONP::MethodOverride
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'action_controller/metal/responder'
|
2
|
+
|
3
|
+
module RestfulJSONP
|
4
|
+
class JSONPResponder < ActionController::Responder
|
5
|
+
|
6
|
+
def display(resource, given_options = {})
|
7
|
+
if format == :json && controller.params[:callback] && !controller.request.xhr?
|
8
|
+
# this is a JSONP request (note that JSONP is not done over XHR!)
|
9
|
+
|
10
|
+
jsonp_options = {}
|
11
|
+
|
12
|
+
if (resource.respond_to?(:errors) && !resource.errors.empty?) ||
|
13
|
+
(given_options[:status] && ![:ok, :created].include?(given_options[:status]))
|
14
|
+
jsonp_options[:status] = :accepted # we can't return an error HTTP response (e.g. 422 or 500) because it would be ignored :(
|
15
|
+
resource = {
|
16
|
+
:error => {
|
17
|
+
:status => given_options[:status],
|
18
|
+
:data => resource.to_json
|
19
|
+
}
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
jsonp_options[format] = resource
|
24
|
+
jsonp_options[:callback] = controller.params[:callback]
|
25
|
+
|
26
|
+
render options.merge!(given_options).merge!(jsonp_options)
|
27
|
+
else
|
28
|
+
# this is not a JSONP request; carry on
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
module RestfulJSONP
|
4
|
+
class MethodOverride < Rack::MethodOverride
|
5
|
+
def call(env)
|
6
|
+
req = Rack::Request.new(env)
|
7
|
+
method = req.params[METHOD_OVERRIDE_PARAM_KEY] ||
|
8
|
+
env[HTTP_METHOD_OVERRIDE_HEADER]
|
9
|
+
method = method.to_s.upcase
|
10
|
+
if HTTP_METHODS.include?(method)
|
11
|
+
env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
|
12
|
+
env["REQUEST_METHOD"] = method
|
13
|
+
end
|
14
|
+
|
15
|
+
@app.call(env)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = "restful_jsonp"
|
3
|
+
gem.version = "1.0.0"
|
4
|
+
gem.authors = ["Matt Zukowski"]
|
5
|
+
gem.email = ["matt.zukowski@utoronto.ca"]
|
6
|
+
gem.homepage = "http://github.com/educoder/restful_jsonp"
|
7
|
+
gem.summary = "Makes your RESTful Rails service accessible using JSONP. Use the '_method' parameter in your requests to specify the request method."
|
8
|
+
gem.description = "Normally Rails/Rack only checks the '_method' parameter in POST requests, but JSONP requests are always GETs. This railtie enables the '_method' check for all request types, including GET."
|
9
|
+
gem.require_paths = ["lib"]
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split("\n")
|
12
|
+
|
13
|
+
gem.add_dependency "rack", "~> 1"
|
14
|
+
gem.add_dependency "rails", "~> 3"
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: restful_jsonp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matt Zukowski
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-21 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rack
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 1
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
version: "1"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rails
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 5
|
44
|
+
segments:
|
45
|
+
- 3
|
46
|
+
version: "3"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Normally Rails/Rack only checks the '_method' parameter in POST requests, but JSONP requests are always GETs. This railtie enables the '_method' check for all request types, including GET.
|
50
|
+
email:
|
51
|
+
- matt.zukowski@utoronto.ca
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- LICENSE
|
61
|
+
- README.markdown
|
62
|
+
- lib/restful_jsonp.rb
|
63
|
+
- lib/restful_jsonp/jsonp_responder.rb
|
64
|
+
- lib/restful_jsonp/method_override.rb
|
65
|
+
- restful_jsonp.gemspec
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: http://github.com/educoder/restful_jsonp
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.4.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: Makes your RESTful Rails service accessible using JSONP. Use the '_method' parameter in your requests to specify the request method.
|
100
|
+
test_files: []
|
101
|
+
|