Sha256: 8cf0e9cb1bfb320f3781a703aec6fe5834c926b184fbe9dd0683834f90ea9f50

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require 'jq_core'
require 'json'

class JQ::Parser
  BUFSIZ = 4096

  def initialize(src, options = {})
    @src = src.kind_of?(IO) ? src : src.to_s
    @options = {
      :parse_json => true,
    }.merge(options)
  end

  def search(program, &block)
    @src.rewind if @src.kind_of?(IO)
    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 @src.kind_of?(IO)
        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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-jq-0.1.1 lib/jq/parser.rb
ruby-jq-0.1.0 lib/jq/parser.rb