]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blobdiff - lib/whatever-dl/configuration.rb
Use strings instead of symbols for the downloader type.
[dead/whatever-dl.git] / lib / whatever-dl / configuration.rb
index 60066cca0c4ea652b87dc3714dbea961aba485b1..9cca34a42b7c4bcb9178a3886cdfa569d0021426 100644 (file)
@@ -1,14 +1,54 @@
-module Configuration
-  # Configurable Options
+require 'yaml'
 
-  # We support a couple of different download methods. The :openuri
+class Configuration
+  # Configurable Options
+  #
+  # We support a couple of different download methods. The 'openuri'
   # method utilizes the ruby open-uri library, and provides its own
-  # progress bar with no external dependencies. The :wget method, on the
+  # progress bar with no external dependencies. The 'wget' method, on the
   # other hand, requires GNU Wget (http://www.gnu.org/software/wget/),
   # but will support auto-resume of partially-downloaded files for
   # example.
-  DOWNLOAD_METHOD = :openuri
-  #DOWNLOAD_METHOD = :wget
 
-  USER_AGENT = 'whatever-dl'
+  USERCONF_PATH = ENV['HOME'] + '/.whatever-dl.conf.yml'
+  @dict = {}
+
+  def initialize()
+    cfg = default_configuration()
+
+    # Now, load the user configuration which will override the
+    # variables defined above.
+    begin
+      user_config = YAML.load(File.open(USERCONF_PATH))
+
+      # We require ruby-1.9.x now so this should exist.
+      cfg.merge!(user_config)
+
+    rescue Errno::ENOENT
+      # If the user config file doesn't exist, whatever.
+    end
+
+    # Convert all of the keys to symbols.
+    cfg = cfg.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
+
+    @dict = cfg
+  end
+
+  def method_missing(sym, *args)
+    # Replace all missing method calls with dictionary lookups.
+    return @dict[sym]
+  end
+
+
+  private;
+
+  def default_configuration()
+    d = {}
+
+    d['download_method'] = 'openuri'
+    d['user_agent'] = 'whatever-dl'
+
+    return d
+  end
+
 end