Skip to content

Instantly share code, notes, and snippets.

@epitron
Last active August 29, 2015 14:07
Show Gist options
  • Save epitron/fda414da483e38ad3533 to your computer and use it in GitHub Desktop.
Save epitron/fda414da483e38ad3533 to your computer and use it in GitHub Desktop.
# 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