jason 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +6 -0
- data/lib/jason.rb +22 -4
- data/lib/jason/version.rb +1 -1
- data/test/test_jason.rb +15 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -47,6 +47,12 @@ You can configure the output format of the JSON:
|
|
47
47
|
`:compact` will remove any unnecessary whitespace in the JSON while `:pretty`
|
48
48
|
will indent the result JSON so that it looks, well, pretty.
|
49
49
|
|
50
|
+
You can add output listeners to jason so that you can run arbitrary code upon
|
51
|
+
the generated JSON. For example, if you'd like to log all of the JSON responses
|
52
|
+
in Rails:
|
53
|
+
|
54
|
+
Jason.output_listeners << lambda { |json| Rails.logger.info(json) }
|
55
|
+
|
50
56
|
## Usage with Rails ##
|
51
57
|
|
52
58
|
Name your view template with the extension `jason`. Everything else is the same.
|
data/lib/jason.rb
CHANGED
@@ -6,6 +6,7 @@ require 'strscan'
|
|
6
6
|
module Jason
|
7
7
|
class << self
|
8
8
|
attr_writer :output_format
|
9
|
+
attr_writer :output_listeners
|
9
10
|
|
10
11
|
# The output format of the JSON.
|
11
12
|
#
|
@@ -16,6 +17,18 @@ module Jason
|
|
16
17
|
def output_format
|
17
18
|
@output_format ||= :compact
|
18
19
|
end
|
20
|
+
|
21
|
+
# The output listeners for jason.
|
22
|
+
#
|
23
|
+
# All objects in this array are sent #call with the generated JSON whenever
|
24
|
+
# jason processes a buffer. This can be useful for logging output like so:
|
25
|
+
#
|
26
|
+
# Jason.output_listeners << lambda { |json| Rails.logger.info(json) }
|
27
|
+
#
|
28
|
+
# @returns [<#call>]
|
29
|
+
def output_listeners
|
30
|
+
@output_listeners ||= []
|
31
|
+
end
|
19
32
|
end
|
20
33
|
|
21
34
|
# Render a template.
|
@@ -48,19 +61,24 @@ module Jason
|
|
48
61
|
|
49
62
|
# Process a rendered buffer.
|
50
63
|
#
|
51
|
-
# Removes any trailing commas and compresses the buffer.
|
64
|
+
# Removes any trailing commas and compresses the buffer. After generating the
|
65
|
+
# JSON, it calls each one of the output listeners with the generated JSON.
|
52
66
|
#
|
53
|
-
#
|
67
|
+
# You should not have to directly call this method.
|
54
68
|
#
|
55
69
|
# @param [String] buffer
|
56
70
|
def self.process(buffer)
|
57
71
|
obj = JSON.load(remove_trailing_commas(buffer))
|
58
72
|
|
59
73
|
if output_format == :pretty
|
60
|
-
JSON.pretty_generate(obj)
|
74
|
+
json = JSON.pretty_generate(obj)
|
61
75
|
else
|
62
|
-
JSON.generate(obj)
|
76
|
+
json = JSON.generate(obj)
|
63
77
|
end
|
78
|
+
|
79
|
+
output_listeners.each { |listener| listener.call(json) }
|
80
|
+
|
81
|
+
json
|
64
82
|
end
|
65
83
|
|
66
84
|
private
|
data/lib/jason/version.rb
CHANGED
data/test/test_jason.rb
CHANGED
@@ -85,4 +85,19 @@ EOF
|
|
85
85
|
assert_equal :pretty, Jason.output_format
|
86
86
|
assert_equal "{\n \"foo\": [\n \"bar\"\n ]\n}", Jason.process('{"foo":["bar"]}')
|
87
87
|
end
|
88
|
+
|
89
|
+
test '.output_listeners' do
|
90
|
+
assert_equal [], Jason.output_listeners
|
91
|
+
|
92
|
+
listener = mock
|
93
|
+
listener.expects(:call).with('{"foo":["bar"]}')
|
94
|
+
Jason.output_listeners << listener
|
95
|
+
|
96
|
+
Jason.process('{"foo":["bar"]}')
|
97
|
+
end
|
98
|
+
|
99
|
+
def teardown
|
100
|
+
Jason.output_format = :compact
|
101
|
+
Jason.output_listeners = []
|
102
|
+
end
|
88
103
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jason
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.5.0
|
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-06-
|
13
|
+
date: 2011-06-07 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|