lib/base/db.rb in logworm-0.7.6 vs lib/base/db.rb in logworm-0.7.7

- old
+ new

@@ -25,27 +25,34 @@ consumer_secret = ENV["#{ENV['APP_ID']}_APPS_SECRET"] host = ENV["#{ENV['APP_ID']}_DB_HOST"] DB.new(DB.make_url(host, consumer_key, consumer_secret, token, token_secret)) end - def self.from_config - # Try with URL from the environment + def self.from_config(app = nil) + # Try with URL from the environment. This will certainly be the case when running on Heroku, in production. return DB.new(ENV['LOGWORM_URL']) if ENV['LOGWORM_URL'] and DB.parse_url(ENV['LOGWORM_URL']) - # Try with configuration file + # If no env. found, try with configuration file, unless app specified config = Logworm::Config.instance - return DB.new(config.url) if config.file_found? and DB.parse_url(config.url) + config.read + unless app + return DB.new(config.url) if config.file_found? and DB.parse_url(config.url) + end # Try with Heroku configuration otherwise - config_vars = %x[heroku config --long] || "" + cmd = "heroku config --long #{app ? " --app #{app}" : ""}" + config_vars = %x[#{cmd}] || "" m = config_vars.match(Regexp.new("LOGWORM_URL\\s+=>\\s+([^\\n]+)")) - config.save(m[1]) and return DB.new(m[1]) if m and DB.parse_url(m[1]) + if m and DB.parse_url(m[1]) + config.save(m[1]) unless (config.file_found? and app) # Do not overwrite if --app is provided + return DB.new(m[1]) + end nil end - def self.from_config_or_die - db = self.from_config + def self.from_config_or_die(app = nil) + db = self.from_config(app) raise "The application is not properly configured. Either use 'heroku addon:add' to add logworm to your app, or save your project's credentials into the .logworm file" unless db db end def self.make_url(host, consumer_key, consumer_secret, token, token_secret)