class Dropsonde::Cache

Public Class Methods

autoupdate() click to toggle source
# File lib/dropsonde/cache.rb, line 68
def self.autoupdate
  return unless @@autoupdate

  unless File.file? @@path
    puts "Dropsonde caches a list of all Forge modules to ensure that it only reports"
    puts "usage data on public modules. Generating this cache may take some time on"
    puts "the first run and you'll see your screen fill up with dots."
    update
  end

  if (Date.today - File.mtime(@@path).to_date).to_i > @@ttl
    update
  end
end
forgeModule?(mod) click to toggle source
# File lib/dropsonde/cache.rb, line 31
def self.forgeModule?(mod)
  case mod
  when Puppet::Module
    modname = mod.forge_slug
  when Hash
    modname = mod[:name] || mod['name']
  when String
    modname = mod
  end
  return unless modname

  modules.include? modname.tr('/','-')
end
init(path, ttl, autoupdate) click to toggle source
# File lib/dropsonde/cache.rb, line 9
def self.init(path, ttl, autoupdate)
  FileUtils.mkdir_p(path)
  @@path = "#{File.expand_path(path)}/forge.json"
  @@ttl  = ttl
  @@autoupdate = autoupdate

  if File.file? @@path
    @@cache = JSON.parse(File.read(@@path))
  else
    @@cache = {
              'timestamp' => '2000-1-1',  # long before any puppet modules were released!
              'modules'   => [],
            }
  end

  PuppetForge.user_agent = "Dropsonde Telemetry Client/0.0.1"
end
modules() click to toggle source
# File lib/dropsonde/cache.rb, line 27
def self.modules
  @@cache['modules']
end
update() click to toggle source
# File lib/dropsonde/cache.rb, line 45
def self.update
  puts "Updating module cache..."
  iter   = PuppetForge::Module.all(:sort_by => 'latest_release')
  newest = DateTime.parse(@@cache['timestamp'])

  @@cache['timestamp'] = iter.first.updated_at

  until iter.next.nil?
    # stop once we reach modules we've already cached
    break if DateTime.parse(iter.first.updated_at) <= newest

    @@cache['modules'].concat iter.map {|mod| mod.slug }

    iter = iter.next
    print '.'
  end
  puts
  @@cache['modules'].sort!
  @@cache['modules'].uniq!

  File.write(@@path, JSON.pretty_generate(@@cache))
end