Skip to content

Instantly share code, notes, and snippets.

@kemenaran
Created October 18, 2022 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kemenaran/2dcc463fdda3e476983bf5500f3524b9 to your computer and use it in GitHub Desktop.
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
# 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