json-sequence 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/json-sequence.gemspec +30 -0
- data/lib/json_sequence.rb +1 -0
- data/lib/json_sequence/parser.rb +61 -0
- data/lib/json_sequence/result.rb +42 -0
- data/lib/json_sequence/version.rb +3 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0cb72c464c78dda06761851bfacc5b734bfd596ee58f30342253fcd7ea04d5d5
|
4
|
+
data.tar.gz: a6d0bbbd1c974b52bce2013379bfe027c0d43a11babc606b69f58b4ce270efa4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb8d028614c4ca5a04d54c2dfa613974a3e6c1faaee2decaa735b104c21fc6776cb1eb3c817f0595a746521b58b03ff28b96cdb67472b5f47677f8023365113f
|
7
|
+
data.tar.gz: b4142272f541834a372a0d84aea77fae6bd87d9270863bd437d4bfb47debe4e99255b357376e03f32550f076ff32babc4ad765aa18fbdc1d752ee86bb0b98af6
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
json-sequence (0.1.2)
|
5
|
+
multi_json (~> 1.13)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.3)
|
11
|
+
json (2.1.0)
|
12
|
+
json (2.1.0-java)
|
13
|
+
multi_json (1.13.1)
|
14
|
+
rake (10.5.0)
|
15
|
+
rspec (3.7.0)
|
16
|
+
rspec-core (~> 3.7.0)
|
17
|
+
rspec-expectations (~> 3.7.0)
|
18
|
+
rspec-mocks (~> 3.7.0)
|
19
|
+
rspec-core (3.7.1)
|
20
|
+
rspec-support (~> 3.7.0)
|
21
|
+
rspec-expectations (3.7.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.7.0)
|
24
|
+
rspec-mocks (3.7.0)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.7.0)
|
27
|
+
rspec-support (3.7.1)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
java
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
bundler (~> 1.16)
|
35
|
+
json (~> 2.0)
|
36
|
+
json-sequence!
|
37
|
+
rake (~> 10.0)
|
38
|
+
rspec (~> 3.0)
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
1.16.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 GreenSync
|
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
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# JsonSequence
|
2
|
+
|
3
|
+
A push parser for [RFC7464 JSON Text Sequences](https://tools.ietf.org/html/rfc7464)
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
The parser is intended for use in scenarios where data is being streamed in
|
8
|
+
chunks, either from the file system or from the network. As each new chuck of
|
9
|
+
data is received it is pushed to the parser, which will yield the parsed values
|
10
|
+
contained within.
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
require 'net/http'
|
14
|
+
require 'json_sequence'
|
15
|
+
|
16
|
+
uri = URI('http://example.com/json_sequence')
|
17
|
+
parser = JsonSequence::Parser.new
|
18
|
+
|
19
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
20
|
+
request = Net::HTTP::Get.new uri
|
21
|
+
|
22
|
+
http.request request do |response|
|
23
|
+
response.value # raises if not success
|
24
|
+
|
25
|
+
response.read_body do |chunk|
|
26
|
+
parser.parse(chunk) do |result|
|
27
|
+
case result
|
28
|
+
when JsonSequence::Result::Json
|
29
|
+
puts "received record: #{result.value.inspect}"
|
30
|
+
when JsonSequence::Result::MaybeTruncated
|
31
|
+
puts "received possibly truncated record: #{result.value.inspect}"
|
32
|
+
when JsonSequence::Result::ParseError
|
33
|
+
puts "stream contained invalid record: #{result.record}, caused by: #{result.error.message}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Development
|
42
|
+
|
43
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
44
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
45
|
+
prompt that will allow you to experiment.
|
46
|
+
|
47
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
48
|
+
release a new version, update the version number in `version.rb`, and then run
|
49
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
50
|
+
git commits and tags, and push the `.gem` file to
|
51
|
+
[rubygems.org](https://rubygems.org).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "json/sequence"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "json_sequence/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "json-sequence"
|
8
|
+
spec.version = JsonSequence::VERSION
|
9
|
+
spec.authors = ["Wesley Moore"]
|
10
|
+
spec.email = ["wes.moore@greensync.com.au"]
|
11
|
+
|
12
|
+
spec.summary = %q{Push parser for RFC7464 JSON Sequences}
|
13
|
+
spec.homepage = "https://github.com/greensync/json-sequence"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
17
|
+
Dir.glob("{bin,lib}/**/*") + %w[README.md Gemfile Gemfile.lock Rakefile LICENSE.txt json-sequence.gemspec]
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "multi_json", "~> 1.13"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "json", "~> 2.0"
|
30
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'json_sequence/parser'
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'json_sequence/result'
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
module JsonSequence
|
5
|
+
class Parser
|
6
|
+
RS = "\x1E".freeze
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@buffer = ''
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse(chunk, &block)
|
13
|
+
@buffer = do_parse(@buffer + chunk, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
# Takes a String buffer to parse and returns String containing any
|
19
|
+
# text remaining to parse when more data is available.
|
20
|
+
def do_parse(buffer)
|
21
|
+
records = buffer.split(RS, -1) # -1 stops suppression of trailing null fields
|
22
|
+
|
23
|
+
records.each_with_index do |record, i|
|
24
|
+
# RFC7464 2.1 Multiple consecutive RS octets do not denote empty
|
25
|
+
# sequence elements between them and can be ignored.
|
26
|
+
next if record == ''
|
27
|
+
|
28
|
+
# Try to decode the record
|
29
|
+
begin
|
30
|
+
value = MultiJson.load(record)
|
31
|
+
result, remaining = handle_parsed(record, value, is_last_record: i == records.size - 1)
|
32
|
+
rescue MultiJson::ParseError => err
|
33
|
+
result, remaining = handle_err(record, err, is_last_record: i == records.size - 1)
|
34
|
+
end
|
35
|
+
|
36
|
+
return remaining if result.nil?
|
37
|
+
yield result
|
38
|
+
end
|
39
|
+
|
40
|
+
''
|
41
|
+
end
|
42
|
+
|
43
|
+
def handle_parsed(record, value, is_last_record:)
|
44
|
+
case value
|
45
|
+
when Numeric, TrueClass, FalseClass, NilClass
|
46
|
+
# Check for truncation, if record was parsed but doesn't end in
|
47
|
+
# whitespace it may be truncated
|
48
|
+
if record !~ /\s$/
|
49
|
+
return is_last_record ? [nil, record] : [JsonSequence::Result::MaybeTruncated.new(value), '']
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
[JsonSequence::Result::Json.new(value), '']
|
54
|
+
end
|
55
|
+
|
56
|
+
def handle_err(record, err, is_last_record:)
|
57
|
+
# Last record, might be incomplete, stash for later
|
58
|
+
is_last_record ? [nil, record] : [JsonSequence::Result::ParseError.new(record, err), '']
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module JsonSequence
|
2
|
+
module Result
|
3
|
+
class Json
|
4
|
+
attr_reader :value
|
5
|
+
|
6
|
+
def initialize(value)
|
7
|
+
@value = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def ==(other)
|
11
|
+
return false unless other.is_a?(self.class)
|
12
|
+
other.value == value
|
13
|
+
end
|
14
|
+
|
15
|
+
alias eql? ==
|
16
|
+
end
|
17
|
+
|
18
|
+
class ParseError
|
19
|
+
attr_reader :record, :error
|
20
|
+
|
21
|
+
def initialize(record, error)
|
22
|
+
@record = record
|
23
|
+
@error = error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class MaybeTruncated
|
28
|
+
attr_reader :value
|
29
|
+
|
30
|
+
def initialize(value)
|
31
|
+
@value = value
|
32
|
+
end
|
33
|
+
|
34
|
+
def ==(other)
|
35
|
+
return false unless other.is_a?(self.class)
|
36
|
+
other.value == value
|
37
|
+
end
|
38
|
+
|
39
|
+
alias eql? ==
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json-sequence
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wesley Moore
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: multi_json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: json
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- wes.moore@greensync.com.au
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- bin/console
|
96
|
+
- bin/setup
|
97
|
+
- json-sequence.gemspec
|
98
|
+
- lib/json_sequence.rb
|
99
|
+
- lib/json_sequence/parser.rb
|
100
|
+
- lib/json_sequence/result.rb
|
101
|
+
- lib/json_sequence/version.rb
|
102
|
+
homepage: https://github.com/greensync/json-sequence
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.7.6
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Push parser for RFC7464 JSON Sequences
|
126
|
+
test_files: []
|