Sha256: 0a09ce26a8fe07533de5a364ea96b604dc51546e3cec41bc655798cdb1bd3d9a

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# -*- encoding: utf-8 -*-

module MusicBrainz
  module Tools
    class Cache
      class << self
        @@cache_path = nil

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

        def cache_path
          @@cache_path
        end

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

        def 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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
musicbrainz-0.7.1 lib/tools/cache.rb
musicbrainz-0.7.0 lib/tools/cache.rb