json-emitter 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82b399ea5929c610e5f113a35c72b9df9f6cf9957292dc8e2dde5837472484c3
4
- data.tar.gz: 9925436be580fa8b772a18e4bad918ffcb3938f638cf0e633db4788c4e1df52c
3
+ metadata.gz: 8fb1f58b4cbaa70be670062103c939cad985a21b709862771989851795a425c8
4
+ data.tar.gz: 58aa111fa70c53090f33ccf8103086a0615ea0e4ac5cfca1b5d42f6047c43107
5
5
  SHA512:
6
- metadata.gz: 85048433a36d8d985d57099c2d0dacc0f5ddb31eb128ef8f030c8136224bd37d101db41ed14aff3f8a59b9c5958aa4e391b4b28598242ff1978d6c65d114f206
7
- data.tar.gz: e058dd052d8604acbf8c8516a887dd1672a0018faf4d25c874feacf273655fbd83f10c587982fa028c98b16dd67bccd32d51421ba5a8bb49395096672df74026
6
+ metadata.gz: 309af9a8b1e04ba28ee14f5a583d70852fc23db51ec6eaa5d14d0590f42381ef1fe502909b5449880fedff08bc4e6920112651ff7ce0f6480ff3c71de5ca7e8c
7
+ data.tar.gz: 15276de4e33f4d8506736aef13c090ba4c7dcc42a301cecbbf216be58f3e9e514a339f1acfb34ed389f8dbd7c18d92c420477ab47cd90448e461b380cb1c6433
@@ -3,6 +3,13 @@ module JsonEmitter
3
3
  # Builds Enumerators that yield JSON from Ruby Arrays or Hashes.
4
4
  #
5
5
  class Emitter
6
+ def initialize
7
+ @wrappers = JsonEmitter.wrappers.map(&:call).compact
8
+ @error_handlers = JsonEmitter.error_handlers
9
+ @pass_through_errors = []
10
+ @pass_through_errors << Puma::ConnectionError if defined? Puma::ConnectionError
11
+ end
12
+
6
13
  #
7
14
  # Generates an Enumerator that will stream out a JSON array.
8
15
  #
@@ -11,6 +18,48 @@ module JsonEmitter
11
18
  # @return [Enumerator]
12
19
  #
13
20
  def array(enum, &mapper)
21
+ Enumerator.new { |y|
22
+ wrapped {
23
+ array_generator(enum, &mapper).each { |json_val|
24
+ y << json_val
25
+ }
26
+ }
27
+ }
28
+ end
29
+
30
+ #
31
+ # Generates an Enumerator that will stream out a JSON object.
32
+ #
33
+ # @param hash [Hash] Keys should be Strings or Symbols and values should be any JSON-compatible value like a number, string, boolean, Array, or Hash.
34
+ # @return [Enumerator]
35
+ #
36
+ def object(hash)
37
+ Enumerator.new { |y|
38
+ wrapped {
39
+ object_generator(hash).each { |json_val|
40
+ y << json_val
41
+ }
42
+ }
43
+ }
44
+ end
45
+
46
+ # Wrap the enumeration in a block. It will be passed a callback which it must call to continue.
47
+ # TODO better docs and examples.
48
+ def wrap(&block)
49
+ if (wrapper = block.call)
50
+ @wrappers.unshift wrapper
51
+ end
52
+ end
53
+
54
+ # Add an error handler.
55
+ # TODO better docs and examples.
56
+ def error(&handler)
57
+ @error_handlers += [handler]
58
+ end
59
+
60
+ private
61
+
62
+ def array_generator(enum, &mapper)
14
63
  Enumerator.new { |y|
15
64
  y << "[".freeze
16
65
 
@@ -29,13 +78,7 @@ module JsonEmitter
29
78
  }
30
79
  end
31
80
 
32
- #
33
- # Generates an Enumerator that will stream out a JSON object.
34
- #
35
- # @param hash [Hash] Keys should be Strings or Symbols and values should be any JSON-compatible value like a number, string, boolean, Array, or Hash.
36
- # @return [Enumerator]
37
- #
38
- def object(hash)
81
+ def object_generator(hash)
39
82
  Enumerator.new { |y|
40
83
  y << "{".freeze
41
84
 
@@ -56,17 +99,30 @@ module JsonEmitter
56
99
  }
57
100
  end
58
101
 
59
- private
60
-
61
102
  def json_values(x)
62
103
  case x
63
- when Hash then object x
64
- when Enumerable then array x
104
+ when Hash
105
+ object_generator x
106
+ when Enumerable
107
+ array_generator x
65
108
  when Proc
66
109
  y = x.call
67
110
  json_values y
68
- else [MultiJson.dump(x)]
111
+ else
112
+ [MultiJson.dump(x)]
69
113
  end
70
114
  end
115
+
116
+ def wrapped(&final)
117
+ @wrappers.reduce(final) { |f, outer_wrapper|
118
+ ->() { outer_wrapper.call(f) }
119
+ }.call
120
+
121
+ rescue *@pass_through_errors => e
122
+ raise e
123
+ rescue => e
124
+ @error_handlers.each { |h| h.call(e) }
125
+ raise e
126
+ end
71
127
  end
72
128
  end
@@ -1,4 +1,4 @@
1
1
  module JsonEmitter
2
2
  # Library version
3
- VERSION = "0.0.1".freeze
3
+ VERSION = "0.0.2".freeze
4
4
  end
data/lib/json-emitter.rb CHANGED
@@ -16,6 +16,13 @@ require 'json-emitter/buffered_stream'
16
16
  # buffered. This works very well with so-called "HTTP chunked responses" in Rack/Rails/Sinatra/Grape/etc.
17
17
  #
18
18
  module JsonEmitter
19
+ class << self
20
+ attr_reader :wrappers
21
+ attr_reader :error_handlers
22
+ end
23
+ @wrappers = []
24
+ @error_handlers = []
25
+
19
26
  #
20
27
  # Generates an stream that will output a JSON array. The input can be any Enumerable, such as an Array or an Enumerator.
21
28
  #
@@ -97,4 +104,16 @@ module JsonEmitter
97
104
  emitter = Emitter.new.object(hash)
98
105
  Stream.new(emitter)
99
106
  end
107
+
108
+ # Wrap the enumeration in a Proc. It will be passed a callback which it must call to continue.
109
+ # TODO better docs and examples.
110
+ def self.wrap(&wrapper)
111
+ @wrappers.unshift wrapper
112
+ end
113
+
114
+ # Add an error handler.
115
+ # TODO better docs and examples.
116
+ def self.error(&handler)
117
+ @error_handlers << handler
118
+ end
100
119
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-emitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-29 00:00:00.000000000 Z
11
+ date: 2019-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json