From: Ben Beltran Date: Sat, 6 Oct 2012 06:19:36 +0000 (-0500) Subject: Enables rdio. :) X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli.rb/commitdiff_plain/4f3dbb13d02f6eb2ac2b8fea1879e9d5150166b1?ds=sidebyside;hp=b9f550e97a60c4953fd1ae9374045110836764bd Enables rdio. :) We still need a lot in the way of documentation. Also, I have no clue how I'll do the tests at this point. --- diff --git a/bin/lrc b/bin/lrc index 7bb2699..b1a44b6 100755 --- 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 diff --git a/lib/lyricli.rb b/lib/lyricli.rb index 47db184..557fa3b 100755 --- a/lib/lyricli.rb +++ b/lib/lyricli.rb @@ -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 diff --git a/lib/lyricli/configuration.rb b/lib/lyricli/configuration.rb index 8d26b7f..0311017 100644 --- a/lib/lyricli/configuration.rb +++ b/lib/lyricli/configuration.rb @@ -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 diff --git a/lib/lyricli/source_manager.rb b/lib/lyricli/source_manager.rb index 0bc81e5..ac1cd7f 100644 --- a/lib/lyricli/source_manager.rb +++ b/lib/lyricli/source_manager.rb @@ -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 diff --git a/lib/lyricli/sources/rdio.rb b/lib/lyricli/sources/rdio.rb index 83548b2..40884b7 100644 --- a/lib/lyricli/sources/rdio.rb +++ b/lib/lyricli/sources/rdio.rb @@ -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: " diff --git a/lyricli-0.0.1.gem b/lyricli-0.0.1.gem index 33a792a..151cb46 100644 Binary files a/lyricli-0.0.1.gem and b/lyricli-0.0.1.gem differ