module Emojidex::Data::EmojiAssetInformation

Asset information for emoji

Attributes

checksums[RW]
paths[RW]
remote_checksums[RW]

Public Instance Methods

blank_checksums() click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 23
def blank_checksums
  @checksums = {}
  @checksums[:svg] = nil
  @checksums[:png] = {}
  Emojidex::Defaults.sizes.keys.each do |size|
    @checksums[:png][size] = nil
  end
  @remote_checksums = @checksums.dup
end
blank_paths() click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 51
def blank_paths
  @paths = {}
  @paths[:svg] = nil
  @paths[:png] = {}
  Emojidex::Defaults.sizes.keys.each do |size|
    @paths[:png][size] = nil
  end
end
cache(format, sizes = nil) click to toggle source

Caches a file of the specified format in the specified sizes

# File lib/emojidex/data/emoji/asset_information.rb, line 90
def cache(format, sizes = nil)
  generate_checksums
  case format
  when :png
    _cache_png(sizes) unless sizes.nil?
  when :svg
    _cache_svg
  end
end
checksum?(format, size = nil) click to toggle source

returns asset checksum

# File lib/emojidex/data/emoji/asset_information.rb, line 17
def checksum?(format, size = nil)
  puts @checksums
  return @checksums[format][size] unless size.nil?
  @checksums[format]
end
fill_checksums(checksums) click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 42
def fill_checksums(checksums)
  @checksums[:svg] = checksums[:svg] if checksums.include? :svg
  return unless checksums.include? :png
  Emojidex::Defaults.sizes.keys.each do |size|
    @checksums[:png][size] = checksums[:png][size] if checksums[:png].include? size
  end
  @checksums
end
fill_paths(paths) click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 60
def fill_paths(paths)
  @paths[:svg] = paths[:svg] if paths.include? :svg
  return unless paths.include? :png
  Emojidex::Defaults.sizes.keys.each do |size|
    @paths[:png][size] = paths[:png][size] if paths[:png].include? size
  end
  @combinations.each { |combo| combo.fill_paths(paths) }
  @paths
end
fill_remote_checksums(checksums) click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 33
def fill_remote_checksums(checksums)
  @remote_checksums[:svg] = checksums[:svg] if checksums.include? :svg
  return unless checksums.include? :png
  Emojidex::Defaults.sizes.keys.each do |size|
    @remote_checksums[:png][size] = checksums[:png][size] if checksums[:png].include? size
  end
  @remote_checksums
end
generate_checksum(format, size = nil) click to toggle source

Generates a checksum for each locally cached file

# File lib/emojidex/data/emoji/asset_information.rb, line 101
def generate_checksum(format, size = nil)
  case format
  when :png
    return @checksums[:png][size] = _checksum_for_file(@paths[:png][size])
  when :svg
    return @checksums[:svg] = _checksum_for_file(@paths[:svg])
  end
  nil
end
generate_checksums() click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 111
def generate_checksums
  @checksums[:svg] = _checksum_for_file(@paths[:svg])
  @paths[:png].keys.each do |size|
    @checksums[:png][size] = _checksum_for_file(@paths[:png][size])
  end
  @combinations.each { |combo| combo.generate_checksums }
  @checksums
end
init_asset_info(details) click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 10
def init_asset_info(details)
  blank_paths
  blank_checksums
  fill_remote_checksums(details[:checksums]) if details.include? :checksums
end
path(format, size = nil) click to toggle source

Acquires path and caches the target file if not found or out of date

# File lib/emojidex/data/emoji/asset_information.rb, line 71
def path(format, size = nil)
  fp = path?(format, size)
  cache(format, [size]) unless !fp.nil? && File.exist?(fp)
  fp
end
path?(format, size = nil) click to toggle source

returns asset path

# File lib/emojidex/data/emoji/asset_information.rb, line 78
def path?(format, size = nil)
  case format
  when :svg
    return @paths[format] if File.exist?(@paths[format])
  when :png
    return nil if size.nil?
    return @paths[format][size] if File.exist?(@paths[format][size])
  end
  nil
end

Private Instance Methods

_cache_png(sizes) click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 131
def _cache_png(sizes)
  sizes = sizes.keys if sizes.is_a?(Hash)
  sizes.each do |size|
    size = size.first if size.is_a?(Array)
    size = size.key if size.is_a?(Hash)
    unless @paths.include?(:png) &&
           @paths[:png].include?(size) && @paths[:png][size].nil? == false
      @paths[:png][size] = "#{Dir.pwd}/#{size}/#{@code}.png"
    end
    next if File.exist?(@paths[:png][size]) && (
            !@remote_checksums[:png][size].nil? &&
            @remote_checksums[:png][size] == generate_checksum(:png, size))
    response = Emojidex::Service::Transactor.download("#{size}/#{@code}.png")
    File.open(@paths[:png][size], 'wb') { |fp| fp.write(response.body) }
    generate_checksum(:png, size)
  end
end
_cache_svg() click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 122
def _cache_svg
  @paths[:svg] = Dir.pwd unless (@paths.include? :svg) && !@paths[:svg].nil?
  return if File.exist?(@paths[:svg]) && (!@remote_checksums[:svg].nil? &&
            @remote_checksums[:svg] == generate_checksum(:svg))
  response = Emojidex::Service::Transactor.download("#{@code}.svg")
  File.open(@paths[:svg], 'wb') { |fp| fp.write(response.body) }
  generate_checksum(:svg)
end
_checksum_for_file(path) click to toggle source
# File lib/emojidex/data/emoji/asset_information.rb, line 149
def _checksum_for_file(path)
  (File.exist? path) ? Digest::MD5.file(path).hexdigest : nil
end