class RPW::CLI

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/rpw/cli.rb, line 18
def self.exit_on_failure?
  true
end

Public Instance Methods

complete() click to toggle source
# File lib/rpw/cli.rb, line 98
def complete
  say "Marked current lesson as complete"
  client.complete(nil)
end
current() click to toggle source
# File lib/rpw/cli.rb, line 89
def current
  exit_with_no_key
  content = client.current
  say "Opening: #{content["title"]}"
  client.download_and_extract(content)
  display_content(content, !options[:"no-open"])
end
download() click to toggle source
# File lib/rpw/cli.rb, line 146
def download
  exit_with_no_key
  total = client.list.size
  client.list.each do |content|
    current = client.list.index(content) + 1
    puts "Downloading #{content["title"]} (#{current}/#{total})"
    client.download_and_extract(content)
  end
end
list() click to toggle source
# File lib/rpw/cli.rb, line 104
def list
  ::CLI::UI::Frame.open("{{*}} {{bold:All Lessons}}", color: :green)

  frame_open = false
  client.list.each do |lesson|
    if lesson["title"].start_with?("Section")
      ::CLI::UI::Frame.close(nil) if frame_open
      ::CLI::UI::Frame.open(lesson["title"])
      frame_open = true
      next
    end

    no_data = client.send(:client_data)["completed"].nil?
    completed = client.send(:client_data)["completed"]&.include?(lesson["position"])

    str = if no_data
      ""
    elsif completed
      "\u{2705} "
    else
      "\u{274C} "
    end

    case lesson["style"]
    when "video"
      puts str + ::CLI::UI.fmt("{{red:#{lesson["title"]}}}")
    when "quiz"
      # puts ::CLI::UI.fmt "{{green:#{"  " + lesson["title"]}}}"
    when "lab"
      puts str + ::CLI::UI.fmt("{{yellow:#{"  " + lesson["title"]}}}")
    when "text"
      puts str + ::CLI::UI.fmt("{{magenta:#{"  " + lesson["title"]}}}")
    else
      puts str + ::CLI::UI.fmt("{{magenta:#{"  " + lesson["title"]}}}")
    end
  end

  ::CLI::UI::Frame.close(nil)
  ::CLI::UI::Frame.close(nil, color: :green)
end
next() click to toggle source
# File lib/rpw/cli.rb, line 70
def next
  exit_with_no_key
  content = client.next

  if content.nil?
    RPW::CLI.new.print_banner
    say "Congratulations!"
    say "You have completed the Rails Performance Workshop."
    exit(0)
  end

  say "Proceeding to next lesson: #{content["title"]}"
  client.download_and_extract(content)
  client.complete(content["position"])
  display_content(content, !options[:"no-open"])
end
print_banner() click to toggle source
reset() click to toggle source
# File lib/rpw/cli.rb, line 185
def reset
  return unless ::CLI::UI.confirm("Are you sure you want to erase all of your progress?", default: false)
  say "Resetting progress."
  client.set_progress(nil)
end
set_progress() click to toggle source
# File lib/rpw/cli.rb, line 173
def set_progress
  title = ::CLI::UI::Prompt.ask(
    "Which lesson would you like to set your progress to? All prior lessons will be marked complete",
    options: client.list.reject { |l| l["title"] == "Quiz" }.map { |l| "  " * l["indent"] + l["title"] }
  )
  title.strip!
  content_order = client.list.find { |l| l["title"] == title }["position"]
  content = client.set_progress(content_order, all_prior: true)
  say "Setting current progress to #{content.last["title"]}"
end
show() click to toggle source
# File lib/rpw/cli.rb, line 159
def show
  exit_with_no_key
  title = ::CLI::UI::Prompt.ask(
    "Which lesson would you like to view?",
    options: client.list.reject { |l| !options[:quizzes] && l["title"] == "Quiz" }.map { |l| "  " * l["indent"] + l["title"] }
  )
  title.strip!
  content_order = client.list.find { |l| l["title"] == title }["position"]
  content = client.show(content_order)
  client.download_and_extract(content)
  display_content(content, !options[:"no-open"])
end
start() click to toggle source
# File lib/rpw/cli.rb, line 23
def start
  warn_if_already_started

  print_banner
  say "\u{1F48E} Welcome to the Rails Performance Workshop. \u{1F48E}"
  say ""
  say "This is rpw, the command line client for this workshop."
  say ""
  say "This client will download files from the internet into the current"
  say "working directory, so it's best to run this client from a new directory"
  say "that you'll use as your 'scratch space' for working on the Workshop."
  say ""

  ans = ::CLI::UI.confirm "Create files and folders in this directory? (no will quit)"

  exit(1) unless ans

  say ""

  ans = ::CLI::UI::Prompt.ask("Where should we save your course progress?",
    options: [
      "here",
      "my home directory (~/.rpw)"
    ])

  client.directory_setup((ans == "my home directory (~/.rpw)"))

  key = ::CLI::UI::Prompt.ask("Your Purchase Key: ")

  unless client.setup(key)
    say "That is not a valid key. Please try again."
    exit(0)
  end

  say ""
  say "Successfully authenticated with the RPW server and saved your key."
  say ""
  say "Setup complete!"
  say ""
  say "To learn how to use this command-line client, consult ./README.md,"
  say "which we just created."
  say ""
  say "Once you've read that and you're ready to get going: $ rpw next"
end

Private Instance Methods

check_version() click to toggle source
# File lib/rpw/cli.rb, line 262
def check_version
  unless client.latest_version?
    say "WARNING: You are running an old version of rpw."
    say "WARNING: Please run `$ gem install rpw`"
  end
end
client() click to toggle source
# File lib/rpw/cli.rb, line 211
def client
  @client ||= RPW::Client.new
end
display_content(content, open_after) click to toggle source
# File lib/rpw/cli.rb, line 215
def display_content(content, open_after)
  openable = false
  case content["style"]
  when "video"
    location = "video/#{content["s3_key"]}"
    openable = true
  when "quiz"
    Quiz.start(["give_quiz", "quiz/" + content["s3_key"]])
  when "lab"
    location = "lab/#{content["s3_key"][0..-8]}"
    openable = true
  when "text"
    location = "text/#{content["s3_key"]}"
    openable = true
  when "cgrp"
    say "The Complete Guide to Rails Performance has been downloaded and extracted to the ./cgrp directory."
    say "All source code for the CGRP is in the src directory, PDF and other compiled formats are in the release directory."
    say "You can check it out now, or to continue: $ rpw next "
  end
  if location
    if openable && !open_after
      say "Download complete. Open with: $ #{open_command} #{location}"
    elsif open_after && openable
      exec "#{open_command} #{location}"
    end
  end
end
exit_with_no_key() click to toggle source
# File lib/rpw/cli.rb, line 199
def exit_with_no_key
  unless client.setup?
    say "You have not yet set up the client. Run $ rpw start"
    exit(1)
  end
  unless client.directories_ready?
    say "You are not in your workshop scratch directory, or you have not yet"
    say "set up the client. Change directory or run $ rpw start"
    exit(1)
  end
end
open_command() click to toggle source
# File lib/rpw/cli.rb, line 244
def open_command
  host_os = RbConfig::CONFIG["host_os"]
  case host_os
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    "start"
  when /darwin|mac os/
    "open"
  else
    "xdg-open"
  end
end
warn_if_already_started() click to toggle source
# File lib/rpw/cli.rb, line 256
def warn_if_already_started
  return unless client.setup?
  exit(0) unless ::CLI::UI.confirm "You have already started the workshop. Continuing "\
    "this command will wipe all of your current progress. Continue?", default: false
end