kor-input-json 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 +4 -4
- data/README.md +5 -1
- data/Rakefile +1 -1
- data/lib/kor/input/json.rb +27 -0
- data/lib/kor/input/json/version.rb +1 -1
- data/lib/kor/input/json_test.rb +12 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb56b1676cdf5ff296c4d829212e53dbf294d4c8
|
4
|
+
data.tar.gz: 6ce74715ecef7a839bd60d3c76f2f6eb2ccd570c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1540ea0571c04956d2f99f335d92fe7efcf9819f36341cbf562c7ed8194abb4c96cf838c036cfd92161153adba709df09cf7becea5a338c8189f68aa342510b8
|
7
|
+
data.tar.gz: f21a63b19f9908cca7a6ccd741c3c686423c11fa7b9f62b9924e87904100fd65b46629c12673dfaabc7b6ece2f990a7f767c4f391b40f61a2d797146cf19fdea
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@ kor-input-json
|
|
3
3
|
|
4
4
|
[](https://travis-ci.org/ksss/kor-input-json)
|
5
5
|
|
6
|
-
JSON input plugin for kor.
|
6
|
+
JSON input plugin for [kor](https://github.com/ksss/kor).
|
7
7
|
|
8
8
|
# Usage
|
9
9
|
|
@@ -51,3 +51,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/ksss/k
|
|
51
51
|
## License
|
52
52
|
|
53
53
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
54
|
+
|
55
|
+
## Refs
|
56
|
+
|
57
|
+
- https://github.com/ksss/kor
|
data/Rakefile
CHANGED
data/lib/kor/input/json.rb
CHANGED
@@ -3,11 +3,15 @@ require 'json'
|
|
3
3
|
module Kor
|
4
4
|
module Input
|
5
5
|
class Json
|
6
|
+
DEFAULT_GUESS_TIME = 5
|
7
|
+
|
6
8
|
def initialize(io)
|
7
9
|
@io = io
|
8
10
|
@single = false
|
9
11
|
@keys = []
|
10
12
|
@jsons = []
|
13
|
+
@count = 0
|
14
|
+
@guess_time = DEFAULT_GUESS_TIME
|
11
15
|
@fiber = Fiber.new do
|
12
16
|
@jsons.each do |json|
|
13
17
|
Fiber.yield @keys.map{ |k| json[k] }
|
@@ -21,6 +25,9 @@ module Kor
|
|
21
25
|
opt.on("--single", "parse single JSON") do |arg|
|
22
26
|
@single = true
|
23
27
|
end
|
28
|
+
opt.on("--guess-time=NUM", "load lines this time for guess. no guess if under 0 (default #{DEFAULT_GUESS_TIME})") do |arg|
|
29
|
+
@guess_time = arg.to_i
|
30
|
+
end
|
24
31
|
end
|
25
32
|
|
26
33
|
def head
|
@@ -30,6 +37,9 @@ module Kor
|
|
30
37
|
while line = @io.gets
|
31
38
|
line.strip!
|
32
39
|
@jsons << JSON.parse(line)
|
40
|
+
if 0 < @guess_time && @guess_time <= @jsons.length
|
41
|
+
break
|
42
|
+
end
|
33
43
|
end
|
34
44
|
end
|
35
45
|
@keys = @jsons.map do |json|
|
@@ -40,6 +50,23 @@ module Kor
|
|
40
50
|
end
|
41
51
|
|
42
52
|
def gets
|
53
|
+
if @guess_time <= 0 || @count < @guess_time
|
54
|
+
@count += 1
|
55
|
+
return resume
|
56
|
+
end
|
57
|
+
|
58
|
+
if line = @io.gets
|
59
|
+
line.strip!
|
60
|
+
json = JSON.parse(line)
|
61
|
+
@keys.map { |k| json[k] }
|
62
|
+
else
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def resume
|
43
70
|
@fiber.resume
|
44
71
|
rescue FiberError
|
45
72
|
nil
|
data/lib/kor/input/json_test.rb
CHANGED
@@ -29,6 +29,17 @@ JSON
|
|
29
29
|
opt.parse(["--single"])
|
30
30
|
json
|
31
31
|
},
|
32
|
+
-> {
|
33
|
+
io = StringIO.new(<<-JSON)
|
34
|
+
{"foo": 100, "bar": 200}
|
35
|
+
{"bar": 500, "baz": 600}
|
36
|
+
JSON
|
37
|
+
json = Kor::Input::Json.new(io)
|
38
|
+
opt = OptionParser.new
|
39
|
+
json.parse(opt)
|
40
|
+
opt.parse(["--guess-time=0"])
|
41
|
+
json
|
42
|
+
},
|
32
43
|
]
|
33
44
|
|
34
45
|
def test_head(t)
|
@@ -59,7 +70,7 @@ JSON
|
|
59
70
|
t.error("expect #{expect} got #{body}")
|
60
71
|
end
|
61
72
|
|
62
|
-
|
73
|
+
5.times do
|
63
74
|
body = json.gets
|
64
75
|
if body != nil
|
65
76
|
t.error("expect nil got #{body}")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kor-input-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kor
|