learn-config 0.0.1 → 0.0.2

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: 08b0e991046426c1c7e4f595ec6698e8a121886d
4
- data.tar.gz: ce5ffc38573092e556d0247b323644615a00fb73
3
+ metadata.gz: 8c222608abd203ce1ed652817f6ffbc534634c53
4
+ data.tar.gz: 10aba38a717371ce7ac06a98b35706aa8635c45e
5
5
  SHA512:
6
- metadata.gz: 8d2aeccaf9a4c7b5865d7ed00cc5cba6e55c3a8ecb4949375f36ee243b299010ba477069248e69fe121b0212cda125034b3147070423e5e35bb17590b41ddbae
7
- data.tar.gz: 847be184e64465135abf8ae01e451cdc4ed146c01d0cbb6ce375951bfbdb6c8bcfa301431d5077678bb5439ddec913e96ee7cbc2a50771085399687c6e2fdded
6
+ metadata.gz: 4e9fcd0b7a31a44a4ffc2a681f9e38a2e75444e07b7054b8ddcfe542faeb81054c6d174f87355034fc22bec3a9ae859809faf336753e0511226b051f61ffb4a5
7
+ data.tar.gz: a553829f22ecebcc82281f4c033c3eb5384b1663e49bac0e2a1d409fa81f9f4d8716b51df9143f287471611e98e23befc527e2fdda19608cc41ff0246ff76d6b
data/bin/learn-config CHANGED
@@ -2,4 +2,13 @@
2
2
 
3
3
  require 'learn_config'
4
4
 
5
- LearnConfig::Setup.run
5
+ KNOWN_COMMANDS = [
6
+ '--reset',
7
+ '--whoami'
8
+ ]
9
+
10
+ ARGV.map! do |arg|
11
+ KNOWN_COMMANDS.include?(arg) ? arg : nil
12
+ end.compact!
13
+
14
+ LearnConfig::Setup.run(ARGV)
@@ -0,0 +1,47 @@
1
+ module LearnConfig
2
+ class CLI
3
+ attr_reader :github_username
4
+ attr_accessor :token
5
+
6
+ def initialize(github_username)
7
+ @github_username = github_username
8
+ end
9
+
10
+ def ask_for_oauth_token(short_text: false, retries_remaining: 5)
11
+ if !short_text
12
+ puts <<-LONG
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
15
+ page at: https://learn.co/#{github_username ? github_username : 'your-github-username'}.
16
+
17
+ LONG
18
+
19
+ print 'Once you have it, please come back here and paste it in: '
20
+ elsif retries_remaining > 0
21
+ print "Hmm...that token doesn't seem to be correct. Please try again: "
22
+ else
23
+ puts "Sorry, you've tried too many times. Please check your token and try again later."
24
+ exit
25
+ end
26
+
27
+ self.token = gets.chomp
28
+
29
+ verify_token_or_ask_again!(retries_remaining: retries_remaining)
30
+ end
31
+
32
+ private
33
+
34
+ def verify_token_or_ask_again!(retries_remaining:)
35
+ if token_valid?
36
+ token
37
+ else
38
+ ask_for_oauth_token(short_text: true, retries_remaining: retries_remaining - 1)
39
+ end
40
+ end
41
+
42
+ def token_valid?
43
+ learn = LearnConfig::LearnWebInteractor.new(token, silent_output: true)
44
+ learn.valid_token?
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,35 @@
1
+ require 'faraday'
2
+
3
+ module LearnConfig
4
+ class LearnWebInteractor
5
+ attr_reader :token, :conn, :silent_output
6
+
7
+ LEARN_URL = 'https://learn.co'
8
+ API_ROOT = '/api/v1'
9
+
10
+ def initialize(token, silent_output: false)
11
+ @token = token
12
+ @silent_output = silent_output
13
+ @conn = Faraday.new(url: LEARN_URL) do |faraday|
14
+ faraday.adapter Faraday.default_adapter
15
+ end
16
+ end
17
+
18
+ def me_endpoint
19
+ "#{API_ROOT}/users/me"
20
+ end
21
+
22
+ def me
23
+ response = @conn.get do |req|
24
+ req.url me_endpoint
25
+ req.headers['Authorization'] = "Bearer #{token}"
26
+ end
27
+
28
+ LearnConfig::Me.new(response, silent_output: silent_output)
29
+ end
30
+
31
+ def valid_token?
32
+ !!me.data
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,52 @@
1
+ require 'oj'
2
+
3
+ module LearnConfig
4
+ class Me
5
+ attr_accessor :response, :id, :first_name, :last_name, :full_name,
6
+ :username, :email, :github_gravatar, :github_uid, :data,
7
+ :silent_output
8
+
9
+ def initialize(response, silent_output: false)
10
+ @response = response
11
+ @silent_output = silent_output
12
+
13
+ parse!
14
+ end
15
+
16
+ def parse!
17
+ if response.status == 200
18
+ self.data = Oj.load(response.body, symbol_keys: true)
19
+
20
+ populate_attributes!
21
+ elsif silent_output == false
22
+ case response.status
23
+ when 401
24
+ puts "It seems your OAuth token is incorrect. Please re-run config with: learn reset"
25
+ exit
26
+ when 500
27
+ puts "Something went wrong. Please try again."
28
+ exit
29
+ else
30
+ puts "Something went wrong. Please try again."
31
+ exit
32
+ end
33
+ end
34
+
35
+ self
36
+ end
37
+
38
+ private
39
+
40
+ def populate_attributes!
41
+ data.each do |attribute, value|
42
+ if !self.respond_to?(attribute)
43
+ class << self
44
+ attr_accessor attribute
45
+ end
46
+ end
47
+
48
+ self.send("#{attribute}=", value)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,34 @@
1
+ require 'netrc'
2
+
3
+ module LearnConfig
4
+ class NetrcInteractor
5
+ attr_reader :login, :password, :netrc
6
+
7
+ def initialize
8
+ ensure_proper_permissions!
9
+ end
10
+
11
+ def read(machine: 'learn-config')
12
+ @netrc = Netrc.read
13
+ @login, @password = netrc[machine]
14
+ end
15
+
16
+ def write(machine: 'learn-config', new_login:, new_password:)
17
+ netrc[machine] = new_login, new_password
18
+ netrc.save
19
+ end
20
+
21
+ def delete!(machine:)
22
+ @netrc = Netrc.read
23
+
24
+ netrc.delete(machine)
25
+ netrc.save
26
+ end
27
+
28
+ private
29
+
30
+ def ensure_proper_permissions!
31
+ system('chmod 0600 ~/.netrc')
32
+ end
33
+ end
34
+ end
@@ -3,14 +3,98 @@ require 'oj'
3
3
 
