lib/airborne/path_matcher.rb in airborne-0.1.15 vs lib/airborne/path_matcher.rb in airborne-0.1.16

- old
+ new

@@ -1,27 +1,26 @@ module Airborne class PathError < StandardError; end - module PathMatcher + module PathMatcher def get_by_path(path, json, &block) - raise PathError, "Invalid Path, contains '..'" if /\.\./ =~ path + 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 + 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 to be an object with property #{part}" + raise PathError, "Expected #{json.class}\nto be an object with property #{part}" end end if type == '*' expect_all(json, &block) elsif type == '?' @@ -49,20 +48,20 @@ ensure_match_one(path, item_count, error_count) if type == '?' end end def process_json(part, json) - if is_index?(part) && json.is_a?(Array) + if index?(part) && json.is_a?(Array) part = part.to_i json = json[part] else json = json[part.to_sym] end json end - def is_index?(part) + def index?(part) part =~ /^\d+$/ end def expect_one(path, json, &block) item_count = json.length @@ -78,26 +77,25 @@ end def expect_all(json, &block) last_error = nil begin - json.each{|part| yield part} + 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) - raise RSpec::Expectations::ExpectationNotMetError, "Expected one object in path #{path} to match provided JSON values" if 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) - raise error unless error.nil? + fail error unless error.nil? end def ensure_array(path, json) - raise RSpec::Expectations::ExpectationNotMetError, "Expected #{path} to be array got #{json.class} from JSON response" unless json.class == Array + fail RSpec::Expectations::ExpectationNotMetError, "Expected #{path} to be array got #{json.class} from JSON response" unless json.class == Array end - end end