Class: Pact::MatchingRules::JsonPath

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/matching_rules/jsonpath.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ JsonPath

Returns a new instance of JsonPath.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pact/matching_rules/jsonpath.rb', line 13

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/pact/matching_rules/jsonpath.rb', line 11

def path
  @path
end

Instance Method Details

#to_sObject



53
54
55
# File 'lib/pact/matching_rules/jsonpath.rb', line 53

def to_s
  path.join
end