class Compostr::MediaLibraryCache

Cache name->id for media items pretty wet copy of entitycache. Unclear how to DRY this up. Wordpress apparently lowercases file endings on upload, so this fact is respected in the lookup (only file ending is modified).

Attributes

name_id_map[RW]

Public Class Methods

new() click to toggle source
# File lib/compostr/media_library_cache.rb, line 9
def initialize
  @name_id_map = nil
end

Public Instance Methods

id_of_name(name) click to toggle source

return id of given name, initializing the cache if necessary

# File lib/compostr/media_library_cache.rb, line 15
def id_of_name name
  return [] if name.nil? || name.empty?
  # Downcase file ending
  name_for_wp = File.basename(name, '.*') + File.extname(name).downcase
  name_id_map[name_for_wp]
end
id_of_names(names) click to toggle source

return array of ids to given names, initializing the cache if necessary

# File lib/compostr/media_library_cache.rb, line 24
def id_of_names names
  return [] if names.nil? || names.empty?
  names.map{|name| id_of_name name }
end

Private Instance Methods

create_name_id_map() click to toggle source
# File lib/compostr/media_library_cache.rb, line 38
def create_name_id_map
  items = Compostr::wp.getMediaLibrary(blog_id: 0)

  # warn if size is interesting.

  items.map do |i|
    uri = URI.parse URI.encode(i['link'])
    filename = File.basename uri.path

    [filename, i['attachment_id']]
  end.to_h
end