4
4
  module LearnConfig
5
5
  class Setup
6
- def self.run
7
- new.run
6
+ attr_reader :netrc, :args, :reset, :whoami
7
+
8
+ def self.run(args)
9
+ new(args).run
8
10
  end
9
11
 
10
- def initialize
12
+ def initialize(args)
13
+ @args = args
14
+ @netrc = LearnConfig::NetrcInteractor.new
15
+ @reset = !!args.include?('--reset')
16
+ @whoami = !!args.include?('--whoami')
11
17
  end
12
18
 
13
19
  def run
20
+ if reset
21
+ args.delete('--reset')
22
+ confirm_and_reset!
23
+ elsif whoami
24
+ args.delete('--whoami')
25
+ whoami?
26
+ else
27
+ setup_netrc
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def whoami?
34
+ _learn, token = netrc.read
35
+ me = LearnConfig::LearnWebInteractor.new(token).me
36
+ puts "Name: #{me.full_name}"
37
+ puts "Username: #{me.username}"
38
+ puts "Email: #{me.email}"
39
+
40
+ exit
41
+ end
42
+
43
+ def confirm_and_reset!
44
+ if confirm_reset?
45
+ netrc.delete!(machine: 'learn-config')
46
+ netrc.delete!(machine: 'flatiron-push')
47
+
48
+ setup_netrc
49
+ end
50
+
51
+ exit
52
+ end
53
+
54
+ def confirm_reset?
55
+ puts "This will remove your existing Learn login configuration and reset.\n"
56
+ print "Are you sure you want to do this? [yN]: "
57
+
58
+ response = gets.chomp.downcase
59
+
60
+ !!(response == 'yes' || response == 'y')
61
+ end
62
+
63
+ def setup_netrc
64
+ setup_learn_config_machine
65
+ setup_flatiron_push_config_machine
66
+ end
67
+
68
+ def setup_learn_config_machine
69
+ login, password = netrc.read
70
+
71
+ if (!login || !password) || !LearnConfig::LearnWebInteractor.new(password, silent_output: true).valid_token?
72
+ github_username, _uid = netrc.read(machine: 'flatiron-push')
73
+ oauth_token = LearnConfig::CLI.new(github_username).ask_for_oauth_token
74
+ netrc.write(new_login: 'learn', new_password: oauth_token)
75
+ end
76
+ end
77
+
78
+ def setup_flatiron_push_config_machine
79
+ learn_login, token = netrc.read(machine: 'learn-config')
80
+
81
+ if (!learn_login || !token) || !LearnConfig::LearnWebInteractor.new(token, silent_output: true).valid_token?
82
+ setup_learn_config_machine
83
+ else
84
+ ensure_correct_push_config(token)
85
+ end
86
+ end
87
+
88
+ def ensure_correct_push_config(token)
89
+ me = LearnConfig::LearnWebInteractor.new(token).me
90
+ github_username = me.username
91
+ github_user_id = me.github_uid
92
+
93
+ netrc.write(
94
+ machine: 'flatiron-push',
95
+ new_login: github_username,
96
+ new_password: github_user_id
97
+ )
14
98
  end
15
99
  end
16
100
  end
@@ -1,3 +1,3 @@
1
1
  module LearnConfig
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/learn_config.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  require 'learn_config/version'
2
2
  require 'learn_config/setup'
3
+ require 'learn_config/netrc_interactor'
4
+ require 'learn_config/learn_web_interactor'
5
+ require 'learn_config/me'
6
+ require 'learn_config/cli'
3
7
 
4
8
  module LearnConfig
5
9
  end
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.1
4
+ version: 0.0.2
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-20 00:00:00.000000000 Z
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,6 +96,10 @@ files:
96
96
  - bin/learn-config
97
97
  - learn-config.gemspec
98
98
  - lib/learn_config.rb
99
+ - lib/learn_config/cli.rb
100
+ - lib/learn_config/learn_web_interactor.rb
101
+ - lib/learn_config/me.rb
102
+ - lib/learn_config/netrc_interactor.rb
99
103
  - lib/learn_config/setup.rb
100
104
  - lib/learn_config/version.rb
101
105
  homepage: https://learn.co