class VCR::Cassette::Migrator
@private
Public Class Methods
new(dir, out = $stdout)
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 7 def initialize(dir, out = $stdout) @dir, @out = dir, out @yaml_load_errors = yaml_load_errors end
Public Instance Methods
migrate!()
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 12 def migrate! @out.puts "Migrating VCR cassettes in #{@dir}..." Dir["#{@dir}/**/*.yml"].each do |cassette| migrate_cassette(cassette) end end
Private Instance Methods
denormalize_header_key(key)
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 98 def denormalize_header_key(key) key.split('-'). # 'user-agent' => %w(user agent) each { |w| w.capitalize! }. # => %w(User Agent) join('-') end
denormalize_http_header_keys(object)
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 90 def denormalize_http_header_keys(object) object.headers = {}.tap do |denormalized| object.headers.each do |k, v| denormalized[denormalize_header_key(k)] = v end if object.headers end end
load_yaml(cassette)
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 56 def load_yaml(cassette) ::YAML.load_file(cassette) rescue *@yaml_load_errors return nil end
migrate_cassette(cassette)
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 21 def migrate_cassette(cassette) unless http_interactions = load_yaml(cassette) @out.puts " - Ignored #{relative_casssette_name(cassette)} since it could not be parsed as YAML (does it have some ERB?)" return end unless valid_vcr_1_cassette?(http_interactions) @out.puts " - Ignored #{relative_casssette_name(cassette)} since it does not appear to be a valid VCR 1.x cassette" return end http_interactions.map! do |interaction| interaction.recorded_at = File.mtime(cassette) remove_unnecessary_standard_port(interaction) denormalize_http_header_keys(interaction.request) denormalize_http_header_keys(interaction.response) normalize_body(interaction.request) normalize_body(interaction.response) interaction.to_hash end hash = { "http_interactions" => http_interactions, "recorded_with" => "VCR #{VCR.version}" } def hash.each yield 'http_interactions', self['http_interactions'] yield 'recorded_with', self['recorded_with'] end File.open(cassette, 'w') { |f| f.write ::YAML.dump(hash) } @out.puts " - Migrated #{relative_casssette_name(cassette)}" end
normalize_body(object)
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 104 def normalize_body(object) object.body = '' if object.body.nil? end
relative_casssette_name(cassette)
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 68 def relative_casssette_name(cassette) cassette.gsub(%r\A#{Regexp.escape(@dir)}/?|, '') end
remove_unnecessary_standard_port(interaction)
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 77 def remove_unnecessary_standard_port(interaction) uri = VCR.configuration.uri_parser.parse(interaction.request.uri) if uri.scheme == 'http' && uri.port == 80 || uri.scheme == 'https' && uri.port == 443 uri.port = nil interaction.request.uri = uri.to_s end rescue URI::InvalidURIError # ignore this URI. # This can occur when the user uses the filter_sensitive_data option # to put a substitution string in their URI end
valid_vcr_1_cassette?(content)
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 72 def valid_vcr_1_cassette?(content) content.is_a?(Array) && content.map(&:class).uniq == [HTTPInteraction] end
yaml_load_errors()
click to toggle source
# File lib/vcr/cassette/migrator.rb, line 62 def yaml_load_errors [ArgumentError].tap do |errors| errors << Psych::SyntaxError if defined?(Psych::SyntaxError) end end