cryx-g5k 0.2.0 → 0.2.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/VERSION.yml +1 -1
- data/lib/g5k/rack/accept_format.rb +40 -42
- data/lib/g5k/rack/jsonp.rb +34 -36
- metadata +1 -1
data/VERSION.yml
CHANGED
@@ -1,46 +1,44 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
DEFAULT_EXTENSION = ".html"
|
1
|
+
module Rack
|
2
|
+
#
|
3
|
+
# A Rack middleware for automatically adding a <tt>format</tt> token at the end of the request path
|
4
|
+
# when there is none. It can detect formats passed in the HTTP_ACCEPT header to populate this token.
|
5
|
+
#
|
6
|
+
# e.g.:
|
7
|
+
# GET /some/resource HTTP/1.1
|
8
|
+
# Accept: application/json
|
9
|
+
# ->
|
10
|
+
# GET /some/resource.json HTTP/1.1
|
11
|
+
# Accept: application/json
|
12
|
+
#
|
13
|
+
# You can add custom types with this kind of function (taken from sinatra):
|
14
|
+
# def mime(ext, type)
|
15
|
+
# ext = ".#{ext}" unless ext.to_s[0] == ?.
|
16
|
+
# Rack::Mime::MIME_TYPES[ext.to_s] = type
|
17
|
+
# end
|
18
|
+
# and then:
|
19
|
+
# mime :json, 'application/json'
|
20
|
+
#
|
21
|
+
# Note: it does not take into account multiple media types in the Accept header.
|
22
|
+
# The first media type takes precedence over all the others.
|
23
|
+
#
|
24
|
+
# MIT-License - Cyril Rohr
|
25
|
+
#
|
26
|
+
class AcceptFormat
|
27
|
+
# Constants
|
28
|
+
DEFAULT_EXTENSION = ".html"
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
def call(env)
|
36
|
-
req = Rack::Request.new(env)
|
37
|
-
unless req.path_info =~ /(.*)\.(.+)/
|
38
|
-
accept = env['HTTP_ACCEPT'].scan(/[^;,\s]*\/[^;,\s]*/)[0] rescue ""
|
39
|
-
extension = Rack::Mime::MIME_TYPES.invert[accept] || DEFAULT_EXTENSION
|
40
|
-
req.path_info = req.path_info+"#{extension}"
|
41
|
-
end
|
42
|
-
@app.call(env)
|
43
|
-
end
|
30
|
+
def initialize(app)
|
31
|
+
@app = app
|
44
32
|
end
|
33
|
+
|
34
|
+
def call(env)
|
35
|
+
req = Rack::Request.new(env)
|
36
|
+
unless req.path_info =~ /(.*)\.(.+)/
|
37
|
+
accept = env['HTTP_ACCEPT'].scan(/[^;,\s]*\/[^;,\s]*/)[0] rescue ""
|
38
|
+
extension = Rack::Mime::MIME_TYPES.invert[accept] || DEFAULT_EXTENSION
|
39
|
+
req.path_info = req.path_info+"#{extension}"
|
40
|
+
end
|
41
|
+
@app.call(env)
|
42
|
+
end
|
45
43
|
end
|
46
44
|
end
|
data/lib/g5k/rack/jsonp.rb
CHANGED
@@ -1,43 +1,41 @@
|
|
1
|
-
module
|
2
|
-
|
1
|
+
module Rack
|
2
|
+
|
3
|
+
# A Rack middleware for providing JSON-P support.
|
4
|
+
#
|
5
|
+
# Full credit to Flinn Mueller (http://actsasflinn.com/) for this contribution.
|
6
|
+
#
|
7
|
+
class JSONP
|
3
8
|
|
4
|
-
|
9
|
+
def initialize(app)
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
# Proxies the request to the application, stripping out the JSON-P callback
|
14
|
+
# method and padding the response with the appropriate callback format.
|
5
15
|
#
|
6
|
-
#
|
16
|
+
# Changes nothing if no <tt>callback</tt> param is specified.
|
7
17
|
#
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
status, headers, response = @app.call(env)
|
21
|
-
request = Rack::Request.new(env)
|
22
|
-
if request.params.include?('callback')
|
23
|
-
response = pad(request.params.delete('callback'), response)
|
24
|
-
headers['Content-Length'] = response.length.to_s
|
25
|
-
end
|
26
|
-
[status, headers, response]
|
18
|
+
def call(env)
|
19
|
+
status, headers, response = @app.call(env)
|
20
|
+
request = Rack::Request.new(env)
|
21
|
+
if request.params.include?('callback')
|
22
|
+
response = pad(request.params.delete('callback'), response)
|
23
|
+
headers['Content-Length'] = response.length.to_s
|
27
24
|
end
|
28
|
-
|
29
|
-
# Pads the response with the appropriate callback format according to the
|
30
|
-
# JSON-P spec/requirements.
|
31
|
-
#
|
32
|
-
# The Rack response spec indicates that it should be enumerable. The method
|
33
|
-
# of combining all of the data into a single string makes sense since JSON
|
34
|
-
# is returned as a full string.
|
35
|
-
#
|
36
|
-
def pad(callback, response, body = "")
|
37
|
-
response.each{ |s| body << s }
|
38
|
-
"#{callback}(#{body})"
|
39
|
-
end
|
40
|
-
|
25
|
+
[status, headers, response]
|
41
26
|
end
|
27
|
+
|
28
|
+
# Pads the response with the appropriate callback format according to the
|
29
|
+
# JSON-P spec/requirements.
|
30
|
+
#
|
31
|
+
# The Rack response spec indicates that it should be enumerable. The method
|
32
|
+
# of combining all of the data into a single string makes sense since JSON
|
33
|
+
# is returned as a full string.
|
34
|
+
#
|
35
|
+
def pad(callback, response, body = "")
|
36
|
+
response.each{ |s| body << s }
|
37
|
+
"#{callback}(#{body})"
|
38
|
+
end
|
39
|
+
|
42
40
|
end
|
43
41
|
end
|