class Client

Constants

PATH

Attributes

password[R]
username[R]

Public Class Methods

get_auth_token() click to toggle source
# File lib/gripst/client.rb, line 7
def get_auth_token
  return oauth_from_file if oauth_from_file
  authorizer = Client.new
  authorizer.authorize
end
login_with_oauth(oauth_token) click to toggle source
# File lib/gripst/client.rb, line 13
def login_with_oauth(oauth_token)
  client = Octokit::Client.new :access_token => oauth_token
  client.user.login
  client
rescue Octokit::Unauthorized
  puts '---'
  puts '---'
  puts '---'
  puts 'Unable to create personal access token on github. Is the gripst application already set? https://github.com/settings/tokens'
  exit
end
new() click to toggle source
# File lib/gripst/client.rb, line 35
def initialize
  @username = get_username
  @password = get_password
end

Private Class Methods

oauth_from_file() click to toggle source
# File lib/gripst/client.rb, line 27
def oauth_from_file
  File.open PATH, &:readline if File.exists?(PATH) && !File.zero?(PATH)
end

Public Instance Methods

authorize() click to toggle source
# File lib/gripst/client.rb, line 40
def authorize
  begin
    auth_token = client.create_authorization :scopes => ['gists'], :note => 'gripst'
  rescue Octokit::OneTimePasswordRequired
    otp = get_otp
    auth_token = authorize_with_otp otp
  rescue Octokit::Unauthorized
    puts '---'
    puts '---'
    puts '---'
    puts 'Username or password was incorrect'
    exit
  end

  write_auth_token auth_token
  auth_token
end

Private Instance Methods

authorize_with_otp(token) click to toggle source
# File lib/gripst/client.rb, line 86
def authorize_with_otp(token)
  response = client.create_authorization :scopes => ['gist'],
                                         :note => 'gripst',
                                         :headers => { 'X-GitHub-OTP' => token }
  response[:token]
rescue Octokit::UnprocessableEntity
  puts '---'
  puts '---'
  puts '---'
  puts 'Unable to create personal access token on github. Is the gripst application already set? https://github.com/settings/tokens'
  exit
end
client() click to toggle source
# File lib/gripst/client.rb, line 81
def client
  @client ||= Octokit::Client.new :login => username,
                                  :password => password
end
get_otp() click to toggle source
# File lib/gripst/client.rb, line 75
def get_otp
  puts 'Looks like you have 2FA. Smart move.'
  puts 'Enter the OTP from GitHub'
  STDIN.gets.chomp
end
get_password() click to toggle source
# File lib/gripst/client.rb, line 64
def get_password
  begin
    system "stty -echo"
    puts 'Enter your GitHub password'
    puts '(the input will be hidden)'
    STDIN.gets.chomp
  ensure
    system "stty echo"
  end
end
get_username() click to toggle source
# File lib/gripst/client.rb, line 59
def get_username
  puts 'Enter your GitHub username'
  STDIN.gets.chomp
end
write_auth_token(oauth_token) click to toggle source
# File lib/gripst/client.rb, line 99
def write_auth_token(oauth_token)
  File.new PATH, 'w+' unless File.exists? PATH
  file = File.open PATH, 'w+'
  file << oauth_token
ensure
  file.close
end