Class: JQ::Parser
- Inherits:
-
Object
- Object
- JQ::Parser
- Defined in:
- lib/jq/parser.rb
Constant Summary collapse
- BUFSIZ =
4096
Instance Method Summary collapse
-
#initialize(src, parse_json: true) ⇒ Parser
constructor
A new instance of Parser.
- #search(program, &block) ⇒ Object
Constructor Details
#initialize(src, parse_json: true) ⇒ Parser
Returns a new instance of Parser.
7 8 9 10 |
# File 'lib/jq/parser.rb', line 7 def initialize(src, parse_json: true) @src = kind_of_io?(src) ? src : src.to_s = { parse_json: parse_json } end |
Instance Method Details
#search(program, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/jq/parser.rb', line 12 def search(program, &block) @src.rewind if kind_of_io?(@src) retval = nil if block block_orig = block block = proc do |str| block_orig.call(parse_json(str)) end else retval = [] block = proc do |str| retval << parse_json(str) end end jq(program) do |jq_core| if kind_of_io?(@src) while (buf = @src.read(BUFSIZ)) jq_core.update(buf, !@src.eof?, &block) end else jq_core.update(@src, false, &block) end end retval end |