Class: Dropsonde::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/dropsonde/cache.rb

Overview

cache class

Constant Summary collapse

@@cache =

rubocop:disable Style/ClassVars

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = '~/.dropsonde', ttl = 7, autoupdate = true) ⇒ Cache

rubocop:disable Style/OptionalBooleanParameter



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dropsonde/cache.rb', line 13

def initialize(path = '~/.dropsonde', ttl = 7, autoupdate = true) # rubocop:disable Style/OptionalBooleanParameter
  path = File.expand_path(path)
  FileUtils.mkdir_p(path)
  @path = "#{path}/forge.json"
  @ttl  = ttl
  @autoupdate = autoupdate

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

  PuppetForge.user_agent = 'Dropsonde Telemetry Client/0.0.1'
end

Class Method Details

.forge_module?(mod) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dropsonde/cache.rb', line 44

def self.forge_module?(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

  @@cache['modules'].include? modname.tr('/', '-')
end

.modulesObject



32
33
34
# File 'lib/dropsonde/cache.rb', line 32

def self.modules
  @@cache['modules']
end

Instance Method Details

#autoupdateObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dropsonde/cache.rb', line 81

def 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

  return update if (Date.today - File.mtime(@path).to_date).to_i > @ttl
end

#cacheObject



40
41
42
# File 'lib/dropsonde/cache.rb', line 40

def cache
  @@cache
end

#modulesObject



36
37
38
# File 'lib/dropsonde/cache.rb', line 36

def modules
  Dropsonde::Cache.modules
end

#updateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dropsonde/cache.rb', line 58

def 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