Sha256: 20ab53641692561e292f691f0b4273a2d250ef2ff70ed35b2535b0dc7b76b801

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require "social_avatar_proxy/avatar_file"
require "social_avatar_proxy/configuration/cache"
require "digest/md5"
require "fileutils"

module SocialAvatarProxy
  class Configuration
    class FileCache < Cache
      def read(options = {})
        path = generate_key(options)
        # ensure the file exists and it's readable
        unless File.exist?(path) && File.readable?(path)
          return false
        end
        # return the file
        AvatarFile.new(path).tap do |file|
          file.mtime(File.mtime(path))
        end
      end
      
      def write(file, options = {})
        # generate the file location
        path = generate_key(options)
        # create the folder
        FileUtils.mkdir_p File.dirname(path)
        # write the file
        File.open(path, "wb") do |f|
          f.write(file.read)
        end
      end
      
      private
      def generate_key(options = {})
        # generate a hash for the filename
        hash = "#{options[:service]}-#{options[:identifier]}"
        name = "sap-#{Digest::MD5.hexdigest(hash)}"
        File.join(@directory, name)
      end
      
      # set the directory
      def directory(value)
        @directory = value
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
social-avatar-proxy-2.0.1 lib/social_avatar_proxy/configuration/file_cache.rb
social-avatar-proxy-2.0.0 lib/social_avatar_proxy/configuration/file_cache.rb