Sha256: 82a6831dd959251727fd3eba5486be190f64d7bbe020d0a12fea544adba9075b

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

require 'strscan'
require 'jsonpath/enumerable'
require 'jsonpath/version'

class JsonPath

  attr_reader :path

  def initialize(path)
    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-Z_]+/)
        @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(/\./)
      elsif token = scanner.scan(/\*/)
        @path << token
      elsif token = scanner.scan(/./)
        @path.last << token
      end
    end
  end

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

  def first(object)
    enum_on(object).first
  end

  def enum_on(object)
    JsonPath::Enumerable.new(self, object)
  end

  def self.on(object, path)
    self.new(path).on(object)
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jsonpath-0.3.2 lib/jsonpath.rb
jsonpath-0.3.1 lib/jsonpath.rb