Created
October 18, 2022 07:14
-
-
Save kemenaran/2dcc463fdda3e476983bf5500f3524b9 to your computer and use it in GitHub Desktop.
A VCR matcher that matches a request where the JSON body is identical to another, except for some keys
This file contains 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
# Match the JSON body of the request, ignoring some specified keys. | |
# | |
# Usage: | |
# VCR.use_cassette( | |
# 'my_request', | |
# match_requests_on: [:host, :path, body_as_json_excluding(:timestamp)] | |
# ) | |
# | |
# See also: body_as_json | |
def body_as_json_excluding(*keys) | |
lambda do |r1, r2| | |
begin | |
keys = keys.map(&:to_s) | |
JSON.parse(r1.body).except(*keys) == JSON.parse(r2.body).except(*keys) | |
rescue JSON::ParserError | |
false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment