lib/bundler/audit/database.rb in mrjoy-bundler-audit-0.3.3 vs lib/bundler/audit/database.rb in mrjoy-bundler-audit-0.3.4
- old
+ new
@@ -31,18 +31,12 @@
class Database
# Git URL of the ruby-advisory-db
URL = 'https://github.com/rubysec/ruby-advisory-db.git'
- # Default path to the ruby-advisory-db
- VENDORED_PATH = File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','data','ruby-advisory-db'))
-
- # Timestamp for when the database was last updated
- VENDORED_TIMESTAMP = Time.parse(File.read("#{VENDORED_PATH}.ts")).utc
-
# Path to the user's copy of the ruby-advisory-db
- USER_PATH = File.expand_path(File.join(ENV['HOME'],'.local','share','ruby-advisory-db'))
+ PATH = File.expand_path(File.join(ENV['HOME'],'.local','share','ruby-advisory-db'))
# The path to the advisory database
attr_reader :path
#
@@ -64,43 +58,82 @@
#
# The default path for the database.
#
# @return [String]
- # The path to the database directory.
+ # The path to the database directory. Defaults to {PATH}.
#
def self.path
- if File.directory?(USER_PATH)
- t1 = Dir.chdir(USER_PATH) { Time.parse(`git log --pretty="%cd" -1`) }
- t2 = VENDORED_TIMESTAMP
+ @@path ||= PATH
+ end
- if t1 >= t2 then USER_PATH
- else VENDORED_PATH
- end
+ #
+ # Sets the default path for the database.
+ #
+ # @return [String]
+ # The new default path for the database.
+ #
+ def self.path=(new_path)
+ @@path = new_path
+ end
+
+ #
+ # Downloads the database.
+ #
+ # @return [Boolean]
+ # Specifies whether the download was successful.
+ #
+ # @since 0.4.0
+ #
+ def self.install!
+ system 'git', 'clone', URL, path
+ end
+
+ #
+ # Updates the user's ruby-advisory-db.
+ #
+ # @return [Boolean]
+ # Specifies whether the update was successful.
+ #
+ # @see #update!
+ #
+ # @since 0.3.0
+ #
+ def self.update!
+ if File.directory?(File.join(path, '.git'))
+ new(path).update!
else
- VENDORED_PATH
+ install!
end
end
#
- # Updates the ruby-advisory-db.
+ # Updates the database.
#
# @return [Boolean]
# Specifies whether the update was successful.
#
# @note
# Requires network access.
#
- # @since 0.3.0
+ # @see 0.4.0
#
- def self.update!
- if File.directory?(USER_PATH)
- Dir.chdir(USER_PATH) do
- system 'git', 'pull', 'origin', 'master'
- end
- else
- system 'git', 'clone', URL, USER_PATH
+ def update!
+ Dir.chdir(@path) do
+ system 'git', 'pull', 'origin', 'master'
end
+ end
+
+ #
+ # Determines when the database was last updated.
+ #
+ # @return [Time]
+ # The time of the last update.
+ #
+ # @since 0.4.0
+ #
+ def last_updated
+ Dir.chdir(@path) { Time.parse(`git log -1 --format=%ad`) }
end
#
# Enumerates over every advisory in the database.
#