We still need a lot in the way of documentation.
Also, I have no clue how I'll do the tests at this point.
}
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
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
@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
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
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
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
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
# 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
# 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: "