Skip to content

Commit

Permalink
Major code refactoring & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JBlackN committed Sep 15, 2015
1 parent d752a0f commit 4102c93
Show file tree
Hide file tree
Showing 18 changed files with 556 additions and 459 deletions.
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require 'rubygems'
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
require 'coveralls/rake/task'
require 'cucumber'
require 'cucumber/rake/task'
require "rspec/core/rake_task"
require 'rspec/core/rake_task'
require 'rubocop/rake_task'

Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = "features --format pretty"
t.cucumber_opts = 'features --format pretty'
end
RSpec::Core::RakeTask.new(:spec)
Coveralls::RakeTask.new
RuboCop::RakeTask.new

task :default => :spec
task default: :spec
6 changes: 3 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "budik"
require 'bundler/setup'
require 'budik'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +10,5 @@ require "budik"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start
56 changes: 30 additions & 26 deletions budik.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,45 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'budik/version'

Gem::Specification.new do |spec|
spec.name = "budik"
spec.name = 'budik'
spec.version = Budik::VERSION
spec.authors = ["Petr Schmied"]
spec.email = ["jblack@paworld.eu"]
spec.authors = ['Petr Schmied']
spec.email = ['jblack@paworld.eu']

spec.summary = %q{Alarm clock.}
spec.description = %q{Alarm clock which randomly plays a song or a video from YouTube or your local collection.}
spec.homepage = "http://jblack.paworld.eu/apps/budik"
spec.license = "MIT"
spec.summary = 'Alarm clock.'
spec.description = 'Alarm clock which randomly plays a song or'\
'a video from YouTube or your local collection.'
spec.homepage = 'http://jblack.paworld.eu/apps/budik'
spec.license = 'MIT'

# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "rubygems.org"
spec.metadata['allowed_push_host'] = 'rubygems.org'
else
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
fail 'RubyGems 2.0 or newer is required to protect'\
'against public gem pushes.'
end

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency "awesome_print"
spec.add_dependency "commander"
spec.add_dependency "r18n-core"
spec.add_dependency "terminal-table"
spec.add_dependency "ya2yaml"
spec.add_dependency "youtube_addy"
spec.add_dependency "youtube-dl.rb"
spec.add_dependency 'awesome_print'
spec.add_dependency 'commander'
spec.add_dependency 'r18n-core'
spec.add_dependency 'terminal-table'
spec.add_dependency 'ya2yaml'
spec.add_dependency 'youtube_addy'
spec.add_dependency 'youtube-dl.rb'

spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
spec.add_development_dependency "cucumber"
spec.add_development_dependency "rubocop"
spec.add_development_dependency "coveralls"
spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'cucumber'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'coveralls'
end
98 changes: 52 additions & 46 deletions lib/budik.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

#require 'awesome_print'
# require 'awesome_print'
require 'commander'
require 'date'
require 'fileutils'
Expand All @@ -26,85 +26,91 @@
require './lib/budik/sources'
require './lib/budik/version'

# 'Budik' is an alarm clock which randomly plays an item from your media
# collection (local or YouTube).
module Budik
# 'Budik' class is application's main entry point.
class Budik
include Commander::Methods

def initialize
@strings = Config.instance.lang.budik

@str_config = @strings.commands.config
@str_run = @strings.commands.run
@str_sources = @strings.commands.sources
@str_translate = @strings.commands.translate
end

def run
program :name, 'Budik'
program :version, '0.0.1'
program :description, @strings.description

commands

default_command :run

command :config do |c|
str_config = @strings.commands.config
str_opts = @strings.commands.config.options
run!
end

private

def commands
command_config(@str_config.options)
command_run(@str_run.options)
command_sources(@str_sources.options)
command_translate
end

def command_config(str_opts)
command :config do |c|
c.syntax = 'budik config [options]'
c.summary = str_config.summary
c.description = str_config.description
c.summary = @str_config.summary
c.description = @str_config.description
c.option '-r', '--reset [string]', String, str_opts.reset

c.action do |_args, opts|
Command.instance.config(_args, opts)
end
c.action { |_args, opts| Command.new(:config, opts) }
end
end

def command_run(str_opts)
command :run do |c|
str_run = @strings.commands.run
str_opts = @strings.commands.run.options

c.syntax = 'budik run [options]'
c.summary = str_run.summary
c.description = str_run.description
c.option '-c', '--categories [string]', String, str_opts.categories
c.option '-d', '--dl-keep [string]', String, str_opts.dl_keep
c.option '-n', '--number [integer]', Integer, str_opts.number
c.option '-p', '--player [string]', String, str_opts.player
c.option '-r', '--rng [string]', String, str_opts.rng

