Class: SKP::Client
- Inherits:
-
Object
- Object
- SKP::Client
- Defined in:
- lib/skp/client.rb
Constant Summary collapse
- SKP_SERVER_DOMAIN =
ENV["SKP_SERVER_DOMAIN"] || "https://rpw-licensor.speedshop.co"
Instance Attribute Summary collapse
-
#gateway ⇒ Object
readonly
Returns the value of attribute gateway.
Instance Method Summary collapse
- #complete(position) ⇒ Object
- #current ⇒ Object
- #directories_ready? ⇒ Boolean
- #directory_setup(home_dir_ok = true) ⇒ Object
- #download_and_extract(content) ⇒ Object
-
#initialize(gateway = nil) ⇒ Client
constructor
A new instance of Client.
- #latest_version? ⇒ Boolean
- #list ⇒ Object
- #next ⇒ Object
- #register_email(email) ⇒ Object
- #set_progress(pos, all_prior: false) ⇒ Object
- #setup(key) ⇒ Object
- #setup? ⇒ Boolean
- #show(content_pos) ⇒ Object
Constructor Details
#initialize(gateway = nil) ⇒ Client
Returns a new instance of Client.
9 10 11 |
# File 'lib/skp/client.rb', line 9 def initialize(gateway = nil) @gateway = gateway || Gateway.new(SKP_SERVER_DOMAIN, client_data["key"]) end |
Instance Attribute Details
#gateway ⇒ Object (readonly)
Returns the value of attribute gateway.
7 8 9 |
# File 'lib/skp/client.rb', line 7 def gateway @gateway end |
Instance Method Details
#complete(position) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/skp/client.rb', line 132 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 ⇒ Object
28 29 30 31 |
# File 'lib/skp/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? ⇒ Boolean
118 119 120 121 122 |
# File 'lib/skp/client.rb', line 118 def directories_ready? ["video", "quiz", "lab", "text", "compiled"].all? do |path| File.directory?(path) end end |
#directory_setup(home_dir_ok = true) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/skp/client.rb', line 53 def directory_setup(home_dir_ok = true) ["video", "quiz", "lab", "text", "compiled", "prof_gray"].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(/skp_info/) File.open(".gitignore", "a") do |f| f.puts "\n" f.puts ".skp_info\n" f.puts "video\n" f.puts "quiz\n" f.puts "lab\n" f.puts "text\n" f.puts "compiled\n" f.puts "prof_gray\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) ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/skp/client.rb', line 124 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? ⇒ Boolean
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/skp/client.rb', line 95 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 ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/skp/client.rb', line 33 def list @list ||= 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 |
#next ⇒ Object
23 24 25 26 |
# File 'lib/skp/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) ⇒ Object
19 20 21 |
# File 'lib/skp/client.rb', line 19 def register_email(email) gateway.register_email(email) end |
#set_progress(pos, all_prior: false) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/skp/client.rb', line 82 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) ⇒ Object
13 14 15 16 17 |
# File 'lib/skp/client.rb', line 13 def setup(key) success = gateway.authenticate_key(key) client_data["key"] = key if success success end |
#setup? ⇒ Boolean
113 114 115 116 |
# File 'lib/skp/client.rb', line 113 def setup? return false unless ClientData.exists? client_data["key"] end |
#show(content_pos) ⇒ Object
49 50 51 |
# File 'lib/skp/client.rb', line 49 def show(content_pos) list.find { |l| l["position"] == content_pos } end |