114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/spaceborne.rb', line 114
def get_by_path(path, json, &block)
fail PathError, "Invalid Path, contains '..'" if /\.\./ =~ path
type = false
if path.class == Symbol
parts = path.to_s.split('.')
else
parts = path.split('.')
end
parts.each_with_index do |part, index|
if part == '*' || part == '?'
ensure_array_or_hash(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 == '*'
case json.class.name
when 'Array'
expect_all(json, &block)
when 'Hash'
json.each do |k,v|
yield json[k]
end
end
elsif type == '?'
expect_one(path, json, &block)
else
yield json
end
end
|