json-emitter 0.0.2 → 0.0.3

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
  SHA256:
3
- metadata.gz: 8fb1f58b4cbaa70be670062103c939cad985a21b709862771989851795a425c8
4
- data.tar.gz: 58aa111fa70c53090f33ccf8103086a0615ea0e4ac5cfca1b5d42f6047c43107
3
+ metadata.gz: d39d9a362efddd2cb39412926290328feb042228099e2b737d6d8450fad28cdd
4
+ data.tar.gz: ff8a418af3119910c46d78320e8b4dcac3cd6c0282202e1bae970c56fadf3e12
5
5
  SHA512:
6
- metadata.gz: 309af9a8b1e04ba28ee14f5a583d70852fc23db51ec6eaa5d14d0590f42381ef1fe502909b5449880fedff08bc4e6920112651ff7ce0f6480ff3c71de5ca7e8c
7
- data.tar.gz: 15276de4e33f4d8506736aef13c090ba4c7dcc42a301cecbbf216be58f3e9e514a339f1acfb34ed389f8dbd7c18d92c420477ab47cd90448e461b380cb1c6433
6
+ metadata.gz: b5cc83b5e2018d7f9b05f98f3cbfa9ab3f45176ff96e121f4336303466abdfb1836db673c5b567ae3579de7cdf9bf3d95b7879488ef35a25d34422d11177c1ed
7
+ data.tar.gz: 33cc0cf57bedd84fda214c19859a32289c31d08e5e1d587d55485e73b5dab81030cd4fc12b14ed0c6e8234238821492fc694e603ad4d7ee8193f479de867ad6c
data/README.md CHANGED
@@ -50,11 +50,6 @@ File.open("/tmp/foo.json", "w+") { |file|
50
50
  stream.each { |json_chunk|
51
51
  ...
52
52
  }
53
-
54
- # this will buffer the JSON into roughly 8k chunks
55
- stream.buffered(8).each { |json_8k_chunk|
56
- ...
57
- }
58
53
  ```
59
54
 
60
55
  # HTTP Chunked Transfer (a.k.a streaming)
@@ -90,7 +85,7 @@ get :orders do
90
85
 
91
86
  stream JsonEmitter.array(enumerator) { |order|
92
87
  ApiV1::Entities::Order.new(order)
93
- }.buffered(16)
88
+ }
94
89
  end
95
90
  ```
96
91
 
@@ -104,7 +99,7 @@ app = ->(env) {
104
99
 
105
100
  stream = JsonEmitter.array(enumerator) { |order|
106
101
  order.to_h
107
- }.buffered(16)
102
+ }
108
103
 
109
104
  [200, {"Content-Type" => "application/json"}, stream]
110
105
  }
@@ -23,6 +23,15 @@ module JsonEmitter
23
23
  end
24
24
  end
25
25
 
26
+ #
27
+ # Returns an unbuffered version of the stream.
28
+ #
29
+ # @return [JsonEmitter::Stream]
30
+ #
31
+ def unbuffered
32
+ Stream.new(@enum)
33
+ end
34
+
26
35
  #
27
36
  # Write the stream to the specified IO object.
28
37
  #
@@ -1,7 +1,7 @@
1
1
  module JsonEmitter
2
2
  #
3
3
  # Represents a stream of JSON to be generated and yielded. It can be treated like any Enumerable.
4
- # The size of the yielded strings can vary from 1 to 1000's. If that's a problem, call JsonEmitter::Stream.buffer.
4
+ # Unlike UnbufferedStream, the size of the yielded strings can vary from 1 to 1000's.
5
5
  #
6
6
  class Stream
7
7
  include Enumerable
@@ -15,17 +15,6 @@ module JsonEmitter
15
15
  @enum = enum
16
16
  end
17
17
 
