rack-jsonp-middleware 0.0.1
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/README.md +44 -0
- data/lib/rack/jsonp.rb +30 -0
- metadata +81 -0
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# rack-jsonp-middleware
|
2
|
+
|
3
|
+
A Rack JSONP middleware
|
4
|
+
|
5
|
+
## Overview
|
6
|
+
|
7
|
+
This is a customized implementation of a JSONP middleware.
|
8
|
+
|
9
|
+
The main difference with the rest of them is that this one will add JSONP support to any of your JSON calls but only when the extension name '.jsonp' is present.
|
10
|
+
|
11
|
+
Since 'callback' is a really generic parameter name if someone wants to get a JSONP response they must request it explicitly.
|
12
|
+
|
13
|
+
## Examples
|
14
|
+
|
15
|
+
Given that ...
|
16
|
+
http://domain.com/action.json
|
17
|
+
|
18
|
+
Returns something like ...
|
19
|
+
{"key":"value"}
|
20
|
+
|
21
|
+
With Content-Type ...
|
22
|
+
application/json
|
23
|
+
|
24
|
+
Then ...
|
25
|
+
http://domain.com/action.jsonp?callback=success
|
26
|
+
|
27
|
+
Will return ...
|
28
|
+
success({"key":"value"})
|
29
|
+
|
30
|
+
With Content-Type ...
|
31
|
+
application/javascript
|
32
|
+
|
33
|
+
But ...
|
34
|
+
http://domain.com/action.json?callback=sucess
|
35
|
+
|
36
|
+
Will still returns ...
|
37
|
+
{"key":"value"}
|
38
|
+
|
39
|
+
With Content-Type ...
|
40
|
+
application/json
|
41
|
+
|
42
|
+
## Notes
|
43
|
+
|
44
|
+
[To be improved] Calling "something.jsonp" without a "callback" will return an empty 400 response
|
data/lib/rack/jsonp.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Rack
|
2
|
+
class JSONP
|
3
|
+
include Rack::Utils
|
4
|
+
|
5
|
+
def initialize(app)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
request = Rack::Request.new(env)
|
12
|
+
requesting_jsonp = Pathname(request.env['REQUEST_PATH']).extname =~ /^\.jsonp$/i
|
13
|
+
callback = request.params['callback']
|
14
|
+
|
15
|
+
return [400,{},[]] if requesting_jsonp && !callback
|
16
|
+
|
17
|
+
env['REQUEST_URI'].sub!(/\.jsonp/i, '.json') if requesting_jsonp
|
18
|
+
|
19
|
+
status, headers, response = @app.call(env)
|
20
|
+
|
21
|
+
if requesting_jsonp
|
22
|
+
response.body = "#{callback}(#{response.body});"
|
23
|
+
headers['Content-Length'] = response.body.length.to_s
|
24
|
+
headers['Content-Type'] = 'application/javascript'
|
25
|
+
end
|
26
|
+
|
27
|
+
[status, headers, response]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-jsonp-middleware
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Roberto Decurnex
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-28 00:00:00 -03: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: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: A Rack JSONP middleware
|
36
|
+
email: nex.development@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- README.md
|
45
|
+
- lib/rack/jsonp.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://robertodecurnex.github.com/rack-jsonp-middleware
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.3.7
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: rack-jsonp-middleware-0.0.1
|
80
|
+
test_files: []
|
81
|
+
|