json-write-stream 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b059dbe6cf700cf762a6f14734e097c17d90d76a
4
- data.tar.gz: b48ccf8bc0205baf88a2b2a9181199873ef01da7
3
+ metadata.gz: 7445d1cf85a82a9414433ebbdb06008eac6ac4e2
4
+ data.tar.gz: a82b04356133b8b46c21906bab7d413aebeac855
5
5
  SHA512:
6
- metadata.gz: 8f8247711d377344b4a4d8164e5dd1ecb357253bae33df1a6e97154da3ffbca1f26f5bca91cb3d5227f53334300e5ffa79f002b52ccbe13cb67a826950524cff
7
- data.tar.gz: 10401cfc09651446c449e2c1f1e1bbd66b0db653022f58c669d49d06f512b403dde8383f42b8a96bd62a6201304a11a2bc3692537ba98b2db97579c06583081d
6
+ metadata.gz: 3818ce8ae032e722dbde636aaefa818975e91ab25a80e20045da648564051dffde7c12fb9e9291c9ecb2ad0bad48c2438665b672f2f773549f60339f6f4b163a
7
+ data.tar.gz: 3632c1b6c3b499f20f46e3577f60221a15aec7a10e523a3d39b84b9d6513a9ff83deb6ee8bed07cc4d3dad5fedd7ef6f9b3a1cc255ce7a00f397ee2835cbc27a
@@ -13,3 +13,7 @@
13
13
  == 1.1.0
14
14
 
15
15
  * Add way of inserting text after commas.
16
+
17
+ == 1.2.0
18
+
19
+ * Add way of inserting text between key/value pairs.
@@ -6,6 +6,7 @@ require 'json-write-stream/stateful'
6
6
 
7
7
  class JsonWriteStream
8
8
  DEFAULT_ENCODING = Encoding::UTF_8
9
+ DEFAULT_OPTIONS = { before: '', between: '' }.freeze
9
10
 
10
11
  class << self
11
12
  def from_stream(stream, encoding = DEFAULT_ENCODING)
@@ -139,12 +139,14 @@ class JsonWriteStream
139
139
  stream.write(':')
140
140
  end
141
141
 
142
- def write_key_value(key, value, before = '')
142
+ def write_key_value(key, value, options = DEFAULT_OPTIONS)
143
143
  write_comma
144
- stream.write(before)
144
+ stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
145
145
  increment
146
146
  write_key(key)
147
- stream.write(":#{escape(value)}")
147
+ stream.write(':')
148
+ stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
149
+ stream.write(escape(value))
148
150
  end
149
151
 
150
152
  def close
@@ -176,9 +178,9 @@ class JsonWriteStream
176
178
  stream.write('[')
177
179
  end
178
180
 
179
- def write_element(element, before = '')
181
+ def write_element(element, options = DEFAULT_OPTIONS)
180
182
  write_comma
181
- stream.write(before)
183
+ stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
182
184
  increment
183
185
  stream.write(escape(element))
184
186
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  class JsonWriteStream
4
- VERSION = "1.1.0"
4
+ VERSION = '1.2.0'
5
5
  end
@@ -60,30 +60,34 @@ class JsonWriteStream
60
60
  stream.write('{')
61
61
  end
62
62
 
63
- def write_array(key, before = '')
63
+ def write_array(key, options = DEFAULT_OPTIONS)
64
64
  write_comma
65
- stream.write(before)
65
+ stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
66
66
  increment
67
67
  write_key(key)
68
68
  stream.write(':')
69
+ stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
69
70
  super(true)
70
71
  end
71
72
 
72
- def write_object(key, before = '')
73
+ def write_object(key, options = DEFAULT_OPTIONS)
73
74
  write_comma
74
- stream.write(before)
75
+ stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
75
76
  increment
76
77
  write_key(key)
77
78
  stream.write(':')
79
+ stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
78
80
  super(true)
79
81
  end
80
82
 
81
- def write_key_value(key, value, before = '')
83
+ def write_key_value(key, value, options = DEFAULT_OPTIONS)
82
84
  write_comma
83
- stream.write(before)
85
+ stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
84
86
  increment
85
87
  write_key(key)
86
- stream.write(":#{escape(value)}")
88
+ stream.write(':')
89
+ stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
90
+ stream.write(escape(value))
87
91
  end
88
92
 
89
93
  def close
@@ -102,9 +106,9 @@ class JsonWriteStream
102
106
  stream.write('[')
103
107
  end
104
108
 
105
- def write_element(element, before = '')
109
+ def write_element(element, options = DEFAULT_OPTIONS)
106
110
  write_comma
107
- stream.write(before)
111
+ stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
108
112
  increment
109
113
  stream.write(escape(element))
110
114
  end
@@ -21,6 +21,22 @@ describe JsonWriteStream::YieldingWriter do
21
21
 
22
22
  it_behaves_like 'a json stream'
23
23
 
24
+ it 'respects the "before" option' do
25
+ stream_writer.write_object
26
+ stream_writer.write_key_value('foo', 'bar', before: "\n ")
27
+ stream_writer.close
28
+
29
+ expect(stream.string).to eq("{\n \"foo\":\"bar\"}")
30
+ end
31
+
32
+ it 'respects the "between" option' do
33
+ stream_writer.write_object
34
+ stream_writer.write_key_value('foo', 'bar', between: ' ')
35
+ stream_writer.close
36
+
37
+ expect(stream.string).to eq('{"foo": "bar"}')
38
+ end
39
+
24
40
  describe '#close' do
25
41
  it 'unwinds the stack, adds appropriate closing punctuation for each unclosed item, and closes the stream' do
26
42
  stream_writer.write_array
@@ -21,6 +21,22 @@ describe JsonWriteStream::YieldingWriter do
21
21
 
22
22
  it_behaves_like 'a json stream'
23
23
 
24
+ it 'respects the "before" option' do
25
+ stream_writer.write_object do |object_writer|
26
+ object_writer.write_key_value('foo', 'bar', before: "\n ")
27
+ end
28
+
29
+ expect(stream.string).to eq("{\n \"foo\":\"bar\"}")
30
+ end
31
+
32
+ it 'respects the "between" option' do
33
+ stream_writer.write_object do |object_writer|
34
+ object_writer.write_key_value('foo', 'bar', between: ' ')
35
+ end
36
+
37
+ expect(stream.string).to eq('{"foo": "bar"}')
38
+ end
39
+
24
40
  describe '#write_key_value' do
25
41
  it 'converts all keys to strings' do
26
42
  stream_writer.write_object do |object_writer|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-write-stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-19 00:00:00.000000000 Z
11
+ date: 2017-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_pure
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  version: '0'
65
65
  requirements: []
66
66
  rubyforge_project:
67
- rubygems_version: 2.2.3
67
+ rubygems_version: 2.5.2
68
68
  signing_key:
69
69
  specification_version: 4
70
70
  summary: An easy, streaming way to generate JSON.