18
- #
19
- # Returns a new stream that will buffer the output. You can perform the same "write" or "each" operations
20
- # on the new stream, but the chunks of output will be (roughly) uniform in size.
21
- #
22
- # @param buffer_size [Integer] The buffer size in kb. This is a size *hint*, not a hard limit.
23
- # @return [JsonEmitter::BufferedStream]
24
- #
25
- def buffered(buffer_size = 16, unit: :kb)
26
- BufferedStream.new(@enum, buffer_size, unit: unit)
27
- end
28
-
29
18
  #
30
19
  # Write the stream to the specified IO object.
31
20
  #
@@ -1,4 +1,4 @@
1
1
  module JsonEmitter
2
2
  # Library version
3
- VERSION = "0.0.2".freeze
3
+ VERSION = "0.0.3".freeze
4
4
  end
data/lib/json-emitter.rb CHANGED
@@ -12,8 +12,8 @@ require 'json-emitter/buffered_stream'
12
12
  # Primitive values will be serialized to JSON using MultiJson.dump. MultiJson finds and uses the most efficient
13
13
  # JSON generator you have on your system (e.g. oj) and falls back to the stdlib JSON library.
14
14
  #
15
- # The emitter can be used to output to anything (files, network sockets, etc), and the output can optionally be
16
- # buffered. This works very well with so-called "HTTP chunked responses" in Rack/Rails/Sinatra/Grape/etc.
15
+ # The emitter can be used to output to anything (files, network sockets, etc). It works very well with so-called
16
+ # "HTTP chunked responses" in Rack/Rails/Sinatra/Grape/etc.
17
17
  #
18
18
  module JsonEmitter
19
19
  class << self
@@ -47,17 +47,15 @@ module JsonEmitter
47
47
  # # do something with each json chunk
48
48
  # end
49
49
  #
50
- # # if you need the outputted chunks to be (roughly) equal in size, call "buffered"
51
- # # and pass in the buffer size in kb.
52
- # buffered_stream = stream.buffered(16)
53
- #
54
50
  # @param enum [Enumerable] Something that can be enumerated over, like an Array or Enumerator. Each element should be something that can be rendered as JSON (e.g. a number, string, boolean, Array, or Hash).
51
+ # @param buffer_size [Integer] The buffer size in kb. This is a size *hint*, not a hard limit.
52
+ # @param unit [Symbol] :bytes | :kb (default) | :mb
55
53
  # @yield If a block is given, it will be yielded each value in the array. The return value from the block will be converted to JSON instead of the original value.
56
- # @return [JsonEmitter::Stream]
54
+ # @return [JsonEmitter::BufferedStream]
57
55
  #
58
- def self.array(enum, &mapper)
56
+ def self.array(enum, buffer_size: 16, buffer_unit: :kb, &mapper)
59
57
  emitter = Emitter.new.array(enum, &mapper)
60
- Stream.new(emitter)
58
+ BufferedStream.new(emitter, buffer_size, unit: buffer_unit)
61
59
  end
62
60
 
63
61
  #
@@ -93,16 +91,14 @@ module JsonEmitter
93
91
  # # do something with each json chunk
94
92
  # end
95
93
  #
96
- # # if you need the outputted chunks to be (roughly) equal in size, call "buffered"
97
- # # and pass in the buffer size in kb.
98
- # buffered_stream = stream.buffered(16)
99
- #
100
94
  # @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.
101
- # @return [JsonEmitter::Stream]
95
+ # @param buffer_size [Integer] The buffer size in kb. This is a size *hint*, not a hard limit.
96
+ # @param unit [Symbol] :bytes | :kb (default) | :mb
97
+ # @return [JsonEmitter::BufferedStream]
102
98
  #
103
- def self.object(hash)
99
+ def self.object(hash, buffer_size: 16, buffer_unit: :kb)
104
100
  emitter = Emitter.new.object(hash)
105
- Stream.new(emitter)
101
+ BufferedStream.new(emitter, buffer_size, unit: buffer_unit)
106
102
  end
107
103
 
108
104
  # Wrap the enumeration in a Proc. It will be passed a callback which it must call to continue.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-emitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger