Module: Airborne::PathMatcher

Included in:
RequestExpectations
Defined in:
lib/airborne/path_matcher.rb

Instance Method Summary collapse

Instance Method Details

#get_by_path(path, json, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/airborne/path_matcher.rb', line 5

def get_by_path(path, json, &block)
  fail PathError, "Invalid Path, contains '..'" if /\.\./ =~ path
  type = false
  parts = path.split('.')
  parts.each_with_index do |part, index|
    if part == '*' || part == '?'
      ensure_array(path, json)
      type = part
      if index < parts.length.pred
        walk_with_path(type, index, path, parts, json, &block) && return
      end
      next
    end
    begin
      json = process_json(part, json)
    rescue
      raise PathError, "Expected #{json.class}\nto be an object with property #{part}"
    end
  end
  if type == '*'
    expect_all(json, &block)
  elsif type == '?'
    expect_one(path, json, &block)
  else
    yield json
  end
end