Class: JRuby::Lint::Libraries::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby/lint/libraries.rb

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir = nil) ⇒ Cache

Returns a new instance of Cache.



8
9
10
11
12
# File 'lib/jruby/lint/libraries.rb', line 8

def initialize(cache_dir = nil)
  @cache_dir = cache_dir || ENV['JRUBY_LINT_CACHE'] ||
    (defined?(Gem.user_dir) && File.join(Gem.user_dir, 'lint')) || Dir::tmpdir
  FileUtils.mkdir_p(@cache_dir) unless File.directory?(@cache_dir)
end

Instance Method Details

#fetch(name) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/jruby/lint/libraries.rb', line 14

def fetch(name)
  filename = filename_for(name)
  if File.file?(filename) && !stale?(filename)
    File.read(filename)
  else
    read_from_wiki(name, filename)
  end
end

#filename_for(name) ⇒ Object



27
28
29
30
# File 'lib/jruby/lint/libraries.rb', line 27

def filename_for(name)
  name = File.basename(name)
  File.join(@cache_dir, File.extname(name).empty? ? "#{name}.md" : name)
end

#read_from_wiki(name, filename) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/jruby/lint/libraries.rb', line 36

def read_from_wiki(name, filename)
  require 'open-uri'
  download = open('https://github.com/jruby/jruby/wiki/C-Extension-Alternatives.md')
  content = download.read(nil)
  File.open(filename, "w") { |f| f << content }
  content
rescue => e
  raise "Error while reading from wiki: #{e.message}\nPlease try again later."
end

#stale?(filename) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/jruby/lint/libraries.rb', line 32

def stale?(filename)
  File.mtime(filename) < Time.now - 24 * 60 * 60
end

#store(name, content) ⇒ Object



23
24
25
# File 'lib/jruby/lint/libraries.rb', line 23

def store(name, content)
  File.open(filename_for(name), "w") {|f| f << content }
end