fis 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FIS
4
+ module CLI
5
+ module Commands
6
+ # `auth whoami` - Requests details about the authenitcated user
7
+ class Whoami < Command
8
+ def execute
9
+ user = FIS.client.user.me
10
+
11
+ FIS.ui.newline
12
+
13
+ FIS.ui.ok 'Account Information:'
14
+ FIS.ui.newline
15
+ FIS.ui.print_table([
16
+ ['Name:', user.full_name],
17
+ ['Email:', user.email],
18
+ ['Username:', user.username],
19
+ ['Flatiron UUID:', user.learn_uuid],
20
+ ['GitHub User ID:', user.github_uid.to_s] # .to_s for appropriate formatting
21
+ ])
22
+
23
+ FIS.ui.newline
24
+
25
+ FIS.ui.ok 'Account Status:'
26
+ FIS.ui.newline
27
+ FIS.ui.print_table([
28
+ ['Verified?', user.learn_verified_user],
29
+ ['Can Start Working?', user.can_start_working]
30
+ ])
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FIS
4
+ module Client
5
+ class Base
6
+ BASE_URL = ENV.fetch('FIS_CLIENT_BASE_URL', 'https://portal.flatironschool.com')
7
+ API_ROOT = '/api/v1'
8
+
9
+ CLIENT_ERROR_STATUSES = [400, 401, 403, 407, 422].freeze
10
+ SERVER_ERROR_STATUSES = (500..511).to_a.freeze
11
+
12
+ ERROR_STATUSES = (CLIENT_ERROR_STATUSES + SERVER_ERROR_STATUSES).freeze
13
+
14
+ attr_reader :client
15
+
16
+ def initialize(token:)
17
+ url = [BASE_URL, API_ROOT].join
18
+
19
+ @client = Faraday.new(url: url) do |conn|
20
+ conn.authorization :Bearer, token
21
+
22
+ conn.response :json,
23
+ content_type: 'application/json',
24
+ parser_options: {
25
+ object_class: OpenStruct
26
+ }
27
+
28
+ conn.request :retry,
29
+ max: 3,
30
+ interval: 2,
31
+ interval_randomness: 0.5,
32
+ backoff_factor: 2,
33
+ methods: Faraday::Request::Retry::IDEMPOTENT_METHODS + [:post],
34
+ retry_statuses: ERROR_STATUSES,
35
+ exceptions: Faraday::Request::Retry::DEFAULT_EXCEPTIONS
36
+
37
+ conn.adapter Faraday.default_adapter
38
+ end
39
+ end
40
+
41
+ def valid_token?
42
+ !!user.data
43
+ end
44
+
45
+ def user
46
+ @user ||= User.new(base: self)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FIS
4
+ module Client
5
+ class User
6
+ def initialize(base:)
7
+ @base = base
8
+ end
9
+
10
+ def me
11
+ @base.client.get('users/me').body
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FIS
4
+ # Inheritable helpers for common command tasks
5
+ class Command
6
+ extend Forwardable
7
+
8
+ def_delegators :command, :run
9
+
10
+ # Execute this command
11
+ #
12
+ # @api public
13
+ def execute(*)
14
+ raise(
15
+ NotImplementedError,
16
+ "#{self.class}##{__method__} must be implemented"
17
+ )
18
+ end
19
+
20
+ # The external commands runner
21
+ #
22
+ # @see http://www.rubydoc.info/gems/tty-command
23
+ #
24
+ # @api public
25
+ def command(**options)
26
+ require 'tty-command'
27
+ TTY::Command.new(options)
28
+ end
29
+
30
+ # The cursor movement
31
+ #
32
+ # @see http://www.rubydoc.info/gems/tty-cursor
33
+ #
34
+ # @api public
35
+ def cursor
36
+ require 'tty-cursor'
37
+ TTY::Cursor
38
+ end
39
+
40
+ # Open a file or text in the user's preferred editor
41
+ #
42
+ # @see http://www.rubydoc.info/gems/tty-editor
43
+ #
44
+ # @api public
45
+ def editor
46
+ require 'tty-editor'
47
+ TTY::Editor
48
+ end
49
+
50
+ # File manipulation utility methods
51
+ #
52
+ # @see http://www.rubydoc.info/gems/tty-file
53
+ #
54
+ # @api public
55
+ def generator
56
+ require 'tty-file'
57
+ TTY::File
58
+ end
59
+
60
+ # Terminal output paging
61
+ #
62
+ # @see http://www.rubydoc.info/gems/tty-pager
63
+ #
64
+ # @api public
65
+ def pager(**options)
66
+ require 'tty-pager'
67
+ TTY::Pager.new(options)
68
+ end
69
+
70
+ # Terminal platform and OS properties
71
+ #
72
+ # @see http://www.rubydoc.info/gems/tty-pager
73
+ #
74
+ # @api public
75
+ def platform
76
+ require 'tty-platform'
77
+ TTY::Platform.new
78
+ end
79
+
80
+ # The interactive prompt
81
+ #
82
+ # @see http://www.rubydoc.info/gems/tty-prompt
83
+ #
84
+ # @api public
85
+ def prompt(**options)
86
+ require 'tty-prompt'
87
+ TTY::Prompt.new(options)
88
+ end
89
+
90
+ # Get terminal screen properties
91
+ #
92
+ # @see http://www.rubydoc.info/gems/tty-screen
93
+ #
94
+ # @api public
95
+ def screen
96
+ require 'tty-screen'
97
+ TTY::Screen
98
+ end
99
+
100
+ # The unix which utility
101
+ #
102
+ # @see http://www.rubydoc.info/gems/tty-which
103
+ #
104
+ # @api public
105
+ def which(*args)
106
+ require 'tty-which'
107
+ TTY::Which.which(*args)
108
+ end
109
+
110
+ # Check if executable exists
111
+ #
112
+ # @see http://www.rubydoc.info/gems/tty-which
113
+ #
114
+ # @api public
115
+ def exec_exist?(*args)
116
+ require 'tty-which'
117
+ TTY::Which.exist?(*args)
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module FIS
6
+ # Handles the global configuration of the gem via ~/.flatiron-school/configuration.yml
7
+ class Configuration
8
+ attr_reader :file_path, :file_name, :file_full_path, :config
9
+
10
+ def initialize
11
+ @file_path = File.join(Dir.home, '.flatiron-school')
12
+ @file_name = 'configuration.yml'
13
+ @file_full_path = File.join(@file_path, @file_name)
14
+
15
+ @config = read
16
+ end
17
+
18
+ def fetch(*keys)
19
+ key = keys.pop
20
+ keys.inject(@config, :fetch)[key]
21
+ end
22
+
23
+ def set(*keys)
24
+ key = keys.pop
25
+ keys.inject(@config, :fetch)[key] = yield
26
+
27
+ write!
28
+ end
29
+
30
+ def unset(*keys)
31
+ set(*keys) do
32
+ key = keys.pop
33
+ keys.inject(default, :fetch)[key]
34
+ end
35
+ end
36
+
37
+ def read
38
+ return default unless File.exist?(@file_full_path)
39
+
40
+ deep_merge(
41
+ default,
42
+ YAML.safe_load(
43
+ File.open(@file_full_path).read,
44
+ [Symbol]
45
+ )
46
+ )
47
+ end
48
+
49
+ def reread
50
+ @config = read
51
+ end
52
+
53
+ def write!
54
+ Dir.mkdir(@file_path, 0o700) unless Dir.exist?(@file_path)
55
+
56
+ File.open(@file_full_path, 'w') do |file|
57
+ yaml = deep_merge(default, @config).to_yaml
58
+ file.write(yaml)
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def default
65
+ {
66
+ identity: {
67
+ portal: {
68
+ token: nil
69
+ },
70
+ github: {
71
+ id: nil,
72
+ username: nil
73
+ }
74
+ }
75
+ }
76
+ end
77
+
78
+ def deep_merge(this_hash, other_hash)
79
+ this_hash.merge(other_hash) do |_key, this_val, other_val|
80
+ if this_val.is_a?(::Hash) && other_val.is_a?(::Hash)
81
+ deep_merge(this_val, other_val)
82
+ else
83
+ other_val
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module FIS
6
+ # Class responsible for displaying different level information
7
+ class UI
8
+ extend Forwardable
9
+
10
+ def_delegators :@prompt, :ok, :ask, :mask, :suggest
11
+
12
+ def initialize(prompt)
13
+ @prompt = prompt
14
+ @debug = ENV['DEBUG']
15
+ @shell = Thor::Shell::Basic.new
16
+ end
17
+
18
+ def newline(num = 1)
19
+ @prompt.say("\n" * num)
20
+ end
21
+
22
+ def info(message)
23
+ @prompt.say(message)
24
+ end
25
+
26
+ def warn(message)
27
+ @prompt.warn(message)
28
+ end
29
+
30
+ def error(message)
31
+ @prompt.error(message)
32
+ end
33
+
34
+ def debug(error, newline = nil)
35
+ return unless @debug
36
+
37
+ message = ["#{error.class}: #{error.message}", *error.backtrace]
38
+ @prompt.say(newline ? message.join("\n") : message)
39
+ end
40
+
41
+ def debug!
42
+ @debug = true
43
+ end
44
+
45
+ def print_table(table, options = {})
46
+ @shell.print_table(table, options)
47
+ end
48
+
49
+ def print_link(title, url)
50
+ @prompt.say(format_link(title, url))
51
+ end
52
+
53
+ def format_link(title, url)
54
+ TTY::Link.link_to(title, url)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FIS
4
+ VERSION = '0.1.1'
5
+ end
metadata ADDED
@@ -0,0 +1,221 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Tom Milewski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: oauth2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.4.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: thin
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.7.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.7.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: thor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: tty-config
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.4'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: tty-link
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.1'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.1'
125
+ - !ruby/object:Gem::Dependency
126
+ name: tty-prompt
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.22'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.22'
139
+ - !ruby/object:Gem::Dependency
140
+ name: zeitwerk
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 2.4.0
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 2.4.0
153
+ description: The command line interface to Flatiron School services.
154
+ email:
155
+ - tmilewski@gmail.com
156
+ executables:
157
+ - fis
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".envrc"
162
+ - ".envrc.example"
163
+ - ".gitignore"
164
+ - ".rspec"
165
+ - ".rubocop.yml"
166
+ - ".ruby-version"
167
+ - ".travis.yml"
168
+ - CODE_OF_CONDUCT.md
169
+ - Gemfile
170
+ - Gemfile.lock
171
+ - LICENSE.txt
172
+ - README.md
173
+ - Rakefile
174
+ - bin/console
175
+ - bin/setup
176
+ - cucumber.yml
177
+ - exe/fis
178
+ - fis.gemspec
179
+ - lib/fis.rb
180
+ - lib/fis/auth.rb
181
+ - lib/fis/auth/client.rb
182
+ - lib/fis/auth/runner.rb
183
+ - lib/fis/auth/server.rb
184
+ - lib/fis/cli.rb
185
+ - lib/fis/cli/auth.rb
186
+ - lib/fis/cli/commands/login.rb
187
+ - lib/fis/cli/commands/logout.rb
188
+ - lib/fis/cli/commands/whoami.rb
189
+ - lib/fis/client/base.rb
190
+ - lib/fis/client/user.rb
191
+ - lib/fis/command.rb
192
+ - lib/fis/configuration.rb
193
+ - lib/fis/ui.rb
194
+ - lib/fis/version.rb
195
+ homepage: https://portal.flatironschool.com
196
+ licenses:
197
+ - MIT
198
+ metadata:
199
+ homepage_uri: https://portal.flatironschool.com
200
+ source_code_uri: https://github.com/tmilewski/fis
201
+ changelog_uri: https://github.com/tmilewski/fis/releases
202
+ post_install_message:
203
+ rdoc_options: []
204
+ require_paths:
205
+ - lib
206
+ required_ruby_version: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - ">="
209
+ - !ruby/object:Gem::Version
210
+ version: 2.6.0
211
+ required_rubygems_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ requirements: []
217
+ rubygems_version: 3.0.3
218
+ signing_key:
219
+ specification_version: 4
220
+ summary: The command line interface to Flatiron School services.
221
+ test_files: []