Sha256: debc1c41beac2ef0f8fea18b9486ed12bc316911d71f0360c73aa1b22a7cd89d

Contents?: true

Size: 1.66 KB

Versions: 21

Compression:

Stored size: 1.66 KB

Contents

require 'strscan'

# Ripped with appreciation from Joshua Hull's useful JsonPath gem
# https://github.com/joshbuddy/jsonpath/blob/792ff9a928998f4252692cd3c1ba378ed931a5aa/lib/jsonpath.rb
# Only including the code that Pact needs, to reduce dependencies and potential gem version clashes.

module Pact
  module MatchingRules
    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-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

        def to_s
          path.join
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
pact-support-1.2.99.alpha.1 lib/pact/matching_rules/jsonpath.rb
pact-support-1.3.0.alpha.2 lib/pact/matching_rules/jsonpath.rb
pact-support-1.3.0.alpha.1 lib/pact/matching_rules/jsonpath.rb
pact-support-1.2.5 lib/pact/matching_rules/jsonpath.rb
pact-support-1.2.4 lib/pact/matching_rules/jsonpath.rb
pact-support-1.2.3 lib/pact/matching_rules/jsonpath.rb
pact-support-1.2.2 lib/pact/matching_rules/jsonpath.rb
pact-support-1.2.1 lib/pact/matching_rules/jsonpath.rb
pact-support-1.2.0 lib/pact/matching_rules/jsonpath.rb
pact-support-1.1.8 lib/pact/matching_rules/jsonpath.rb
pact-support-1.1.7 lib/pact/matching_rules/jsonpath.rb
pact-support-1.1.6 lib/pact/matching_rules/jsonpath.rb
pact-support-1.1.5 lib/pact/matching_rules/jsonpath.rb
pact-support-1.1.4 lib/pact/matching_rules/jsonpath.rb
pact-support-1.1.3 lib/pact/matching_rules/jsonpath.rb
pact-support-1.1.2 lib/pact/matching_rules/jsonpath.rb
pact-support-1.1.1 lib/pact/matching_rules/jsonpath.rb
pact-support-1.1.0 lib/pact/matching_rules/jsonpath.rb
pact-support-1.0.1 lib/pact/matching_rules/jsonpath.rb
pact-support-1.0.0 lib/pact/matching_rules/jsonpath.rb