dennmart/wkcheck

View on GitHub
bin/wkcheck

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'yaml'
require 'slop'

$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
require 'wkcheck'

opts = Slop.parse do |o|
  o.banner = "Usage: wkcheck [options]"

  o.string "--api-key", "Saves your WaniKani API Key to #{WKCheck::CONFIG_FILE}", suppress_errors: true
  o.on "-q", "--queue", "Displays your current study queue (new lessons / pending reviews)"
  o.on "-l", "--level", "Displays your current level progression (radicals / kanji)"
  o.integer "-c", "--critical", "Displays your current critical items, with an optional argument to set the maximum percentage", suppress_errors: true
  o.on "-k", "--kanji", "Displays information of a randomly selected kanji"
  o.on "-w", "--word", "Displays information of a randomly selected vocabulary word"
  o.on "-v", "--version", "Show version number and quit" do
    puts "wkcheck #{WKCheck::VERSION}"
    exit
  end
  o.on "-h", "--help", "Displays this help information" do
    puts o
    exit
  end
end

unless opts["api-key"].nil? || opts["api-key"].empty?
  File.open(WKCheck::CONFIG_FILE, 'w') { |f| f.write({ api_key: opts["api-key"] }.to_yaml) } unless opts["api-key"].nil?
  puts "WaniKani API Key saved to #{WKCheck::CONFIG_FILE}!"
  exit
end

api_key = if File.exists?(WKCheck::CONFIG_FILE)
            config = YAML.load_file(WKCheck::CONFIG_FILE)
            config[:api_key]
          end

if api_key.nil? || api_key.empty?
  puts "You must first save your WaniKani API Key using the --api-key flag."
  exit 1
end

Wanikani.api_key = api_key

if opts.queue?
  stats = WKCheck::Stats.new
  puts stats.study_queue
  exit
end

if opts.level?
  stats = WKCheck::Stats.new
  puts stats.level_progression
  exit
end

if opts.critical?
  percentage = opts[:critical] || 75

  if percentage.to_i.between?(1, 99)
    critical = WKCheck::CriticalItems.new(percentage.to_i)
    puts critical.critical_items
    exit
  else
    puts "The maximum percentage should be between 1 and 99!"
    exit 1
  end
end

if opts.kanji?
  stats = WKCheck::Stats.new
  puts stats.random_kanji
  exit
end

if opts.word?
  stats = WKCheck::Stats.new
  puts stats.random_word
  exit
end

# No flags / Invalid flags - Display the help.
puts opts