Skip to content

Instantly share code, notes, and snippets.

@tfwright
Created March 14, 2012 16:46
Show Gist options
  • Save tfwright/2037799 to your computer and use it in GitHub Desktop.
Save tfwright/2037799 to your computer and use it in GitHub Desktop.
require 'rake'
require 'yaml'
task :migrate_to_vcr, :resource do |t, args|
resource = args[:resource]
blank_vcr_cassette = YAML.load(File.read "test/vcr_cassettes/blank.yml")
`mkdir test/vcr_cassettes/#{resource}`
responses = Dir["test/fixtures/#{resource}*.txt"]
responses.each do |path|
# Build new cassette
response_text = File.read(path)
new_cassette = blank_vcr_cassette.dup["http_interactions"].first
new_cassette["response"]["body"] = response_text.gsub "\n", ""
# Get current cassettes
action_name = File.basename(path, ".txt").scan(/reseller_(.*)_/).flatten.first
action_cassettes_path = "test/vcr_cassettes/#{resource}/#{action_name}.yml"
`touch #{action_cassettes_path}`
current_cassettes = YAML.load(File.read(action_cassettes_path)) || {}
# Insert new cassette
current_cassettes["http_interactions"] ||= []
current_cassettes["http_interactions"] << new_cassette
current_cassettes["recorded_with"] = "VCR 2.0.0"
# Rewrite file
File.truncate(action_cassettes_path, 0)
File.open(action_cassettes_path, 'w') do |f|
f << current_cassettes.to_yaml
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment