class RPW::Client

Constants

RPW_SERVER_DOMAIN

Attributes

gateway[R]

Public Class Methods

new(gateway = nil) click to toggle source
# File lib/rpw/client.rb, line 9
def initialize(gateway = nil)
  @gateway = gateway || Gateway.new(RPW_SERVER_DOMAIN, client_data["key"])
end

Public Instance Methods

complete(position) click to toggle source
# File lib/rpw/client.rb, line 133
def complete(position)
  if client_data["completed"]
    # we actually have to put the _next_ lesson on the completed stack
    set_progress(self.next["position"])
  else
    client_data["completed"] = []
    set_progress(position)
  end
end
current() click to toggle source
# File lib/rpw/client.rb, line 28
def current
  return list.first unless client_data["completed"]
  list.sort_by { |c| c["position"] }.find { |c| c["position"] == current_position }
end
directories_ready?() click to toggle source
# File lib/rpw/client.rb, line 119
def directories_ready?
  ["video", "quiz", "lab", "text", "cgrp"].all? do |path|
    File.directory?(path)
  end
end
directory_setup(home_dir_ok = true) click to toggle source
# File lib/rpw/client.rb, line 55
def directory_setup(home_dir_ok = true)
  ["video", "quiz", "lab", "text", "cgrp"].each do |path|
    FileUtils.mkdir_p(path) unless File.directory?(path)
  end

  if home_dir_ok
    ClientData.create_in_home!
  else
    ClientData.create_in_pwd!
  end

  unless File.exist?(".gitignore") && File.read(".gitignore").match(/rpw_info/)
    File.open(".gitignore", "a") do |f|
      f.puts "\n"
      f.puts ".rpw_info\n"
      f.puts "video\n"
      f.puts "quiz\n"
      f.puts "lab\n"
      f.puts "text\n"
      f.puts "cgrp\n"
    end
  end

  File.open("README.md", "w+") do |f|
    f.puts File.read(File.join(File.dirname(__FILE__), "README.md"))
  end
end
download_and_extract(content) click to toggle source
# File lib/rpw/client.rb, line 125
def download_and_extract(content)
  location = content["style"] + "/" + content["s3_key"]
  unless File.exist?(location)
    gateway.download_content(content, folder: content["style"])
    extract_content(content) if content["s3_key"].end_with?(".tar.gz")
  end
end
latest_version?() click to toggle source
# File lib/rpw/client.rb, line 96
def latest_version?
  return true unless ClientData.exists?
  return true if client_data["last_version_check"] &&
    client_data["last_version_check"] >= Time.now - (60 * 60)

  begin
    latest = gateway.latest_version?
  rescue
    return true
  end

  client_data["last_version_check"] = if latest
    Time.now
  else
    false
  end
end
list() click to toggle source
# File lib/rpw/client.rb, line 33
def list
  @list ||= begin
    if client_data["content_cache_generated"] &&
        client_data["content_cache_generated"] >= Time.now - 60 * 60

      client_data["content_cache"]
    else
      begin
        client_data["content_cache"] = gateway.list_content
        client_data["content_cache_generated"] = Time.now
        client_data["content_cache"]
      rescue
        client_data["content_cache"] || (raise Error.new("No internet connection"))
      end
    end
  end
end
next() click to toggle source
# File lib/rpw/client.rb, line 23
def next
  return list.first unless client_data["completed"]
  list.sort_by { |c| c["position"] }.find { |c| c["position"] > current_position }
end
register_email(email) click to toggle source
# File lib/rpw/client.rb, line 19
def register_email(email)
  gateway.register_email(email)
end
set_progress(pos, all_prior: false) click to toggle source
# File lib/rpw/client.rb, line 83
def set_progress(pos, all_prior: false)
  client_data["completed"] = [] && return if pos.nil?
  if all_prior
    lessons = list.select { |l| l["position"] <= pos }
    client_data["completed"] = lessons.map { |l| l["position"] }
    lessons
  else
    lesson = list.find { |l| l["position"] == pos }
    client_data["completed"] += [pos]
    lesson
  end
end
setup(key) click to toggle source
# File lib/rpw/client.rb, line 13
def setup(key)
  success = gateway.authenticate_key(key)
  client_data["key"] = key if success
  success
end
setup?() click to toggle source
# File lib/rpw/client.rb, line 114
def setup?
  return false unless ClientData.exists?
  client_data["key"]
end
show(content_pos) click to toggle source
# File lib/rpw/client.rb, line 51
def show(content_pos)
  list.find { |l| l["position"] == content_pos }
end

Private Instance Methods

client_data() click to toggle source
# File lib/rpw/client.rb, line 149
def client_data
  @client_data ||= ClientData.new
end
current_position() click to toggle source
# File lib/rpw/client.rb, line 145
def current_position
  @current_position ||= client_data["completed"]&.last || 0
end
extract_content(content) click to toggle source
# File lib/rpw/client.rb, line 153
def extract_content(content)
  folder = content["style"]
  `tar -C #{folder} -xzf #{folder}/#{content["s3_key"]}`
end