-
Notifications
You must be signed in to change notification settings - Fork 124
/
path_matcher.rb
101 lines (91 loc) · 2.63 KB
/
path_matcher.rb
1
2
3
4
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
module Airborne
class PathError < StandardError; end
module PathMatcher
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
private
def walk_with_path(type, index, path, parts, json, &block)
last_error = nil
item_count = json.length
error_count = 0
json.each do |element|
begin
sub_path = parts[(index.next)...(parts.length)].join('.')
get_by_path(sub_path, element, &block)
rescue Exception => e
last_error = e
error_count += 1
end
ensure_match_all(last_error) if type == '*'
ensure_match_one(path, item_count, error_count) if type == '?'
end
end
def process_json(part, json)
if index?(part) && json.is_a?(Array)
part = part.to_i
json = json[part]
else
json = json[part.to_sym]
end
json
end
def index?(part)
part =~ /^\d+$/
end
def expect_one(path, json, &block)
item_count = json.length
error_count = 0
json.each do |part|
begin
yield part
rescue Exception
error_count += 1
ensure_match_one(path, item_count, error_count)
end
end
end
def expect_all(json, &block)
last_error = nil
begin
json.each { |part| yield part }
rescue Exception => e
last_error = e
end
ensure_match_all(last_error)
end
def ensure_match_one(path, item_count, error_count)
fail RSpec::Expectations::ExpectationNotMetError, "Expected one object in path #{path} to match provided JSON values" if item_count == error_count
end
def ensure_match_all(error)
fail error unless error.nil?
end
def ensure_array(path, json)
fail RSpec::Expectations::ExpectationNotMetError, "Expected #{path} to be array got #{json.class} from JSON response" unless json.class == Array
end
end
end