Sha256: 9e4c6a61d2146d391a7aa27d9fc981056f9843cfa81099a19836b4d143eba643
Contents?: true
Size: 1.19 KB
Versions: 4
Compression:
Stored size: 1.19 KB
Contents
require 'jq_core' require 'json' require 'tempfile' require 'stringio' class JQ::Parser BUFSIZ = 4096 def initialize(src, options = {}) @src = kind_of_io?(src) ? src : src.to_s @options = { :parse_json => true, }.merge(options) end 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 return retval end private def jq(program) jq_core = nil begin jq_core = JQ::Core.new(program) retval = yield(jq_core) ensure jq_core.close if jq_core end end def parse_json(str) if @options[:parse_json] JSON.parse("[#{str}]").first else str end end def kind_of_io?(obj) [IO, Tempfile, StringIO].any? {|c| obj.kind_of?(c) } end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
ruby-jq-0.1.5 | lib/jq/parser.rb |
ruby-jq-0.1.4 | lib/jq/parser.rb |
ruby-jq-0.1.3 | lib/jq/parser.rb |
ruby-jq-0.1.2 | lib/jq/parser.rb |