]> git.r.bdr.sh - rbdr/lyricli.rb/commitdiff
Enables rdio. :)
authorBen Beltran <redacted>
Sat, 6 Oct 2012 06:19:36 +0000 (01:19 -0500)
committerBen Beltran <redacted>
Sat, 6 Oct 2012 06:19:36 +0000 (01:19 -0500)
We still need a lot in the way of documentation.
Also, I have no clue how I'll do the tests at this point.

bin/lrc
lib/lyricli.rb
lib/lyricli/configuration.rb
lib/lyricli/source_manager.rb
lib/lyricli/sources/rdio.rb
lyricli-0.0.1.gem

diff --git a/bin/lrc b/bin/lrc
index 7bb26999cee3aeda4ea4b88323a31858458e798a..b1a44b625e45d182bdb8e1a020d34178e7775ff3 100755 (executable)
--- a/bin/lrc
+++ b/bin/lrc
@@ -15,7 +15,8 @@ Options:
 }
 
     opts.on("-e", "--enable SOURCE", "Enable SOURCE") do |source|
-      puts "Enabling sources is not yet implemented"
+      Lyricli.enable(source)
+      puts "#{source} has been enabled"
       exit
     end
 
@@ -25,12 +26,14 @@ Options:
     end
 
     opts.on("-d", "--disable SOURCE", "Disable SOURCE") do |source|
-      puts "Disabling sources is not yet implemented"
+      Lyricli.disable(source)
+      puts "#{source} has been disabled"
       exit
     end
 
     opts.on("-r", "--reset SOURCE", "Reset the configuration of SOURCE") do |source|
-      puts "Resetting sources is not yet implemented"
+      Lyricli.reset(source)
+      puts "#{source} has been disabled and all its configuration reset"
       exit
     end
 
index 47db184bb23434fe4e1110fdc31c5e2fe133d95d..557fa3bc331dc65806360374bfc2861e3af07f4b 100755 (executable)
@@ -32,24 +32,30 @@ module Lyricli
     @lyricli.get_lyrics
   end
 
+  # Returns the version of the library
+  # @return [String] the version
   def self.version
     Gem.loaded_specs["lyricli"].version
   end
 
+  # Returns a list of the available sources to enable or disable
   def self.sources
     source_manager = SourceManager.new
-    source_manager.available_sources.join(", ")
+    source_manager.available_sources(true).join(", ")
   end
 
-  def self.enable
-
+  def self.enable(source_name)
+    source_manager = SourceManager.new
+    source_manager.enable(source_name)
   end
 
-  def self.disable
-
+  def self.disable(source_name)
+    source_manager = SourceManager.new
+    source_manager.disable(source_name)
   end
 
-  def self.reset
-
+  def self.reset(source_name)
+    source_manager = SourceManager.new
+    source_manager.reset(source_name)
   end
 end
index 8d26b7f179c8bd48b7539a1b0fae3131aabb81e7..031101720bc87ee74b6bbdec421fb5908cec1cfd 100644 (file)
@@ -24,22 +24,24 @@ module Lyricli
       save_config
     end
 
-    private_class_method :new
+    def delete(key)
+      load_config unless @config
+      @config.delete(key)
+      save_config
+    end
 
-    private
+    private_class_method :new
 
     # TODO: Apart from this, load a default yml that will be used for this.
     # And just extend everything from the user's config.
     def load_config
-      # path = File.expand_path(@config_path)
-      path_root = File.expand_path(File.dirname(__FILE__))
-      path = File.join(path_root, @defaults_path)
+      path = File.expand_path(@config_path)
 
       if File.exists?(path)
         file = File.new(path, "r")
         @config = MultiJson.decode(file.read)
       else
-        @config = {}
+        load_default_config
       end
     end
 
@@ -49,5 +51,20 @@ module Lyricli
       file.print(MultiJson.encode(@config))
       file.close
     end
+
+    private
+
+    def load_default_config
+      # Load the default
+      path_root = File.expand_path(File.dirname(__FILE__))
+      path = File.join(path_root, @defaults_path)
+
+      if File.exists?(path)
+        file = File.new(path, "r")
+        @config = MultiJson.decode(file.read)
+      else
+        @config = {}
+      end
+    end
   end
 end
index 0bc81e5f5b313c45c0904dc5cf8c943aa7ab3854..ac1cd7fcd01263a6b8bac00127e6ac1219b3f90f 100644 (file)
@@ -17,29 +17,43 @@ module Lyricli
     end
 
     def enable(source_name)
