Sha256: a1991fa4159627a5d2fc78a36e347e782c7d3182d254faac8f45c8527581610a

Contents?: true

Size: 1.66 KB

Versions: 8

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

8 entries across 8 versions & 1 rubygems

Version Path
pact-support-0.6.0 lib/pact/matching_rules/jsonpath.rb
pact-support-0.5.9 lib/pact/matching_rules/jsonpath.rb
pact-support-0.5.8 lib/pact/matching_rules/jsonpath.rb
pact-support-0.5.7 lib/pact/matching_rules/jsonpath.rb
pact-support-0.5.5 lib/pact/matching_rules/jsonpath.rb
pact-support-0.5.4 lib/pact/matching_rules/jsonpath.rb
pact-support-0.5.3 lib/pact/matching_rules/jsonpath.rb
pact-support-0.5.1 lib/pact/matching_rules/jsonpath.rb