Sha256: 26fdbb2c3357900610717720654a6235930f3632d31581d7bf7f9a765e2915c6

Contents?: true

Size: 1.8 KB

Versions: 10

Compression:

Stored size: 1.8 KB

Contents

require 'fileutils'

module VCR
  class Cassette
    class Persisters
      # The only built-in cassette persister. Persists cassettes to the file system.
      module FileSystem
        extend self

        # @private
        attr_reader :storage_location

        # @private
        def storage_location=(dir)
          FileUtils.mkdir_p(dir) if dir
          @storage_location = dir ? absolute_path_for(dir) : nil
        end

        # Gets the cassette for the given storage key (file name).
        #
        # @param [String] file_name the file name
        # @return [String] the cassette content
        def [](file_name)
          path = absolute_path_to_file(file_name)
          return nil unless File.exist?(path)
          File.read(path)
        end

        # Sets the cassette for the given storage key (file name).
        #
        # @param [String] file_name the file name
        # @param [String] content the content to store
        def []=(file_name, content)
          path = absolute_path_to_file(file_name)
          directory = File.dirname(path)
          FileUtils.mkdir_p(directory) unless File.exist?(directory)
          File.open(path, 'w') { |f| f.write(content) }
        end

        # @private
        def absolute_path_to_file(file_name)
          return nil unless storage_location
          File.join(storage_location, sanitized_file_name_from(file_name))
        end

      private

        def absolute_path_for(path)
          Dir.chdir(path) { Dir.pwd }
        end

        def sanitized_file_name_from(file_name)
          parts = file_name.to_s.split('.')

          if parts.size > 1 && !parts.last.include?(File::SEPARATOR)
            file_extension = '.' + parts.pop
          end

          parts.join('.').gsub(/[^\w\-\/]+/, '_') + file_extension.to_s
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 4 rubygems

Version Path
dwolla_swagger-1.0.6 vendor/bundle/ruby/2.2.0/gems/vcr-2.9.3/lib/vcr/cassette/persisters/file_system.rb
vcr-3.0.0 lib/vcr/cassette/persisters/file_system.rb
mustwin-vcr-2.9.3 lib/vcr/cassette/persisters/file_system.rb
vcr-2.9.3 lib/vcr/cassette/persisters/file_system.rb
vcr-2.9.2 lib/vcr/cassette/persisters/file_system.rb
vcr-2.9.1 lib/vcr/cassette/persisters/file_system.rb
vcr-2.9.0 lib/vcr/cassette/persisters/file_system.rb
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/vcr-2.8.0/lib/vcr/cassette/persisters/file_system.rb
vcr-2.8.0 lib/vcr/cassette/persisters/file_system.rb
vcr-2.7.0 lib/vcr/cassette/persisters/file_system.rb