lib/mite-backup.rb in mite-backup-0.2.0 vs lib/mite-backup.rb in mite-backup-0.3.0

- old
+ new

@@ -4,21 +4,24 @@ require "uri" require 'zlib' require 'yaml' class MiteBackup - MAX_CHECKS = 48 + DEFAULT_WAIT_FOR = 240 # seconds SLEEP_BEFORE_EACH_CHECK = 5 # seconds CONFIG_FILE = File.expand_path('~/.mite-backup.yml') USER_AGENT = "mite-backup/#{MiteBackup::VERSION}" def initialize(options={}) @options = options self.class.clear_config if options["clear_config"] @account = correct_account(options["account"] || config["account"]) @email = options["email"] || config["email"] @password = options["password"] || config["password"] + @max_checks = options["wait_for"] != DEFAULT_WAIT_FOR ? + options["wait_for"] / SLEEP_BEFORE_EACH_CHECK : + config["wait_for"] || DEFAULT_WAIT_FOR end def run runnable? create @@ -28,10 +31,12 @@ def setup (config["account"] = @account) || config.delete("account") (config["email"] = @email) || config.delete("email") (config["password"] = @password) || config.delete("password") + (config["wait_for"] = @options["wait_for"]) || config.delete("wait_for") + config.delete("wait_for") if config["wait_for"] == DEFAULT_WAIT_FOR if config.size == 0 self.class.clear_config else File.open(CONFIG_FILE, "w") do |f| @@ -57,11 +62,11 @@ "Could not find any account located at #{domain}" if code == "404" })["id"] end def check - MAX_CHECKS.times do |i| + @max_checks.times do |i| sleep(SLEEP_BEFORE_EACH_CHECK) @ready = parse_json(perform_request(Net::HTTP::Get.new("/account/backup/#{@id}.json")))["ready"] break if @ready end end @@ -75,10 +80,10 @@ else Zlib::GzipReader.new(content_str, :external_encoding => content.encoding) end puts gz.read else - failed "Backup was not ready for download after #{MAX_CHECKS*SLEEP_BEFORE_EACH_CHECK} seconds. Contact the mite support." + failed "Backup was not ready for download after #{@options["wait_for"]} seconds. Contact the mite.support." end end def perform_request(request) request.basic_auth(@email, @password)