c.action do |_args, opts|
Command.instance.run(_args, opts)
end
c.summary = @str_run.summary
c.description = @str_run.description
command_run_options(c, str_opts)
c.action { |_args, opts| Command.new(:run, opts) }
end
end

command :sources do |c|
str_sources = @strings.commands.sources
str_opts = @strings.commands.sources.options
def command_run_options(c, str_opts)
c.option '-c', '--categories [string]', String, str_opts.categories
c.option '-d', '--dl-keep [string]', String, str_opts.dl_keep
c.option '-n', '--number [integer]', Integer, str_opts.number
c.option '-p', '--player [string]', String, str_opts.player
c.option '-r', '--rng [string]', String, str_opts.rng
end

def command_sources(str_opts)
command :sources do |c|
c.syntax = 'budik sources [options]'
c.summary = str_sources.summary
c.description = str_sources.description
c.summary = @str_sources.summary
c.description = @str_sources.description
c.option '-l', '--list [string]', String, str_opts.list
c.option '-d', '--download [string]', String, str_opts.download

c.action do |_args, opts|
Command.instance.sources(_args, opts)
end
c.action { |_args, opts| Command.new(:sources, opts) }
end
end

alias_command :test, :'run', '--trace'

def command_translate
command :translate do |c|
str_translate = @strings.commands.translate

c.syntax = 'budik translate [options]'
c.summary = str_translate.summary
c.description = str_translate.description

c.action do |_args, opts|
Command.instance.translate(_args, opts)
end
c.summary = @str_translate.summary
c.description = @str_translate.description
c.action { |_args, opts| Command.new(:translate, opts) }
end

run!
end
end

Budik.new.run if $0 == __FILE__
Budik.new.run if $PROGRAM_NAME == __FILE__
end
62 changes: 39 additions & 23 deletions lib/budik/command.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,64 @@
module Budik
# 'Command' class holds definitions of CLI commands.
class Command
include Singleton
def initialize(command, opts)
@options = Config.instance.options
@sources = Config.instance.sources
@strings = Config.instance.lang.command

def config(_args, opts)
send(command, opts)
end

def run(_args, opts)
options = Config.instance.options
dl_options = options['sources']['download']
private

dl_options['keep'] = opts.dl_keep if opts.dl_keep
options['player']['player'] = opts.player if opts.player
options['rng']['method'] = opts.rng if opts.rng
def config(_opts)
end

def run(opts)
sources = Sources.instance
rng = Rng.instance
devices = Devices.instance
rng = Rng.new
output = Output.instance
player = Player.instance

sources.parse(options['sources']['path'], opts.categories)
run_use_cli_opts(opts)
source = run_prepare(opts, sources, devices, rng, output)
run_play(source, devices, player, sources)
end

def run_use_cli_opts(opts)
@options['sources']['download']['keep'] = opts.dl_keep if opts.dl_keep
@options['player']['player'] = opts.player if opts.player
@options['rng']['method'] = opts.rng if opts.rng
end

def run_prepare(opts, sources, devices, rng, output)
sources.parse(@sources)
mods = opts.categories ? sources.parse_mods(opts.categories) : nil
sources.apply_mods(mods) if mods

devices.storage_mount
number = opts.number ? opts.number : rng.generate(sources.count)
number = opts.number || rng.generate(sources.count)
source = sources.get(number)

puts Output.instance.run_info_table(number, sources.sources[number][:name])
sources.download(number)
puts output.run_info_table(number, source[:name])
sources.download(source)
end

def run_play(source, devices, player, sources)
devices.tv_on
player.play(sources.sources[number])
player.play(source)

devices.tv_off
sources.remove(number)
sources.remove(source)
devices.storage_unmount
devices.storage_sleep
end

def set(_args, opts)
end

def sources(_args, opts)
end

def translate(_args, opts)
def sources(_opts)
end

def unset(_args, opts)
def translate(_opts)
end
end
end
3 changes: 2 additions & 1 deletion lib/budik/config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Budik
# 'Config' class loads and manages app configuration.
class Config
include Singleton

Expand All @@ -15,7 +16,7 @@ def initialize
attr_accessor :options
attr_accessor :sources

def edit(opts)
def edit(_opts)
# TODO
end

Expand Down
1 change: 1 addition & 0 deletions lib/budik/config/lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ budik:
description: "Open options file in your default editor."
options:
reset: "Resets configuration file to default values."
invalid: "Invalid command."
run:
summary: "Runs alarm (default command)."
description: "Runs alarm with specified options. CLI options > options.yml."
Expand Down
Loading

0 comments on commit 4102c93

Please sign in to comment.