Class: JRuby::Lint::Libraries::Cache
- Inherits:
-
Object
- Object
- JRuby::Lint::Libraries::Cache
- Defined in:
- lib/jruby/lint/libraries.rb
Instance Method Summary collapse
- #fetch(name) ⇒ Object
- #filename_for(name) ⇒ Object
-
#initialize(cache_dir = nil) ⇒ Cache
constructor
A new instance of Cache.
- #read_from_wiki(name, filename) ⇒ Object
- #stale?(filename) ⇒ Boolean
- #store(name, content) ⇒ Object
Constructor Details
#initialize(cache_dir = nil) ⇒ Cache
Returns a new instance of Cache.
9 10 11 12 13 |
# File 'lib/jruby/lint/libraries.rb', line 9 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
15 16 17 18 19 20 21 22 |
# File 'lib/jruby/lint/libraries.rb', line 15 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
28 29 30 31 |
# File 'lib/jruby/lint/libraries.rb', line 28 def filename_for(name) name = File.basename(name) File.join(@cache_dir, File.extname(name).empty? ? "#{name}.html" : name) end |
#read_from_wiki(name, filename) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jruby/lint/libraries.rb', line 37 def read_from_wiki(name, filename) content = nil uri = Net::HTTP.start('wiki.jruby.org', 80) do |http| URI.parse http.head(name =~ %r{^/} ? name : "/#{name}")['Location'] end if uri.host == "github.com" Net::HTTP.new(uri.host, uri.port).tap do |http| if uri.scheme == "https" http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER # gd_bundle.crt from https://certs.godaddy.com/anonymous/repository.seam http.ca_file = File.('../github.crt', __FILE__) end http.start do response = http.get(uri.path) content = response.body File.open(filename, "w") do |f| f << content end end end end raise "Unknown location '#{uri}' for page '#{name}'" unless content content rescue => e raise "Error while reading from wiki: #{e.message}\nPlease try again later." end |
#stale?(filename) ⇒ Boolean
33 34 35 |
# File 'lib/jruby/lint/libraries.rb', line 33 def stale?(filename) File.mtime(filename) < Time.now - 24 * 60 * 60 end |
#store(name, content) ⇒ Object
24 25 26 |
# File 'lib/jruby/lint/libraries.rb', line 24 def store(name, content) File.open(filename_for(name), "w") {|f| f << content } end |