learn-config 0.0.32 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6136a968684df0874fd1233d310236ecef7a71f7
4
- data.tar.gz: 962d7fc423fca0d5c815197f2d3adbee3c0c2271
3
+ metadata.gz: cb03acc57001c37cbe478f0643a06dd3dfaa382a
4
+ data.tar.gz: d2c7e51ab9b44078230c00dc6b6d824b47be9ca7
5
5
  SHA512:
6
- metadata.gz: 18a389430d3ee342378cf2d396e4979901dc7329316267cdc3c1de375eb7165854552cc7f3a9f370c64925a37657e1891d8d9d53213ade3d9b3f958672e3d675
7
- data.tar.gz: 60aec616c42eaebea70d96e24486a7a4e0b4283d6f97d1b731f9fdedd5956657c0fd7dc92993328037895a21699619dbff7b50b3ecbcad0dd04b87b4367d04e1
6
+ metadata.gz: a4466289fdd1415b918f20646745589e5cd36b8f9fa3bb3bcdf1335faabfd6fd81b06d2cb4ccaefed4d242346f6e45776664b63293a1dfe70e6df4973334bbb8
7
+ data.tar.gz: 73c820c29bd1a684bf6c119f29e93e17d75f8e761684035a857cc22ea34abd6bd54c5fb8c1217d943377f5d9168baa64bed1b0d5bcaba1a23749ac4da6648d80
data/bin/learn-config CHANGED
@@ -4,7 +4,8 @@ require 'learn_config'
4
4
 
5
5
  KNOWN_COMMANDS = [
6
6
  '--reset',
7
- '--whoami'
7
+ '--whoami',
8
+ '--set-directory'
8
9
  ]
9
10
 
10
11
  ARGV.map! do |arg|
data/learn-config.gemspec CHANGED
@@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "rake", "~> 10.0"
22
22
 
23
23
  spec.add_runtime_dependency "netrc"
24
- spec.add_runtime_dependency "learn-web"
24
+ spec.add_runtime_dependency "learn-web", ">= 1.0.0"
25
25
  end
@@ -11,7 +11,7 @@ module LearnConfig
11
11
  if !short_text
12
12
  puts <<-LONG
13
13
  To connect with the Learn web application, you will need to configure
14
- the Learn gem with an OAuth token. You can find yours on your profile
14
+ the Learn gem with an OAuth token. You can find yours at the bottom of your profile
15
15
  page at: https://learn.co/#{github_username ? github_username : 'your-github-username'}.
16
16
 
17
17
  LONG
@@ -39,9 +39,21 @@ page at: https://learn.co/#{github_username ? github_username : 'your-github-use
39
39
  end
40
40
  end
41
41
 
42
- def token_valid?
42
+ def token_valid?(retries=3)
43
43
  learn = LearnWeb::Client.new(token: token, silent_output: true)
44
- learn.valid_token?
44
+ begin
45
+ Timeout::timeout(15) do
46
+ learn.valid_token?
47
+ end
48
+ rescue Timeout::Error
49
+ if retries > 0
50
+ puts "There was an error validating your token. Retrying..."
51
+ token_valid?(retries-1)
52
+ else
53
+ puts "There was a problem connecting to Learn. Please try again."
54
+ exit
55
+ end
56
+ end
45
57
  end
46
58
  end
47
59
  end
@@ -1,16 +1,17 @@
1
1
  module LearnConfig
2
2
  class Setup
3
- attr_reader :netrc, :args, :reset, :whoami
3
+ attr_reader :netrc, :args, :reset, :whoami, :set_dir
4
4
 
5
5
  def self.run(args)
6
6
  new(args).run
7
7
  end
8
8
 
9
9
  def initialize(args)
10
- @args = args
11
- @netrc = LearnConfig::NetrcInteractor.new
12
- @reset = !!args.include?('--reset')
13
- @whoami = !!args.include?('--whoami')
10
+ @args = args
11
+ @netrc = LearnConfig::NetrcInteractor.new
12
+ @reset = !!args.include?('--reset')
13
+ @whoami = !!args.include?('--whoami')
14
+ @set_dir = !!args.include?('--set-directory')
14
15
  end
15
16
 
16
17
  def run
@@ -19,24 +20,101 @@ module LearnConfig
19
20
  confirm_and_reset!
20
21
  elsif whoami
21
22
  args.delete('--whoami')
23
+ check_config
22
24
  whoami?
25
+ elsif set_dir
26
+ args.delete('--set-directory')
27
+ check_config
28
+ set_directory!
23
29
  else
24
- setup_netrc
30
+ check_config
25
31
  end
26
32
  end
27
33
 
28
34
  private
29
35
 
30
- def whoami?
36
+ def check_config
37
+ setup_netrc
38
+ setup_learn_directory
39
+ setup_editor
40
+ end
41
+
42
+ def whoami?(retries=3)
31
43
  _learn, token = netrc.read
32
- me = LearnWeb::Client.new(token: token).me
33
- puts "Name: #{me.full_name}"
34
- puts "Username: #{me.username}"
35
- puts "Email: #{me.email}"
44
+ begin
45
+ me = Timeout::timeout(15) do
46
+ LearnWeb::Client.new(token: token).me
47
+ end
48
+ rescue Timeout::Error
49
+ if retries > 0
50
+ puts "There was an error connecting to Learn. Retrying..."
51
+ whoami?(retries-1)
52
+ else
53
+ puts "Could not connect to Learn. Please try again."
54
+ exit
55
+ end
56
+ end
57
+ puts "Name: #{me.full_name}"
58
+ puts "Username: #{me.username}"
59
+ puts "Email: #{me.email}"
60
+ puts "Learn Dir: #{learn_directory}"
36
61
 
37
62
  exit
38
63
  end
39
64
 
65
+ def learn_directory
66
+ config_data = File.read(File.expand_path('~/.learn-config'))
67
+ YAML.load(config_data)[:learn_directory]
68
+ end
69
+
70
+ def set_directory!
71
+ path = ''
72
+
73
+ while !path.start_with?('/')
74
+ print "Enter the directory in which to store Learn lessons (/Users/#{ENV['USER']}/Development/code): "
75
+ path = gets.chomp
76
+
77
+ if path.start_with?('~')
78
+ path = File.expand_path(path)
79
+ elsif path == ''
80
+ path = "/Users/#{ENV['USER']}/Development/code"
81
+ elsif !path.start_with?('/')
82
+ puts "Absolute paths only, please!"
83
+ end
84
+ end
85
+
86
+ write_new_directory_data!(path)
87
+ end
88
+
89
+ def write_new_directory_data!(path)
90
+ create_dir = true
91
+
92
+ if !new_directory_exists?(path)
93
+ print "#{path} doesn't exist. Create it? [Yn]: "
94
+ response = gets.chomp.downcase
95
+
96
+ if !['yes', 'y', ''].include?(response)
97
+ create_dir = false
98
+ end
99
+ end
100
+
101
+ if create_dir
102
+ FileUtils.mkdir_p(path)
103
+
104
+ config_path = File.expand_path('~/.learn-config')
105
+ existing_editor = YAML.load(File.read(config_path))[:editor]
106
+ data = YAML.dump({ learn_directory: path, editor: existing_editor })
107
+
108
+ File.write(config_path, data)
109
+ else
110
+ set_directory!
111
+ end
112
+ end
113
+
114
+ def new_directory_exists?(path)
115
+ File.exists?(path)
116
+ end
117
+
40
118
  def confirm_and_reset!
41
119
  if confirm_reset?
42
120
  netrc.delete!(machine: 'learn-config')
@@ -62,6 +140,63 @@ module LearnConfig
62
140
  setup_flatiron_push_config_machine
63
141
  end
64
142
 
143
+ def setup_learn_directory
144
+ if !config_file?
145
+ write_default_config!
146
+ end
147
+ end
148
+
149
+ def setup_editor
150
+ if !config_file?
151
+ write_defalt_config!
152
+ end
153
+ end
154
+
155
+ def config_file?
156
+ path = File.expand_path('~/.learn-config')
157
+ File.exists?(path) && has_yaml?(path)
158
+ end
159
+
160
+ def has_yaml?(file_path)
161
+ !!YAML.load(File.read(file_path)) && valid_config_yaml?(file_path)
162
+ end
163
+
164
+ def valid_config_yaml?(path)
165
+ yaml = YAML.load(File.read(path))
166
+ dir = !!yaml[:learn_directory]
167
+ dir_path = yaml[:learn_directory]
168
+ dir_exists = dir && File.exists?(dir_path)
169
+ editor = !!yaml[:editor]
170
+
171
+ if !dir_exists
172
+ puts "It seems like your Learn directory isn't quite right. Let's fix that."
173
+ set_directory!
174
+ dir_exists = true
175
+ end
176
+
177
+ dir && dir_exists && editor
178
+ end
179
+
180
+ def write_default_config!
181
+ learn_dir = File.expand_path('~/Development/code')
182
+ config_path = File.expand_path('~/.learn-config')
183
+
184
+ ensure_default_dir_exists!(learn_dir)
185
+ ensure_config_file_exists!(config_path)
186
+
187
+ data = YAML.dump({ learn_directory: learn_dir, editor: "subl" })
188
+
189
+ File.write(config_path, data)
190
+ end
191
+
192
+ def ensure_default_dir_exists!(learn_dir)
193
+ FileUtils.mkdir_p(learn_dir)
194
+ end
195
+
196
+ def ensure_config_file_exists!(config_path)
197
+ FileUtils.touch(config_path)
198
+ end
199
+
65
200
  def setup_learn_config_machine
66
201
  login, password = netrc.read
67
202
 
@@ -1,3 +1,3 @@
1
1
  module LearnConfig
2
- VERSION = "0.0.32"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/learn_config.rb CHANGED
@@ -1,3 +1,7 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+ require 'timeout'
4
+
1
5
  require 'learn_config/version'
2
6
  require 'learn_config/setup'
3
7
  require 'learn_config/netrc_interactor'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: learn-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.32
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flatiron School
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-22 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 1.0.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 1.0.0
69
69
  description:
70
70
  email:
71
71
  - learn@flatironschool.com