Open robertely opened 5 years ago
I'm realizing now that I miss-understood the example. I expected it to count elements on the page when really it was checking for a key page.totalElements
So I misunderstood that but adding the behavior I expected would maybe be not so bad.
We could perhaps detect the type on the key and make some reasonable decisions:
if leaf.is_a?(Array)
if config[:value]
# For if you inexplicably wanted to pass in the string version of a ruby array on the command line
raise "unexpected value for key: '#{leaf}.to_s' != '#{config[:value]}'" unless leaf.to_s == config[:value].to_s
message += "equals '#{config[:value]}'"
end
if config[:valueGt]
raise "unexpected count for key: '#{leaf.count}' not > '#{config[:valueGt]}'" unless leaf.count > config[:valueGt].to_f
message += "greater than '#{config[:valueGt]}'"
end
if config[:valueLt]
raise "unexpected count for key: '#{leaf.count}' not < '#{config[:valueLt]}'" unless leaf.count < config[:valueLt].to_f
message += "less than '#{config[:valueLt]}'"
end
else
if config[:value]
raise "unexpected value for key: '#{leaf}' != '#{config[:value]}'" unless leaf.to_s == config[:value].to_s
message += "equals '#{config[:value]}'"
end
if config[:valueGt]
raise "unexpected value for key: '#{leaf}' not > '#{config[:valueGt]}'" unless leaf.to_f > config[:valueGt].to_f
message += "greater than '#{config[:valueGt]}'"
end
if config[:valueLt]
raise "unexpected value for key: '#{leaf}' not < '#{config[:valueLt]}'" unless leaf.to_f < config[:valueLt].to_f
message += "less than '#{config[:valueLt]}'"
end
end
Additionally we could add support for the top level element by implementing a keyword.
def deep_value(data, desired_key, parent = '')
if desired_key == '.'
return data
end
case data
when Array
data.each_with_index do |value, index|
...
Then something like this would work:
check-http-json-sg.rb -u https://jsonplaceholder.typicode.com/todos/ --key . --value-greater-than 5
If your interested I could clean that up and make a pr
Trying this example:
I don't see where
page.totalElements
is setup. It does not exist in leaf so far as i see.Command run:
Returns:
Versions: