json-streamer 1.1.0 → 1.1.1

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: defb27980ce9856c197b03d9dcd036c6a9676faf
4
- data.tar.gz: be55ab586714595baab1ae9e570fc5c0d1c869d9
3
+ metadata.gz: 9fa1fe76772ac9b1bf8e8b64da2362b13f3d4d5d
4
+ data.tar.gz: 723a1e77a3fbed2ff1a387409633d53f852070c9
5
5
  SHA512:
6
- metadata.gz: ccefc6735d211f77a1cd1c18b05073d0d585033478f8495a922402dbf8c479b9f9eb3804a53c55c6f5b21b6dc8c93873ab9a569df97e95daefd5d04e24a9432d
7
- data.tar.gz: c132b1c90100166e7716959b12fa085e64ebfaa754fcfc919e20c290537704d1e915632846ae680b697fe3a29adefafdd751e1cc79ae6685b510da6c9d6801a2
6
+ metadata.gz: '06830de8e7a7bbadd1e3e0db3cfd77cbad559807ae50c2588c3aeb1f1bbd6554cd04abaf590c40eb93862f519d035eade7dc90934eadb200e8a0afdb37ecacb0'
7
+ data.tar.gz: 0e443f59c654125a9cba390b9ea12575f437e37f280a303b134a403774d5421a7517f379fba7dd7f92124f38e566f16f289b98e181e35f508ac85d2375a3b20a
data/.gitignore CHANGED
@@ -1,11 +1,11 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- .idea/*
11
- *.iml
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/*
11
+ *.iml
data/.gitmodules CHANGED
@@ -1,3 +1,3 @@
1
- [submodule "ruby-devenv"]
2
- path = ruby-devenv
3
- url = https://github.com/thisismydesign/ruby-devenv
1
+ [submodule "ruby-devenv"]
2
+ path = ruby-devenv
3
+ url = https://github.com/thisismydesign/ruby-devenv
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --format documentation
2
- --color
1
+ --format documentation
2
+ --color
data/.travis.yml CHANGED
@@ -1,5 +1,5 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.3.3
5
- before_install: gem install bundler -v 1.14.6
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.6
data/CODE_OF_CONDUCT.md CHANGED
@@ -1,49 +1,49 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, and in the interest of
4
- fostering an open and welcoming community, we pledge to respect all people who
5
- contribute through reporting issues, posting feature requests, updating
6
- documentation, submitting pull requests or patches, and other activities.
7
-
8
- We are committed to making participation in this project a harassment-free
9
- experience for everyone, regardless of level of experience, gender, gender
10
- identity and expression, sexual orientation, disability, personal appearance,
11
- body size, race, ethnicity, age, religion, or nationality.
12
-
13
- Examples of unacceptable behavior by participants include:
14
-
15
- * The use of sexualized language or imagery
16
- * Personal attacks
17
- * Trolling or insulting/derogatory comments
18
- * Public or private harassment
19
- * Publishing other's private information, such as physical or electronic
20
- addresses, without explicit permission
21
- * Other unethical or unprofessional conduct
22
-
23
- Project maintainers have the right and responsibility to remove, edit, or
24
- reject comments, commits, code, wiki edits, issues, and other contributions
25
- that are not aligned to this Code of Conduct, or to ban temporarily or
26
- permanently any contributor for other behaviors that they deem inappropriate,
27
- threatening, offensive, or harmful.
28
-
29
- By adopting this Code of Conduct, project maintainers commit themselves to
30
- fairly and consistently applying these principles to every aspect of managing
31
- this project. Project maintainers who do not follow or enforce the Code of
32
- Conduct may be permanently removed from the project team.
33
-
34
- This code of conduct applies both within project spaces and in public spaces
35
- when an individual is representing the project or its community.
36
-
37
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
- reported by contacting a project maintainer at csapagyi@users.noreply.github.com. All
39
- complaints will be reviewed and investigated and will result in a response that
40
- is deemed necessary and appropriate to the circumstances. Maintainers are
41
- obligated to maintain confidentiality with regard to the reporter of an
42
- incident.
43
-
44
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
- version 1.3.0, available at
46
- [http://contributor-covenant.org/version/1/3/0/][version]
47
-
48
- [homepage]: http://contributor-covenant.org
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at csapagyi@users.noreply.github.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
49
  [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in json-streamer.gemspec
4
- gemspec
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json-streamer.gemspec
4
+ gemspec
data/LICENSE.txt CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2016 Csaba Apagyi
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Csaba Apagyi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,176 +1,176 @@
1
- # Json::Streamer
2
-
3
- #### Ruby utility that supports JSON streaming allowing you to get data based on various criteria (key, nesting level, etc).
4
-
5
- | Branch | Build status |
6
- | ------ | ------------ |
7
- | Release | [![Build Status](https://travis-ci.org/thisismydesign/json-streamer.svg?branch=release)](https://travis-ci.org/thisismydesign/json-streamer) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/json-streamer/badge.svg?branch=release)](https://coveralls.io/github/thisismydesign/json-streamer?branch=release) [![Gem Version](https://badge.fury.io/rb/json-streamer.svg)](https://badge.fury.io/rb/json-streamer) |
8
- | Development | [![Build Status](https://travis-ci.org/thisismydesign/json-streamer.svg?branch=master)](https://travis-ci.org/thisismydesign/json-streamer) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/json-streamer/badge.svg?branch=master)](https://coveralls.io/github/thisismydesign/json-streamer?branch=master) |
9
-
10
- *If you've tried JSON streaming with other Ruby libraries before (e.g. [JSON::Stream](https://github.com/dgraham/json-stream), [Yajl::FFI](https://github.com/dgraham/yajl-ffi)):*
11
-
12
- This gem will basically spare you the need to define your own callbacks (i.e. implement an actual JSON parser using `start_object`, `end_object`, `key`, `value`, etc.).
13
-
14
-
15
- *If you're new to this:*
16
-
17
- Streaming is useful for
18
- - big files that do not fit in the memory (or you'd rather avoid the risk)
19
- - files read in chunks (e.g. arriving over network)
20
- - cases where you expect some issue with the file (e.g. losing connection to source, invalid data at some point) but would like to get as much data as possible anyway
21
-
22
- This gem is aimed at making streaming as easy and convenient as possible.
23
-
24
- *Performance:*
25
-
26
- The gem uses JSON::Stream's events in the background. It was chosen because it's a pure Ruby parser.
27
- A similar implementation can be done using the ~10 times faster Yajl::FFI gem that is dependent on the native YAJL library.
28
- I did not measure the performance of my implementation on top of these libraries.
29
-
30
- ## Installation
31
-
32
- Add this line to your application's Gemfile:
33
-
34
- ```ruby
35
- gem 'json-streamer'
36
- ```
37
-
38
- And then execute:
39
-
40
- $ bundle
41
-
42
- Or install it yourself as:
43
-
44
- $ gem install json-streamer
45
-
46
- ## Usage
47
-
48
- ```ruby
49
- require 'json/streamer'
50
-
51
- file_stream = File.open('data.json', 'r')
52
-
53
- # Get a JsonStreamer object that will parse file_stream by chunks of 500
54
- # Default chunk size in 1000
55
- streamer = Json::Streamer::JsonStreamer.new(file_stream, 500)
56
- ```
57
-
58
- #### Get objects based on nesting level
59
-
60
- ```ruby
61
- # Level zero will give you the full JSON, first level will give you data within full JSON object, etc.
62
- streamer.get(nesting_level:1) do |object|
63
- p object
64
- end
65
- ```
66
-
67
- Input:
68
- ```json
69
- {
70
- "object1": "first_level_value",
71
- "object2": {}
72
- }
73
- ```
74
-
75
- Output:
76
- ```json
77
- "first_level_value"
78
- {}
79
- ```
80
-
81
- #### Get data based on key
82
-
83
- ```ruby
84
- streamer.get(key:'desired_key') do |object|
85
- p object
86
- end
87
- ```
88
-
89
- Input:
90
- ```json
91
- {
92
- "obj1" : {
93
- "desired_key" : "value1"
94
- },
95
- "desired_key" : "value2",
96
- "obj2" : {
97
- "desired_key" : {
98
- "desired_key" : "value3"
99
- }
100
- }
101
- }
102
- ```
103
-
104
- Output:
105
- ```json
106
- "value1"
107
- "value2"
108
- "value3"
109
- {"desired_key" : "value3"}
110
- ```
111
-
112
- #### Skip values if you'd only like to get objects and arrays
113
-
114
- ```ruby
115
- streamer.get(nesting_level:1, yield_values:false) do |object|
116
- p object
117
- end
118
- ```
119
-
120
- Input:
121
- ```json
122
- {
123
- "obj1" : {},
124
- "key" : "value"
125
- }
126
- ```
127
-
128
- Output:
129
- ```json
130
- {}
131
- ```
132
-
133
- #### EventMachine-style input (since 1.1.0)
134
-
135
- ```ruby
136
- # Get a JsonStreamer object that provides access to the parser
137
- # but does not start processing immediately
138
- streamer = Json::Streamer::JsonStreamer.new
139
-
140
- streamer.get(nesting_level:1) do |object|
141
- p object
142
- end
143
- ```
144
-
145
- Then later in your EventMachine handler:
146
-
147
- ```ruby
148
- def receive_data(data)
149
- streamer.parser << data
150
- end
151
- ```
152
-
153
- Check the unit tests for more examples ([spec/streamer_spec.rb](spec/streamer_spec.rb)).
154
-
155
- ## Feedback
156
-
157
- Any feedback is much appreciated.
158
-
159
- I can only tailor this project to fit use-cases I know about - which are usually my own ones. If you find that this might be the right direction to solve your problem too but you find that it's suboptimal or lacks features don't hesitate to contact me.
160
-
161
- Please let me know if you make use of this project so that I can prioritize further efforts.
162
-
163
- ## Development
164
-
165
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
166
-
167
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
168
-
169
- ## Contributing
170
-
171
- Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/json-streamer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
172
-
173
-
174
- ## License
175
-
176
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
1
+ # Json::Streamer
2
+
3
+ #### Ruby utility that supports JSON streaming allowing you to get data based on various criteria (key, nesting level, etc).
4
+
5
+ | Branch | Status |
6
+ | ------ | ------ |
7
+ | Release | [![Build Status](https://travis-ci.org/thisismydesign/json-streamer.svg?branch=release)](https://travis-ci.org/thisismydesign/json-streamer) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/json-streamer/badge.svg?branch=release)](https://coveralls.io/github/thisismydesign/json-streamer?branch=release) [![Gem Version](https://badge.fury.io/rb/json-streamer.svg)](https://badge.fury.io/rb/json-streamer) [![Total Downloads](http://ruby-gem-downloads-badge.herokuapp.com/json-streamer?type=total)](https://rubygems.org/gems/json-streamer) |
8
+ | Development | [![Build Status](https://travis-ci.org/thisismydesign/json-streamer.svg?branch=master)](https://travis-ci.org/thisismydesign/json-streamer) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/json-streamer/badge.svg?branch=master)](https://coveralls.io/github/thisismydesign/json-streamer?branch=master) |
9
+
10
+ *If you've tried JSON streaming with other Ruby libraries before (e.g. [JSON::Stream](https://github.com/dgraham/json-stream), [Yajl::FFI](https://github.com/dgraham/yajl-ffi)):*
11
+
12
+ This gem will basically spare you the need to define your own callbacks (i.e. implement an actual JSON parser using `start_object`, `end_object`, `key`, `value`, etc.).
13
+
14
+
15
+ *If you're new to this:*
16
+
17
+ Streaming is useful for
18
+ - big files that do not fit in the memory (or you'd rather avoid the risk)
19
+ - files read in chunks (e.g. arriving over network)
20
+ - cases where you expect some issue with the file (e.g. losing connection to source, invalid data at some point) but would like to get as much data as possible anyway
21
+
22
+ This gem is aimed at making streaming as easy and convenient as possible.
23
+
24
+ *Performance:*
25
+
26
+ The gem uses JSON::Stream's events in the background. It was chosen because it's a pure Ruby parser.
27
+ A similar implementation can be done using the ~10 times faster Yajl::FFI gem that is dependent on the native YAJL library.
28
+ I did not measure the performance of my implementation on top of these libraries.
29
+
30
+ ## Installation
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'json-streamer'
36
+ ```
37
+
38
+ And then execute:
39
+
40
+ $ bundle
41
+
42
+ Or install it yourself as:
43
+
44
+ $ gem install json-streamer
45
+
46
+ ## Usage
47
+
48
+ ```ruby
49
+ require 'json/streamer'
50
+
51
+ file_stream = File.open('data.json', 'r')
52
+
53
+ # Get a JsonStreamer object that will parse file_stream by chunks of 500
54
+ # Default chunk size in 1000
55
+ streamer = Json::Streamer::JsonStreamer.new(file_stream, 500)
56
+ ```
57
+
58
+ #### Get objects based on nesting level
59
+
60
+ ```ruby
61
+ # Level zero will give you the full JSON, first level will give you data within full JSON object, etc.
62
+ streamer.get(nesting_level:1) do |object|
63
+ p object
64
+ end
65
+ ```
66
+
67
+ Input:
68
+ ```json
69
+ {
70
+ "object1": "first_level_value",
71
+ "object2": {}
72
+ }
73
+ ```
74
+
75
+ Output:
76
+ ```json
77
+ "first_level_value"
78
+ {}
79
+ ```
80
+
81
+ #### Get data based on key
82
+
83
+ ```ruby
84
+ streamer.get(key:'desired_key') do |object|
85
+ p object
86
+ end
87
+ ```
88
+
89
+ Input:
90
+ ```json
91
+ {
92
+ "obj1" : {
93
+ "desired_key" : "value1"
94
+ },
95
+ "desired_key" : "value2",
96
+ "obj2" : {
97
+ "desired_key" : {
98
+ "desired_key" : "value3"
99
+ }
100
+ }
101
+ }
102
+ ```
103
+
104
+ Output:
105
+ ```json
106
+ "value1"
107
+ "value2"
108
+ "value3"
109
+ {"desired_key" : "value3"}
110
+ ```
111
+
112
+ #### Skip values if you'd only like to get objects and arrays
113
+
114
+ ```ruby
115
+ streamer.get(nesting_level:1, yield_values:false) do |object|
116
+ p object
117
+ end
118
+ ```
119
+
120
+ Input:
121
+ ```json
122
+ {
123
+ "obj1" : {},
124
+ "key" : "value"
125
+ }
126
+ ```
127
+
128
+ Output:
129
+ ```json
130
+ {}
131
+ ```
132
+
133
+ #### EventMachine-style input (since 1.1.0)
134
+
135
+ ```ruby
136
+ # Get a JsonStreamer object that provides access to the parser
137
+ # but does not start processing immediately
138
+ streamer = Json::Streamer::JsonStreamer.new
139
+
140
+ streamer.get(nesting_level:1) do |object|
141
+ p object
142
+ end
143
+ ```
144
+
145
+ Then later in your EventMachine handler:
146
+
147
+ ```ruby
148
+ def receive_data(data)
149
+ streamer.parser << data
150
+ end
151
+ ```
152
+
153
+ Check the unit tests for more examples ([spec/streamer_spec.rb](spec/streamer_spec.rb)).
154
+
155
+ ## Feedback
156
+
157
+ Any feedback is much appreciated.
158
+
159
+ I can only tailor this project to fit use-cases I know about - which are usually my own ones. If you find that this might be the right direction to solve your problem too but you find that it's suboptimal or lacks features don't hesitate to contact me.
160
+
161
+ Please let me know if you make use of this project so that I can prioritize further efforts.
162
+
163
+ ## Development
164
+
165
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
166
+
167
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
168
+
169
+ ## Contributing
170
+
171
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/json-streamer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
172
+
173
+
174
+ ## License
175
+
176
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,11 +1,11 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- desc "Check if source can be required locally"
7
- task :require do
8
- sh "ruby -e \"require '#{File.dirname __FILE__}/lib/json/streamer'\""
9
- end
10
-
11
- task :default => [:require, :spec]
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ desc "Check if source can be required locally"
7
+ task :require do
8
+ sh "ruby -e \"require '#{File.dirname __FILE__}/lib/json/streamer'\""
9
+ end
10
+
11
+ task :default => [:require, :spec]
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "json/streamer"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json/streamer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,33 +1,32 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'json/streamer/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "json-streamer"
8
- spec.version = Json::Streamer::VERSION
9
- spec.authors = ["thisismydesign"]
10
- spec.email = ["thisismydesign@users.noreply.github.com"]
11
-
12
- spec.summary = %q{Utility to support JSON streaming allowing you to get data based on various criteria (key, nesting level, etc)}
13
- spec.homepage = "https://github.com/thisismydesign/json-streamer"
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
- spec.bindir = "exe"
18
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
- spec.require_paths = ["lib"]
20
-
21
- # Because of `require_relative`
22
- spec.required_ruby_version = '>= 1.9.2'
23
-
24
- spec.add_development_dependency "bundler", "~> 1.14"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "rspec", "~> 3.0"
27
- spec.add_development_dependency "coveralls"
28
- spec.add_development_dependency "ndhash"
29
- spec.add_development_dependency "sys-proctable"
30
- spec.add_development_dependency "get_process_mem"
31
-
32
- spec.add_dependency "json-stream"
33
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json/streamer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json-streamer"
8
+ spec.version = Json::Streamer::VERSION
9
+ spec.authors = ["thisismydesign"]
10
+ spec.email = ["thisismydesign@users.noreply.github.com"]
11
+
12
+ spec.summary = %q{Utility to support JSON streaming allowing you to get data based on various criteria (key, nesting level, etc)}
13
+ spec.homepage = "https://github.com/thisismydesign/json-streamer"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ # Because of `require_relative`
22
+ spec.required_ruby_version = '>= 1.9.2'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "coveralls"
28
+ spec.add_development_dependency "ndhash"
29
+ spec.add_development_dependency "get_process_mem"
30
+
31
+ spec.add_dependency "json-stream"
32
+ end
data/lib/json/streamer.rb CHANGED
@@ -1,7 +1,7 @@
1
- require_relative "streamer/json_streamer"
2
-
3
- module Json
4
- module Streamer
5
-
6
- end
7
- end
1
+ require_relative "streamer/json_streamer"
2
+
3
+ module Json
4
+ module Streamer
5
+
6
+ end
7
+ end
@@ -1,113 +1,112 @@
1
- require "json/stream"
2
-
3
- module Json
4
- module Streamer
5
- class JsonStreamer
6
-
7
- attr_reader :aggregator
8
- attr_reader :parser
9
-
10
- def initialize(file_io = nil, chunk_size = 1000)
11
- @parser = JSON::Stream::Parser.new
12
-
13
- @file_io = file_io
14
- @chunk_size = chunk_size
15
-
16
- @current_nesting_level = -1
17
- @current_key = nil
18
- @aggregator = {}
19
- @temp_aggregator_keys = {}
20
-
21
- @parser.start_object {start_object}
22
- @parser.start_array {start_array}
23
- @parser.key {|k| key(k)}
24
- end
25
-
26
- # Callbacks containing `yield` have to be defined in the method called via block otherwise yield won't work
27
- def get(nesting_level:-1, key:nil, yield_values:true)
28
- yield_nesting_level = nesting_level
29
- wanted_key = key
30
-
31
- @parser.value do |v|
32
- if @aggregator[@current_nesting_level].kind_of? Array
33
- if yield_values and yield_value?(yield_nesting_level)
34
- yield v
35
- else
36
- @aggregator[@current_nesting_level] << v
37
- end
38
- else
39
- @aggregator[@current_nesting_level][@current_key] = v
40
- if yield_values and yield_value?(yield_nesting_level, wanted_key)
41
- yield v
42
- end
43
- end
44
- end
45
-
46
- @parser.end_object do
47
- if yield_object?(yield_nesting_level, wanted_key)
48
- yield @aggregator[@current_nesting_level].clone
49
- @aggregator[@current_nesting_level] = {}
50
- else
51
- merge_up
52
- end
53
-
54
- @current_nesting_level -= 1
55
- end
56
-
57
- @parser.end_array do
58
- if yield_object?(yield_nesting_level, wanted_key)
59
- yield @aggregator[@current_nesting_level].clone
60
- @aggregator[@current_nesting_level] = []
61
- else
62
- merge_up
63
- end
64
-
65
- @current_nesting_level -= 1
66
- end
67
-
68
- if @file_io
69
- @file_io.each(@chunk_size) do |chunk|
70
- @parser << chunk
71
- end
72
- end
73
- end
74
-
75
- def yield_object?(yield_nesting_level, wanted_key)
76
- @current_nesting_level.eql? yield_nesting_level or (not wanted_key.nil? and wanted_key == @temp_aggregator_keys[@current_nesting_level-1])
77
- end
78
-
79
- def yield_value?(yield_nesting_level, wanted_key = nil)
80
- (@current_nesting_level + 1).eql? yield_nesting_level or (not wanted_key.nil? and wanted_key == @current_key)
81
- end
82
-
83
- def start_object
84
- @temp_aggregator_keys[@current_nesting_level] = @current_key
85
- @current_nesting_level += 1
86
- @aggregator[@current_nesting_level] = {}
87
- end
88
-
89
- def start_array
90
- @temp_aggregator_keys[@current_nesting_level] = @current_key
91
- @current_nesting_level += 1
92
- @aggregator[@current_nesting_level] = []
93
- end
94
-
95
- def key(k)
96
- @current_key = k
97
- end
98
-
99
- def merge_up
100
- return if @current_nesting_level == 0
101
- previous_nesting_level = @current_nesting_level - 1
102
- if @aggregator[previous_nesting_level].kind_of? Array
103
- @aggregator[previous_nesting_level] << @aggregator[@current_nesting_level]
104
- else
105
- @aggregator[previous_nesting_level][@temp_aggregator_keys[previous_nesting_level]] = @aggregator[@current_nesting_level]
106
- end
107
-
108
- @aggregator.delete(@current_nesting_level)
109
- @aggregator
110
- end
111
- end
112
- end
113
- end
1
+ require "json/stream"
2
+
3
+ module Json
4
+ module Streamer
5
+ class JsonStreamer
6
+
7
+ attr_reader :aggregator
8
+ attr_reader :parser
9
+
10
+ def initialize(file_io = nil, chunk_size = 1000)
11
+ @parser = JSON::Stream::Parser.new
12
+
13
+ @file_io = file_io
14
+ @chunk_size = chunk_size
15
+
16
+ @current_nesting_level = -1
17
+ @current_key = nil
18
+ @aggregator = {}
19
+ @temp_aggregator_keys = {}
20
+
21
+ @parser.start_object {start_object}
22
+ @parser.start_array {start_array}
23
+ @parser.key {|k| key(k)}
24
+ end
25
+
26
+ # Callbacks containing `yield` have to be defined in the method called via block otherwise yield won't work
27
+ def get(nesting_level:-1, key:nil, yield_values:true)
28
+ yield_nesting_level = nesting_level
29
+ wanted_key = key
30
+
31
+ @parser.value do |v|
32
+ if @aggregator[@current_nesting_level].kind_of? Array
33
+ if yield_values and yield_value?(yield_nesting_level)
34
+ yield v
35
+ else
36
+ @aggregator[@current_nesting_level] << v
37
+ end
38
+ else
39
+ @aggregator[@current_nesting_level][@current_key] = v
40
+ if yield_values and yield_value?(yield_nesting_level, wanted_key)
41
+ yield v
42
+ end
43
+ end
44
+ end
45
+
46
+ @parser.end_object do
47
+ if yield_object?(yield_nesting_level, wanted_key)
48
+ yield @aggregator[@current_nesting_level].clone
49
+ @aggregator[@current_nesting_level] = {}
50
+ else
51
+ merge_up
52
+ end
53
+
54
+ @current_nesting_level -= 1
55
+ end
56
+
57
+ @parser.end_array do
58
+ if yield_object?(yield_nesting_level, wanted_key)
59
+ yield @aggregator[@current_nesting_level].clone
60
+ @aggregator[@current_nesting_level] = []
61
+ else
62
+ merge_up
63
+ end
64
+
65
+ @current_nesting_level -= 1
66
+ end
67
+
68
+ if @file_io
69
+ @file_io.each(@chunk_size) do |chunk|
70
+ @parser << chunk
71
+ end
72
+ end
73
+ end
74
+
75
+ def yield_object?(yield_nesting_level, wanted_key)
76
+ @current_nesting_level.eql? yield_nesting_level or (not wanted_key.nil? and wanted_key == @temp_aggregator_keys[@current_nesting_level-1])
77
+ end
78
+
79
+ def yield_value?(yield_nesting_level, wanted_key = nil)
80
+ (@current_nesting_level + 1).eql? yield_nesting_level or (not wanted_key.nil? and wanted_key == @current_key)
81
+ end
82
+
83
+ def start_object
84
+ @temp_aggregator_keys[@current_nesting_level] = @current_key
85
+ @current_nesting_level += 1
86
+ @aggregator[@current_nesting_level] = {}
87
+ end
88
+
89
+ def start_array
90
+ @current_nesting_level += 1
91
+ @aggregator[@current_nesting_level] = []
92
+ end
93
+
94
+ def key(k)
95
+ @current_key = k
96
+ end
97
+
98
+ def merge_up
99
+ return if @current_nesting_level == 0
100
+ previous_nesting_level = @current_nesting_level - 1
101
+ if @aggregator[previous_nesting_level].kind_of? Array
102
+ @aggregator[previous_nesting_level] << @aggregator[@current_nesting_level]
103
+ else
104
+ @aggregator[previous_nesting_level][@temp_aggregator_keys[previous_nesting_level]] = @aggregator[@current_nesting_level]
105
+ end
106
+
107
+ @aggregator.delete(@current_nesting_level)
108
+ @aggregator
109
+ end
110
+ end
111
+ end
112
+ end
@@ -1,5 +1,5 @@
1
- module Json
2
- module Streamer
3
- VERSION = "1.1.0"
4
- end
5
- end
1
+ module Json
2
+ module Streamer
3
+ VERSION = "1.1.1"
4
+ end
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-streamer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thisismydesign
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-29 00:00:00.000000000 Z
11
+ date: 2017-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: sys-proctable
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: get_process_mem
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -164,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
150
  version: '0'
165
151
  requirements: []
166
152
  rubyforge_project:
167
- rubygems_version: 2.5.2
153
+ rubygems_version: 2.6.11
168
154
  signing_key:
169
155
  specification_version: 4
170
156
  summary: Utility to support JSON streaming allowing you to get data based on various