alexander 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +13 -0
- data/lib/alexander/version.rb +1 -1
- data/lib/alexander/xsl_processor.rb +20 -8
- data/run_guard.rb +1 -0
- data/spec/alexander/xsl_processor_spec.rb +21 -2
- metadata +3 -2
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
A Rack middleware to process XML through XSLT to generate HTML.
|
4
4
|
|
5
|
+
It use the `<?xml-stylesheet ...?>` processing instruction found in XML to find wich XSLT to use. It works only if the XSLT is hosted inside the same application as the XML, as it does another call the the same Rack stack to found it.
|
6
|
+
|
5
7
|
The process occur only:
|
6
8
|
|
7
9
|
1. If the file served is a XML (`mime-type: "application/xml"`);
|
@@ -10,6 +12,8 @@ The process occur only:
|
|
10
12
|
|
11
13
|
If *any* of these conditions is *false*, Alexander will do nothing.
|
12
14
|
|
15
|
+
The XSLT processing can be forced in a XSLT enable browser passing "`force_xslt_parameter=true`" as URL parameter (`QUERY_STRING`). The parameter name is intentionaly long and specific to minimize chances of conflict.
|
16
|
+
|
13
17
|
## Installation
|
14
18
|
|
15
19
|
Add this line to your application's Gemfile:
|
@@ -30,6 +34,10 @@ Add to your Rack stack:
|
|
30
34
|
|
31
35
|
use Alexander::XslProcessor
|
32
36
|
|
37
|
+
If you want to force ALL requests to be processed by the server:
|
38
|
+
|
39
|
+
use Alexander::XslProcessor, force_xslt_parameter: true
|
40
|
+
|
33
41
|
## Browsers with XSLT processing support:
|
34
42
|
|
35
43
|
* Chrome >= 1.0
|
@@ -38,6 +46,11 @@ Add to your Rack stack:
|
|
38
46
|
* Internet Explorer >= 6.0
|
39
47
|
* Opera >= 9.0
|
40
48
|
|
49
|
+
## CHANGELOG
|
50
|
+
|
51
|
+
0.5.0 - Initial published version
|
52
|
+
0.5.1 - "force_xslt_processing" parameter
|
53
|
+
|
41
54
|
## TODO
|
42
55
|
* Better error handling:
|
43
56
|
* Invalid XML.
|
data/lib/alexander/version.rb
CHANGED
@@ -3,8 +3,9 @@ require 'useragent'
|
|
3
3
|
require 'nokogiri'
|
4
4
|
|
5
5
|
module Alexander
|
6
|
-
|
6
|
+
FORCE_PROCESSING_PARAMETER = "force_xslt_processing"
|
7
7
|
|
8
|
+
Browser = Struct.new(:browser, :version)
|
8
9
|
XSLT_ENABLE_BROWSERS = [
|
9
10
|
Browser.new("Chrome", "1.0"),
|
10
11
|
Browser.new("Firefox", "3.0"),
|
@@ -14,21 +15,26 @@ module Alexander
|
|
14
15
|
]
|
15
16
|
|
16
17
|
class XslProcessor
|
17
|
-
|
18
|
+
|
19
|
+
def initialize(app, options = {})
|
18
20
|
@app = app
|
21
|
+
@options = options
|
19
22
|
end
|
20
23
|
|
21
24
|
def call(env)
|
22
|
-
|
25
|
+
response = @app.call(env)
|
26
|
+
status, headers, body = response
|
23
27
|
|
24
|
-
return
|
25
|
-
return [status, headers, body] if xlst_enable_browser?(env)
|
28
|
+
return response unless xml?(headers)
|
26
29
|
|
27
|
-
|
28
|
-
return
|
30
|
+
force = force_processing(env)
|
31
|
+
return response if xlst_enable_browser?(env) && !force
|
32
|
+
|
33
|
+
html_body = to_html(env, body)
|
34
|
+
return response unless html_body
|
29
35
|
|
30
36
|
headers["Content-type"] = "text/html"
|
31
|
-
Rack::Response.new([
|
37
|
+
Rack::Response.new([html_body], status, headers).finish
|
32
38
|
end
|
33
39
|
|
34
40
|
def xml?(headers)
|
@@ -41,6 +47,12 @@ module Alexander
|
|
41
47
|
XSLT_ENABLE_BROWSERS.detect { |browser| user_agent >= browser }
|
42
48
|
end
|
43
49
|
|
50
|
+
def force_processing(env)
|
51
|
+
return true if @options[FORCE_PROCESSING_PARAMETER.to_sym]
|
52
|
+
request = Rack::Request.new(env)
|
53
|
+
request.params[FORCE_PROCESSING_PARAMETER] == "true"
|
54
|
+
end
|
55
|
+
|
44
56
|
def to_html(env, body)
|
45
57
|
xml = body_to_string(body)
|
46
58
|
xslt_request = detect_xslt_processing_instruction(xml)
|
data/run_guard.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
exec "guard"
|
@@ -44,11 +44,11 @@ CURL = "curl/7.21.6 (x86_64-pc-linux-gnu) libcurl/7.21.6 OpenSSL/1.0.0e zlib/1.2
|
|
44
44
|
describe Alexander::XslProcessor do
|
45
45
|
|
46
46
|
def env_with_chrome
|
47
|
-
{"HTTP_USER_AGENT" => CHROME_18}
|
47
|
+
{"HTTP_USER_AGENT" => CHROME_18, "rack.input" => ""}
|
48
48
|
end
|
49
49
|
|
50
50
|
def env_with_curl
|
51
|
-
{"HTTP_USER_AGENT" => CURL}
|
51
|
+
{"HTTP_USER_AGENT" => CURL, "rack.input" => ""}
|
52
52
|
end
|
53
53
|
|
54
54
|
before do
|
@@ -74,6 +74,14 @@ describe Alexander::XslProcessor do
|
|
74
74
|
status, headers, response = @filter.call(env_with_chrome)
|
75
75
|
response.body.must_equal @dummy_app.xml.body
|
76
76
|
end
|
77
|
+
describe "when receive a force process parameter" do
|
78
|
+
it "should parse XML to HTML" do
|
79
|
+
status, headers, response = @filter.call(env_with_chrome.merge({"QUERY_STRING" => "force_xslt_processing=true"}))
|
80
|
+
status.must_equal 200
|
81
|
+
headers["Content-type"].must_equal "text/html"
|
82
|
+
response.body.must_equal ["<html><body></body></html>\n"]
|
83
|
+
end
|
84
|
+
end
|
77
85
|
end
|
78
86
|
describe "when request came from a XSLT NOT enable browser" do
|
79
87
|
it "should parse XML to HTML" do
|
@@ -94,6 +102,17 @@ describe Alexander::XslProcessor do
|
|
94
102
|
response.body.must_equal @dummy_app.xml.body
|
95
103
|
end
|
96
104
|
end
|
105
|
+
describe "when response is XML" do
|
106
|
+
describe "when filter is set to force XSLT processing" do
|
107
|
+
it "should parse XML to HTML" do
|
108
|
+
@filter = Alexander::XslProcessor.new(@dummy_app, force_xslt_processing: true)
|
109
|
+
status, headers, response = @filter.call(env_with_chrome)
|
110
|
+
status.must_equal 200
|
111
|
+
headers["Content-type"].must_equal "text/html"
|
112
|
+
response.body.must_equal ["<html><body></body></html>\n"]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
97
116
|
end
|
98
117
|
end
|
99
118
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alexander
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -173,6 +173,7 @@ files:
|
|
173
173
|
- lib/alexander.rb
|
174
174
|
- lib/alexander/version.rb
|
175
175
|
- lib/alexander/xsl_processor.rb
|
176
|
+
- run_guard.rb
|
176
177
|
- spec/alexander/xsl_processor_spec.rb
|
177
178
|
homepage: http://github.com/ruliana/alexander
|
178
179
|
licenses: []
|