Last active
August 29, 2015 14:07
-
-
Save epitron/fda414da483e38ad3533 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# examples | |
spec = { | |
"apiKey" => String, | |
"language" => ["en", "ru"], | |
"clientData" => { | |
"userName" => String, | |
"userCity" => String, | |
"userPhoneNo" => String, | |
"userPassword" => String | |
} | |
} | |
example = { | |
"apiKey" => "api key", | |
"language" => "en", | |
"clientData" => { | |
"userName" => "dr_toboggan", | |
"userCity" => "Philly", | |
"userPhoneNo" => "phone number here", | |
"userPassword" => "you got the virus" | |
} | |
} | |
example2 = { | |
"apiKey" => "api key", | |
"language" => "fr", | |
"clientData" => { | |
"userName" => "dr_toboggan", | |
"userCity" => "Philly", | |
"userPhoneNo" => "phone number here", | |
"userPassword" => "you got the virus" | |
} | |
} | |
def match(spec, example) | |
return false unless spec.keys == example.keys | |
spec.each do |key, val| | |
case val | |
when Hash | |
if example[key].is_a? Hash | |
return false unless match(val, example[key]) | |
else | |
return false | |
end | |
when Array | |
# alternation | |
return false unless val.include?(example[key]) | |
else | |
return false unless example[key].is_a?(val) | |
end | |
end | |
true | |
end | |
p match(spec, example) # => true | |
p match(spec, example2) # => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment