haml_interceptor 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ gem 'rake', '>= 0.8.7'
8
8
 
9
9
  gem 'haml', '>= 3.1.0'
10
10
  gem 'hpricot', '>= 0.8.4'
11
-
11
+ gem 'json_pure', '>= 1.5.0'
12
12
 
13
13
  # Add dependencies to develop your gem here.
14
14
  # Include everything needed to run rake, tests, features, etc.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{haml_interceptor}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Scofield"]
@@ -45,6 +45,7 @@ Gem::Specification.new do |s|
45
45
  s.add_runtime_dependency(%q<rake>, [">= 0.8.7"])
46
46
  s.add_runtime_dependency(%q<haml>, [">= 3.1.0"])
47
47
  s.add_runtime_dependency(%q<hpricot>, [">= 0.8.4"])
48
+ s.add_runtime_dependency(%q<json_pure>, [">= 1.5.0"])
48
49
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
50
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
50
51
  s.add_development_dependency(%q<rcov>, [">= 0"])
@@ -54,6 +55,7 @@ Gem::Specification.new do |s|
54
55
  s.add_dependency(%q<rake>, [">= 0.8.7"])
55
56
  s.add_dependency(%q<haml>, [">= 3.1.0"])
56
57
  s.add_dependency(%q<hpricot>, [">= 0.8.4"])
58
+ s.add_dependency(%q<json_pure>, [">= 1.5.0"])
57
59
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
60
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
59
61
  s.add_dependency(%q<rcov>, [">= 0"])
@@ -64,6 +66,7 @@ Gem::Specification.new do |s|
64
66
  s.add_dependency(%q<rake>, [">= 0.8.7"])
65
67
  s.add_dependency(%q<haml>, [">= 3.1.0"])
66
68
  s.add_dependency(%q<hpricot>, [">= 0.8.4"])
69
+ s.add_dependency(%q<json_pure>, [">= 1.5.0"])
67
70
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
68
71
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
69
72
  s.add_dependency(%q<rcov>, [">= 0"])
@@ -8,8 +8,9 @@ class HamlInterceptor
8
8
  def call(env)
9
9
  status, headers, resp = @app.call(env)
10
10
  body = resp.body.respond_to?(:join) ? resp.body.join : resp.body
11
+
11
12
  if js_response?(env, headers)
12
- body = Haml::HTML.new(body).render
13
+ body = parse(body)
13
14
  end
14
15
  [status, headers, body]
15
16
  end
@@ -17,4 +18,16 @@ class HamlInterceptor
17
18
  def js_response?(env, headers)
18
19
  env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' || (headers.has_key?('Content-Type') && headers['Content-Type'] =~ /javascript/)
19
20
  end
21
+
22
+ def parse(body, json = true)
23
+ parsed = json ? (JSON.parse(body) rescue body) : body
24
+ if parsed.respond_to?(:values)
25
+ parsed.keys.each do |key|
26
+ parsed[key] = parse(parsed[key], false)
27
+ end
28
+ else
29
+ parsed = Haml::HTML.new(parsed).render
30
+ end
31
+ json ? (JSON.generate(parsed) rescue parsed) : parsed
32
+ end
20
33
  end
data/test/sample_app.rb CHANGED
@@ -1,13 +1,17 @@
1
+ require 'json'
1
2
  class SampleApp
2
3
  def call(env)
3
4
  request = Rack::Request.new(env)
4
5
 
5
6
  if request.path == '/'
6
7
  content_type = 'text/html'
7
- body = '<html><head><title>Boneshaker</title></head><body><p>Hello!</p></body></html>'
8
- else
8
+ body = '<html><head><title>Boneshaker</title></head><body><p>by Cherie Priest</p></body></html>'
9
+ elsif request.path == '/ajax'
9
10
  content_type = 'application/javascript'
10
11
  body = '<h1>Boneshaker</h1><div id="author"><p>by Cherie Priest</p></div>'
12
+ elsif request.path == '/json'
13
+ content_type = 'application/javascript'
14
+ body = JSON.generate({:name => 'Boneshaker', :markup => '<h1>Boneshaker</h1><div id="author"><p>by Cherie Priest</p></div>'})
11
15
  end
