Skip to content

Instantly share code, notes, and snippets.

@zernel
Forked from chancancode/0.rb
Created March 1, 2013 16:43
Show Gist options
  • Save zernel/5065942 to your computer and use it in GitHub Desktop.
Save zernel/5065942 to your computer and use it in GitHub Desktop.
mbp:godfrey json_expressions [master] $ irb -Ilib
>> require 'json_expressions/matcher'
=> true
>> m = JsonExpressions::Matcher.new({a: :capture_me})
=> {:a=>:capture_me}
>> require 'json'
=> true
>> m =~ JSON.parse('{"a": 123}')
=> true
>> m.captures
=> {:capture_me=>123}
# For RSpec...
response.body # => '{"a": 123}'
matcher = JsonExpressions::Matcher.new({a: :capture_me})
response.body.should match_json_expression(matcher)
# If we reaches here it means the response matched our pattern, so we can use the captured value
matcher.captures[:capture_me] # => 123
# Ideally, I'd like to be able to just do this, but I wouldn't know how to make it work. Pull requests welcomed
matcher = response.body.should match_json_expression({a: :capture_me})
matcher.captures[:capture_me] # => 123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment