Class: RPW::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rpw/client.rb

Constant Summary collapse

RPW_SERVER_DOMAIN =
ENV["RPW_SERVER_DOMAIN"] || "https://rpw-licensor.speedshop.co"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway = nil) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/rpw/client.rb', line 9

def initialize(gateway = nil)
  @gateway = gateway || Gateway.new(RPW_SERVER_DOMAIN, client_data["key"])
end

Instance Attribute Details

#gatewayObject (readonly)

Returns the value of attribute gateway.



7
8
9
# File 'lib/rpw/client.rb', line 7

def gateway
  @gateway
end

Instance Method Details

#complete(position) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/rpw/client.rb', line 131

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

#currentObject



28
29
30
31
# 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?Boolean

Returns:

  • (Boolean)


117
118
119
120
121
# File 'lib/rpw/client.rb', line 117

def directories_ready?
  ["video", "quiz", "lab", "text", "cgrp"].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
# File 'lib/rpw/client.rb', line 53

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) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/rpw/client.rb', line 123

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

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rpw/client.rb', line 94

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

#listObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rpw/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

#nextObject



23
24
25
26
# 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) ⇒ Object



19
20
21
# File 'lib/rpw/client.rb', line 19

def register_email(email)
  gateway.register_email(email)
end

#set_progress(pos, all_prior: false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rpw/client.rb', line 81

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/rpw/client.rb', line 13

def setup(key)
  success = gateway.authenticate_key(key)
  client_data["key"] = key if success
  success
end

#setup?Boolean

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/rpw/client.rb', line 112

def setup?
  return false unless ClientData.exists?
  client_data["key"]
end

#show(content_pos) ⇒ Object



49
50
51
# File 'lib/rpw/client.rb', line 49

def show(content_pos)
  list.find { |l| l["position"] == content_pos }
end