Sha256: 0f52671be66a3c13ad330a7f7bc1d344de07cb0646d509e9276d5c12169c51bf

Contents?: true

Size: 1.65 KB

Versions: 15

Compression:

Stored size: 1.65 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 at the bottom of 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?(retries=3)
      learn = LearnWeb::Client.new(token: token, silent_output: true)
      begin
        Timeout::timeout(15) do
          learn.valid_token?
        end
      rescue Timeout::Error
        if retries > 0
          puts "There was an error validating your token. Retrying..."
          token_valid?(retries-1)
        else
          puts "There was a problem connecting to Learn. Please try again."
          exit
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
learn-config-1.0.77 lib/learn_config/cli.rb
learn-config-1.0.76 lib/learn_config/cli.rb
learn-config-1.0.75 lib/learn_config/cli.rb
learn-config-1.0.74 lib/learn_config/cli.rb
learn-config-1.0.73 lib/learn_config/cli.rb
learn-config-1.0.72 lib/learn_config/cli.rb
learn-config-1.0.71 lib/learn_config/cli.rb
learn-config-1.0.7 lib/learn_config/cli.rb
learn-config-1.0.6 lib/learn_config/cli.rb
learn-config-1.0.5 lib/learn_config/cli.rb
learn-config-1.0.4 lib/learn_config/cli.rb
learn-config-1.0.3 lib/learn_config/cli.rb
learn-config-1.0.2 lib/learn_config/cli.rb
learn-config-1.0.1 lib/learn_config/cli.rb
learn-config-1.0.0 lib/learn_config/cli.rb