Sha256: 003348b95df04d87db62dcb5845886538406c4df1afb823d957f83da77a472db

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module LearnConfig
  class CLI
    attr_reader   :github_username
    attr_accessor :token

    def initialize(github_username)
      @github_username = github_username
    end

    def ask_for_oauth_token(short_text: false, retries_remaining: 5)
      if !short_text
        puts <<-LONG
To connect with the Learn web application, you will need to configure
the Learn gem with an OAuth token. You can find yours on your profile
page at: https://learn.co/#{github_username ? github_username : 'your-github-username'}.

        LONG

        print 'Once you have it, please come back here and paste it in: '
      elsif retries_remaining > 0
        print "Hmm...that token doesn't seem to be correct. Please try again: "
      else
        puts "Sorry, you've tried too many times. Please check your token and try again later."
        exit
      end

      self.token = gets.chomp

      verify_token_or_ask_again!(retries_remaining: retries_remaining)
    end

    private

    def verify_token_or_ask_again!(retries_remaining:)
      if token_valid?
        token
      else
        ask_for_oauth_token(short_text: true, retries_remaining: retries_remaining - 1)
      end
    end

    def token_valid?
      learn = LearnWeb::Client.new(token: token, silent_output: true)
      learn.valid_token?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
learn-config-0.0.32 lib/learn_config/cli.rb
learn-config-0.0.31 lib/learn_config/cli.rb
learn-config-0.0.3 lib/learn_config/cli.rb