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 CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 2
3
- :patch: 0
3
+ :patch: 1
4
4
  :major: 0
@@ -1,46 +1,44 @@
1
- module G5K
2
- module Rack
3
- #
4
- # A Rack middleware for automatically adding a <tt>format</tt> token at the end of the request path
5
- # when there is none. It can detect formats passed in the HTTP_ACCEPT header to populate this token.
6
- #
7
- # e.g.:
8
- # GET /some/resource HTTP/1.1
9
- # Accept: application/json
10
- # ->
11
- # GET /some/resource.json HTTP/1.1
12
- # Accept: application/json
13
- #
14
- # You can add custom types with this kind of function (taken from sinatra):
15
- # def mime(ext, type)
16
- # ext = ".#{ext}" unless ext.to_s[0] == ?.
17
- # Rack::Mime::MIME_TYPES[ext.to_s] = type
18
- # end
19
- # and then:
20
- # mime :json, 'application/json'
21
- #
22
- # Note: it does not take into account multiple media types in the Accept header.
23
- # The first media type takes precedence over all the others.
24
- #
25
- # MIT-License - Cyril Rohr
26
- #
27
- class AcceptFormat
28
- # Constants
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
- def initialize(app)
32
- @app = app
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
@@ -1,43 +1,41 @@
1
- module G5K
2
- module Rack
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
- # A Rack middleware for providing JSON-P support.
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
- # Full credit to Flinn Mueller (http://actsasflinn.com/) for this contribution.
16
+ # Changes nothing if no <tt>callback</tt> param is specified.
7
17
  #
8
- class JSONP
9
-
10
- def initialize(app)
11
- @app = app
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
- 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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cryx-g5k
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Rohr