Sha256: 5bc1426ca6e6b04003be163eace694a14a25d0968edb17b615760c43b1a72fab

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# -*- encoding: utf-8 -*-
module MusicBrainz
  module Tools
    class Cache
      @@cache_path = nil

      def self.cache_path=(path)
        @@cache_path = path
      end

      def self.cache_path
        @@cache_path
      end

      def self.clear_cache
        FileUtils.rm_r(@@cache_path) if @@cache_path && File.exist?(@@cache_path)
      end

      def self.cache_contents(url)
        response = nil
        url_parts = url.split('/')
        file_name = url_parts.pop
        directory = url_parts.pop
        file_path = @@cache_path ? "#{@@cache_path}/#{directory}/#{file_name}" : nil

        if file_path && File.exist?(file_path)
          response = File.open(file_path).gets
        else
          response = yield

          unless response.nil? or file_path.nil?
            FileUtils.mkdir_p file_path.split('/')[0..-2].join('/')
            file = File.new(file_path, 'w')
            file.puts(response.gets) # .force_encoding('UTF-8')
            file.chmod(0755)
            file.close
            response.rewind
          end
        end

        response
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
musicbrainz-0.6.0 lib/tools/cache.rb