Sha256: ef8f6db1e758777dca192a14430ba1dc96558c3097b2a25725585ef10a3b2b48

Contents?: true

Size: 1.63 KB

Versions: 61

Compression:

Stored size: 1.63 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 = []
        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 to_s
        path.join
      end
    end
  end
end

Version data entries

61 entries across 61 versions & 2 rubygems

Version Path
pact-support-1.3.0 lib/pact/matching_rules/jsonpath.rb