jason 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/Rakefile +1 -1
- data/lib/jason.rb +12 -1
- data/lib/jason/version.rb +1 -1
- data/test/rails/test/test_helper.rb +1 -0
- data/test/test_jason.rb +38 -10
- metadata +2 -2
data/.travis.yml
CHANGED
data/Rakefile
CHANGED
data/lib/jason.rb
CHANGED
@@ -32,6 +32,13 @@ module Jason
|
|
32
32
|
"#{eruby_template(template).src}; Jason.process(_buf)"
|
33
33
|
end
|
34
34
|
|
35
|
+
# Process a rendered buffer.
|
36
|
+
#
|
37
|
+
# Removes any trailing commas and compresses the buffer.
|
38
|
+
#
|
39
|
+
# Usually, you should not have to call this method.
|
40
|
+
#
|
41
|
+
# @param [String] buffer
|
35
42
|
def self.process(buffer)
|
36
43
|
JSON.load(remove_trailing_commas(buffer)).to_json
|
37
44
|
end
|
@@ -42,7 +49,11 @@ module Jason
|
|
42
49
|
JSONEruby.new(template)
|
43
50
|
end
|
44
51
|
|
52
|
+
# Based upon the string scanner found in jnunemaker's crack.
|
53
|
+
#
|
54
|
+
# @see https://github.com/jnunemaker/crack crack
|
45
55
|
def self.remove_trailing_commas(json)
|
56
|
+
json = json.dup
|
46
57
|
comma_position = nil
|
47
58
|
quoting = nil
|
48
59
|
scanner = StringScanner.new(json)
|
@@ -56,7 +67,7 @@ module Jason
|
|
56
67
|
quoting = nil
|
57
68
|
end
|
58
69
|
when ']', '}'
|
59
|
-
if comma_position && json[comma_position + 1...scanner.pos - 1] =~
|
70
|
+
if comma_position && json[comma_position + 1...scanner.pos - 1] =~ /\A\s*\z/
|
60
71
|
json[comma_position] = ''
|
61
72
|
scanner.pos -= 1
|
62
73
|
comma_position = nil
|
data/lib/jason/version.rb
CHANGED
data/test/test_jason.rb
CHANGED
@@ -3,18 +3,46 @@ require 'test_helper'
|
|
3
3
|
class TestJason < MiniTest::Unit::TestCase
|
4
4
|
test '.render without binding' do
|
5
5
|
template = <<-EOF
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
{
|
7
|
+
"message": "OK",
|
8
|
+
"user": {
|
9
|
+
"id": <%= 'test' %>,
|
10
|
+
"name": <%= 'blah' %>,
|
11
|
+
<% if true %>
|
12
|
+
"link": [
|
13
|
+
<% if true %>
|
14
|
+
{
|
15
|
+
"rel": "self",
|
16
|
+
"href": <%= 'hablaba' %>
|
17
|
+
},
|
18
|
+
<% end %>
|
19
|
+
{
|
20
|
+
"rel": "checkins",
|
21
|
+
"href": <%= 'hablaba' %>
|
22
|
+
}
|
23
|
+
]
|
24
|
+
<% end %>
|
25
|
+
}
|
26
|
+
}
|
27
|
+
EOF
|
28
|
+
|
29
|
+
assert_equal({
|
30
|
+
'message' => 'OK',
|
31
|
+
'user' => {
|
32
|
+
'id' => 'test',
|
33
|
+
'name' => 'blah',
|
34
|
+
'link' => [
|
35
|
+
{
|
36
|
+
'rel' => 'self',
|
37
|
+
'href' => 'hablaba'
|
38
|
+
},
|
39
|
+
{
|
40
|
+
'rel' => 'checkins',
|
41
|
+
'href' => 'hablaba'
|
42
|
+
}
|
13
43
|
]
|
14
44
|
}
|
15
|
-
|
16
|
-
|
17
|
-
assert_equal({ 'foo' => 'bar', 'baz' => ['quz', 'quuz'] }, JSON.load(Jason.render(template)))
|
45
|
+
}, JSON.load(Jason.render(template)))
|
18
46
|
end
|
19
47
|
|
20
48
|
test '.render with binding' do
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jason
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.3.
|
5
|
+
version: 0.3.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Alexander Kern
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-06-01 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|