summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
inline | side by side (from parent 1:
b9f550e)
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|
}
opts.on("-e", "--enable SOURCE", "Enable SOURCE") do |source|
- puts "Enabling sources is not yet implemented"
+ Lyricli.enable(source)
+ puts "#{source} has been enabled"
end
opts.on("-d", "--disable SOURCE", "Disable SOURCE") do |source|
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|
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"
+ # Returns the version of the library
+ # @return [String] the version
def self.version
Gem.loaded_specs["lyricli"].version
end
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
def self.sources
source_manager = SourceManager.new
- source_manager.available_sources.join(", ")
+ source_manager.available_sources(true).join(", ")
+ def self.enable(source_name)
+ source_manager = SourceManager.new
+ source_manager.enable(source_name)
+ def self.disable(source_name)
+ source_manager = SourceManager.new
+ source_manager.disable(source_name)
+ def self.reset(source_name)
+ source_manager = SourceManager.new
+ source_manager.reset(source_name)
- private_class_method :new
+ def delete(key)
+ load_config unless @config
+ @config.delete(key)
+ save_config
+ end
+ 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
# 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
if File.exists?(path)
file = File.new(path, "r")
@config = MultiJson.decode(file.read)
else
file.print(MultiJson.encode(@config))
file.close
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
def enable(source_name)
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
- raise EnableSourceException
end
end
def disable(source_name)
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
- raise DisableSourceException
end
end
def reset(source_name)
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
- raise ResetSourceException
track = nil
@enabled_sources.each do |source|
begin
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
- fail "Source #{source.name} has failed to start. Please reset the source by running `#{$0} source reset #{source.name}.`"
+ raise SourceConfigurationException
+ def available_sources(format = false)
path_root = File.expand_path(File.dirname(__FILE__))
sources = Dir[path_root+"/sources/*"].map{ |s|
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")
}
# Remove arguments (Hack?) We don't want anybody to touch tihs one.
sources.delete("arguments")
+ if format
+ format_sources(sources)
+ else
+ sources
+ end
+ end
+
+ def format_sources(sources)
+ sources.map{ |s|
+ s << "*" if @config["enabled_sources"].include?(s)
+ s
+ }
# the source. In the case of Rdio it has to authenticate with OAuth.
def self.enable
# Validation Code
# 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
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'
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
end
# The current_track method should return the name of the current
# The reset method resets any configurations it may have
def self.reset
# The reset method resets any configurations it may have
def self.reset
+ @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
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
# Request Authorization
puts "Follow this URL to authorize lyricli:"
auth_url = rdio.begin_authentication('oob')
puts auth_url
+ ::Launchy.open(auth_url)
# Request Code, Obtain Token
print "Please type the authorization code: "
# Request Code, Obtain Token
print "Please type the authorization code: "