Sha256: 39aca354c2a4df309da1c3c9992da9cbb70e61be8aad0b23ca1e52178b6f0c2c

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

require 'strscan'
require 'multi_json'
require 'jsonpath/proxy'
require 'jsonpath/enumerable'
require 'jsonpath/version'

class JsonPath

  PATH_ALL = '$..*'

  attr_reader :path

  def initialize(path, opts = nil)
    @opts = opts
    scanner = StringScanner.new(path)
    @path = []
    bracket_count = 0
    while not scanner.eos?
      if token = scanner.scan(/\$/)
        @path << token
      elsif token = scanner.scan(/@/)
        @path << token
      elsif token = scanner.scan(/[a-zA-Z0-9_-]+/)
        @path << "['#{token}']"
      elsif token = scanner.scan(/'(.*?)'/)
        @path << "[#{token}]"
      elsif token = scanner.scan(/\[/)
        count = 1
        while !count.zero?
          if t = scanner.scan(/\[/)
            token << t
            count += 1
          elsif t = scanner.scan(/\]/)
            token << t
            count -= 1
          elsif t = scanner.scan(/[^\[\]]*/)
            token << t
          end
        end
        @path << token
      elsif token = scanner.scan(/\.\./)
        @path << token
      elsif scanner.scan(/\./)
        nil
      elsif token = scanner.scan(/\*/)
        @path << token
      elsif token = scanner.scan(/[><=] \d+/)
        @path.last << token
      elsif token = scanner.scan(/./)
        @path.last << token
      end
    end
  end

  def on(obj_or_str)
    enum_on(obj_or_str).to_a
  end

  def first(obj_or_str, *args)
    enum_on(obj_or_str).first(*args)
  end

  def enum_on(obj_or_str, mode = nil)
    JsonPath::Enumerable.new(self, self.class.process_object(obj_or_str), mode, @opts)
  end
  alias_method :[], :enum_on

  def self.on(obj_or_str, path, opts = nil)
    self.new(path, opts).on(process_object(obj_or_str))
  end

  def self.for(obj_or_str)
    Proxy.new(process_object(obj_or_str))
  end

  private
  def self.process_object(obj_or_str)
    obj_or_str.is_a?(String) ? MultiJson.decode(obj_or_str) : obj_or_str
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jsonpath-0.5.6 lib/jsonpath.rb
jsonpath-0.5.5 lib/jsonpath.rb
jsonpath-0.5.4 lib/jsonpath.rb
jsonpath-0.5.3 lib/jsonpath.rb
jsonpath-0.5.2 lib/jsonpath.rb