12
16
 
13
17
  return Rack::Response.new(body, 200, {'Content-Type' => content_type}).finish
@@ -10,7 +10,7 @@ class TestHamlInterceptor < Test::Unit::TestCase
10
10
 
11
11
  def test_it_should_ignore_html_responses
12
12
  get '/'
13
- assert_equal last_response.body, '<html><head><title>Boneshaker</title></head><body><p>Hello!</p></body></html>'
13
+ assert_equal '<html><head><title>Boneshaker</title></head><body><p>by Cherie Priest</p></body></html>', last_response.body
14
14
  end
15
15
 
16
16
  def test_it_should_capture_js_responses_by_content_type
@@ -29,7 +29,13 @@ HAML
29
29
  %head
30
30
  %title Boneshaker
31
31
  %body
32
- %p Hello!
32
+ %p by Cherie Priest
33
33
  HAML
34
34
  end
35
+
36
+ def test_it_should_descend_into_json_responses
37
+ get '/json'
38
+ assert_equal JSON.parse('{"name":"Boneshaker\n", "markup":"%h1 Boneshaker\n#author\n %p by Cherie Priest\n"}'),
39
+ JSON.parse(last_response.body)
40
+ end
35
41
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: haml_interceptor
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Scofield
@@ -58,8 +58,19 @@ dependencies:
58
58
  prerelease: false
59
59
  version_requirements: *id004
60
60
  - !ruby/object:Gem::Dependency
61
- name: bundler
61
+ name: json_pure
62
62
  requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 1.5.0
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: bundler
73
+ requirement: &id006 !ruby/object:Gem::Requirement
63
74
  none: false
64
75
  requirements:
65
76
  - - ~>
@@ -67,10 +78,10 @@ dependencies:
67
78
  version: 1.0.0
68
79
  type: :development
69
80
  prerelease: false
70
- version_requirements: *id005
81
+ version_requirements: *id006
71
82
  - !ruby/object:Gem::Dependency
72
83
  name: jeweler
73
- requirement: &id006 !ruby/object:Gem::Requirement
84
+ requirement: &id007 !ruby/object:Gem::Requirement
74
85
  none: false
75
86
  requirements:
76
87
  - - ~>
@@ -78,10 +89,10 @@ dependencies:
78
89
  version: 1.6.2
79
90
  type: :development
80
91
  prerelease: false
81
- version_requirements: *id006
92
+ version_requirements: *id007
82
93
  - !ruby/object:Gem::Dependency
83
94
  name: rcov
84
- requirement: &id007 !ruby/object:Gem::Requirement
95
+ requirement: &id008 !ruby/object:Gem::Requirement
85
96
  none: false
86
97
  requirements:
87
98
  - - ">="
@@ -89,10 +100,10 @@ dependencies:
89
100
  version: "0"
90
101
  type: :development
91
102
  prerelease: false
92
- version_requirements: *id007
103
+ version_requirements: *id008
93
104
  - !ruby/object:Gem::Dependency
94
105
  name: rack-test
95
- requirement: &id008 !ruby/object:Gem::Requirement
106
+ requirement: &id009 !ruby/object:Gem::Requirement
96
107
  none: false
97
108
  requirements:
98
109
  - - ~>
@@ -100,7 +111,7 @@ dependencies:
100
111
  version: 0.6.0
101
112
  type: :development
102
113
  prerelease: false
103
- version_requirements: *id008
114
+ version_requirements: *id009
104
115
  description: haml_interceptor is a Rack middleware designed to intercept JS responses from a web application and re-process them from HTML to Haml, resulting in a smaller package sent down the wire to the client.
105
116
  email: git@turrean.com
106
117
  executables: []
@@ -138,7 +149,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
149
  requirements:
139
150
  - - ">="
140
151
  - !ruby/object:Gem::Version
141
- hash: -1072519243524474088
152
+ hash: -1031114291824803886
142
153
  segments:
143
154
  - 0
144
155
  version: "0"