Created
January 6, 2017 16:24
-
-
Save mathieujobin/f890508d7ff87714a07394b9248af231 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'yaml' | |
require 'byebug' | |
$debug_mode = false #$ true | |
def remove_cassettes(mode, pattern) | |
case mode | |
when 'status_code' | |
grep_match = "code:.#{pattern}" | |
when 'uri_match' | |
grep_match = "uri:.#{pattern}" | |
when 'code_uri' | |
grep_match = "code:.#{pattern.first}" | |
else | |
raise 'error' | |
end | |
# Pathname.glob('*/cassettes/**/*.yml').map | |
`grep -rl #{grep_match} */cassettes/ | sort -n`.split.each do |file| | |
content = YAML.load_file(file) | |
content["http_interactions"].delete_if do |i| | |
debugger if $debug_mode | |
case mode | |
when 'status_code' | |
i["response"]["status"]["code"] == pattern | |
when 'uri_match' | |
i["request"]["uri"].match(pattern) | |
when 'code_uri' | |
i["response"]["status"]["code"] == pattern.first && i["request"]["uri"].match(pattern.last) | |
else | |
false | |
end | |
end | |
if content["http_interactions"].size > 0 | |
File.open(file, 'wb+') {|f| f.write(content.to_yaml)} | |
else | |
`git rm "#{file}" || rm "#{file}"` | |
end | |
end | |
end | |
unless [2,3].include? ARGV.size | |
puts "please say 'code 404' or 'uri http://localhost:9200'" | |
end | |
case ARGV[0].to_s | |
when 'code' | |
remove_cassettes('status_code', ARGV[1].to_i) # 404 | |
when 'uri' | |
remove_cassettes('uri_match', ARGV[1].to_s) # "http://localhost:9200" | |
when 'code_uri' | |
remove_cassettes('code_uri', [ARGV[1].to_i, ARGV[2].to_s]) # "http://localhost:9200" | |
else | |
puts "dont know what to do with #{ARGV[0]}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment