kor-input-json 0.0.1 → 0.0.2

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: 6d9e432effb699b4fad0b96031a5f3f170d3f427
4
- data.tar.gz: 41732af281ecb4594aa8444e6722322ea52540c6
3
+ metadata.gz: fb56b1676cdf5ff296c4d829212e53dbf294d4c8
4
+ data.tar.gz: 6ce74715ecef7a839bd60d3c76f2f6eb2ccd570c
5
5
  SHA512:
6
- metadata.gz: 76740bb76f25d17fa39ee71fa7affaf105e0479691f0554671f102ee9edf505d317f68de1d88293db513c59cc6073bfef02f2d7d81d05a8ce907ccc6de45e826
7
- data.tar.gz: f7d71d46ff660f9f10451267954c9fa059c04409a186a4d47576a38c524dc2ec8f07d247c3ba7f2c67a5b245f97f556479bff82658700e12fc7b3dabdcb4ee9f
6
+ metadata.gz: 1540ea0571c04956d2f99f335d92fe7efcf9819f36341cbf562c7ed8194abb4c96cf838c036cfd92161153adba709df09cf7becea5a338c8189f68aa342510b8
7
+ data.tar.gz: f21a63b19f9908cca7a6ccd741c3c686423c11fa7b9f62b9924e87904100fd65b46629c12673dfaabc7b6ece2f990a7f767c4f391b40f61a2d797146cf19fdea
data/README.md CHANGED
@@ -3,7 +3,7 @@ kor-input-json
3
3
 
4
4
  [![Build Status](https://travis-ci.org/ksss/kor-input-json.svg)](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
@@ -1,7 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
3
  task :test do
4
- sh "rgot #{Dir.glob("lib/**/*_test.rb").join(' ')}"
4
+ sh "rgot -v #{Dir.glob("lib/**/*_test.rb").join(' ')}"
5
5
  end
6
6
 
7
7
  task :default => [:test]
@@ -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
@@ -1,7 +1,7 @@
1
1
  module Kor
2
2
  module Input
3
3
  class Json
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
@@ -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
- 10.times do
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.1
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-07 00:00:00.000000000 Z
11
+ date: 2015-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kor