-      if klass = parse_class(camelize(source_name))
-        klass.enable
-        @config["enabled_sources"] << klass.name
-        @config["enabled_sources"].uniq!
+      if available_sources.include?(source_name)
+        if klass = parse_class(camelize(source_name))
+          klass.enable
+          @config["enabled_sources"] << klass.name
+          @config["enabled_sources"].uniq!
+          @config.save_config
+        else
+          raise EnableSourceException
+        end
       else
-        raise EnableSourceException
+        raise UnknownSource
       end
     end
 
     def disable(source_name)
-      if klass = parse_class(camelize(source_name))
-        @config["enabled_sources"].delete(klass.name)
+      if available_sources.include?(source_name)
+        if klass = parse_class(camelize(source_name))
+          @config["enabled_sources"].delete(klass.name)
+          @config.save_config
+        else
+          raise DisableSourceException
+        end
       else
-        raise DisableSourceException
+        raise UnknownSource
       end
     end
 
     def reset(source_name)
-      if klass = parse_class(camelize(source_name))
-        klass.reset
-        disable(source_name)
+      if available_sources.include?(source_name)
+        if klass = parse_class(camelize(source_name))
+          klass.reset
+          disable(source_name)
+        else
+          raise ResetSourceException
+        end
       else
-        raise ResetSourceException
+        raise UnknownSource
       end
     end
 
@@ -47,23 +61,41 @@ module Lyricli
       track = nil
       @enabled_sources.each do |source|
         begin
-          track ||= source.current_track
+          current_track = source.current_track
+
+          unless current_track[:artist].nil? || current_track[:artist].empty? || current_track[:song].nil? || current_track[:song].empty?
+            track = current_track
+          end
         rescue
-          fail "Source #{source.name} has failed to start. Please reset the source by running `#{$0} source reset #{source.name}.`"
+          raise SourceConfigurationException
         end
       end
       track
     end
 
-    def available_sources
+    def available_sources(format = false)
       path_root = File.expand_path(File.dirname(__FILE__))
       sources = Dir[path_root+"/sources/*"].map{ |s|
-        s.split("/").last.gsub(/\.rb/, "")
+        name = s.split("/").last.gsub(/\.rb/, "")
+
+        # Add a star to denote enabled sources
+        name
       }
 
       # Remove arguments (Hack?) We don't want anybody to touch tihs one.
       sources.delete("arguments")
-      sources
+      if format
+        format_sources(sources)
+      else
+        sources
+      end
+    end
+
+    def format_sources(sources)
+      sources.map{ |s|
+        s << "*" if @config["enabled_sources"].include?(s)
+        s
+      }
     end
   end
 end
index 83548b28af680c85f8794f50e4dc9f398ccb2b55..40884b7cb78e278e68a3fc79df2e31b32ae9cd80 100644 (file)
@@ -12,18 +12,22 @@ module Lyricli
       # the source. In the case of Rdio it has to authenticate with OAuth.
       def self.enable
         # Validation Code
-        @config = Lyricli::Configuration.instance
+        @config = Configuration.instance
         unless @config["rdio_auth_token"] && !@config["rdio_auth_token"].empty?
           create_auth_token
         end
 
+        puts "***"
+        puts "Hello, rdio tends to be a bit aggressive and tends to have trouble with other sources. If you're having trouble, you can disable it temporarily. You will not have to reauthenticate."
+        puts "***"
+
       end
 
       # Instantiates everything it needs to run.
       def initialize
         @name = 'rdio'
-        @config = Lyricli::Configuration.instance
-        @rdio = Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
+        @config = Configuration.instance
+        @rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
       end
 
       # The current_track method should return the name of the current
@@ -38,19 +42,20 @@ module Lyricli
 
       # The reset method resets any configurations it may have
       def self.reset
-        # Reset Code
+        @config = Configuration.instance
+        @config.delete("rdio_auth_token")
       end
 
       # Signs in to rdio with our credentials and requests access for a new auth
       # token.
       def self.create_auth_token
-        @rdio = Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
+        rdio = ::Rdio::SimpleRdio.new([@config["rdio_key"], @config["rdio_secret"]], @config["rdio_auth_token"])
 
         # Request Authorization
         puts "Follow this URL to authorize lyricli:"
         auth_url = rdio.begin_authentication('oob')
         puts auth_url
-        Launchy.open(auth_url)
+        ::Launchy.open(auth_url)
 
         # Request Code, Obtain Token
         print "Please type the authorization code: "
index 33a792af384b4405314d173d6ceb8374781c07ef..151cb466788e15d203d69f375acebd6c431c350f 100644 (file)
Binary files a/lyricli-0.0.1.gem and b/lyricli-0.0.1.gem differ