<commit_before>#!/usr/bin/env ruby

class GitStatTool
  require 'english'
  require 'highline/import'
  require 'octokit'
  require 'awesome_print'
  require 'json'

  USER_AUTH_ERROR      = "Authentication failed."
  GET_REPO_FAILURE_MSG = "An error occurred while trying to get repos. Exiting."

  def go(options)
    if options[:username]
      username = options[:username]
    else
      username = get_username
    end
    if options[:password]
      password = options[:password]
    else
      password = <PASSWORD>
    end
    @User  = init_user(username, password)
    @Repos = get_repos(@User)
    print_welcome
  end

  def get_username
    # TODO find out real username requirements
    username = ask("Github login: ") { |un| un.validate = /^[\w]{4,20}$/ }
  end

  def get_user_password
    #TODO find out real password requirements
    password      = ask("Enter your password:  ") { |pw|
      pw.echo     = false
      pw.validate = /^[\w\W]{3,20}$/
    }
  end

  # Creates and authorizes the GitHub user
  #
  # == Parameters:
  # username::
  #   user provided GitHub username
  # password::
  #   user provided GitHub password
  # == Returns:
  #  returns a Sawyer::Resource object
  def init_user(username, password)
    new_user = Octokit::Client.new(:login => username, :password => password)
    begin
      new_user.user
    rescue Exception => e
      abort USER_AUTH_ERROR
    end
    return new_user
  end

  def print_welcome
    puts "Welcome to the GitHub repository statistics tool. \nYou currently have #{@Repos.size} repositories."
  end

  # Get a list of user repositories
  #
  # == Parameters:
  #  user::
  #    user Sawyer::Resource object
  # == Returns:
  # return an array of the user's repositories
  def get_repos(user)
    begin
      repos    = Array.new
      response = user.repositories
      # return an array of hashes, get just the names
      response.each { |repo|
        repos.push(repo["name"])
      }
      return repos
    rescue Exception => e
      abort GET_REPO_FAILURE_MSG
    end
  end

end

GitStatTool.new
<commit_msg>added: print repos upon auth fo user<commit_after>#!/usr/bin/env ruby

class GitStatTool
  require 'english'
  require 'highline/import'
  require 'octokit'
  require 'awesome_print'
  require 'json'

  USER_AUTH_ERROR      = "Authentication failed."
  GET_REPO_FAILURE_MSG = "An error occurred while trying to get repos. Exiting."

  ft = HighLine::ColorScheme.new do |cs|
    cs[:headline]        = [ :bold, :red ]
    cs[:horizontal_line] = [ :bold, :white ]
    cs[:blue]            = [ :blue ]
    cs[:even_row]        = [ :red ]
    cs[:odd_row]         = [ :green ]
    cs[:bold]            = [ :bold ]
  end
  HighLine.color_scheme = ft

  def go(options)
    if options[:username]
      username = options[:username]
    else
      username = get_username
    end
    if options[:password]
      password = options[:password]
    else
      password = <PASSWORD>
    end
    @User  = init_user(username, password)
    @Repos = get_repos(@User)
    print_repos
  end

  def get_username
    # TODO find out real username requirements
    username = ask("Github login: ") { |un| un.validate = /^[\w]{4,20}$/ }
  end

  def get_user_password
    #TODO find out real password requirements
    password      = ask("Enter your password:  ") { |pw|
      pw.echo     = false
      pw.validate = /^[\w\W]{3,20}$/
    }
  end

  # Creates and authorizes the GitHub user
  #
  # == Parameters:
  # username::
  #   user provided GitHub username
  # password::
  #   user provided GitHub password
  # == Returns:
  #  returns a Sawyer::Resource object
  def init_user(username, password)
    new_user = Octokit::Client.new(:login => username, :password => password)
    begin
      new_user.user
    rescue Exception => e
      abort USER_AUTH_ERROR
    end
    return new_user
  end

  # Prints a welcome statement along with a list of the user's 
  # repositories, date created and last pushed.
  def print_repos
    say("<%= color('Welcome to the GitHub repository statistics tool.', :headline) %>")
    say("<%= color('                  Your Repositories, total: #{@Repos.size}\n', :bold) %>")
    say("<%= color('  name                    date dreated             last pushed       \n', :underscore) %>")
    @Repos.each { |repo|
      spaces = 24 - repo[:name].length
      print " #{repo[:name]}"
      printf( "%-#{spaces}s", ''   )
      spaces = 24 - repo[:created_at].strftime("%d %B, %Y").length
      print "#{repo[:created_at].strftime("%d %B, %Y")}"
      printf( "%-#{spaces}s", ''   )
      puts "#{repo[:last_pushed].strftime("%d %B, %Y")}"
    }
  end

  # Get a list of user repositories, sorted buy
  # the date & time of creation
  #
  # == Parameters:
  #  user::
  #    user Sawyer::Resource object
  # == Returns:
  # return an array of the user's repositories
  def get_repos(user)
    repos   = Array.new
    response = user.repositories
    begin
      response.each {  |repo|
        # time.strftime("%Y-%m-%d %H:%M:%S")
        repo_hash = {:name => repo["name"],
                     :created_at => repo["created_at"],
                     :last_pushed => repo["pushed_at"] }
        repos.push(repo_hash)
      }
      return repos.sort_by { |repo|  repo[:created_at] }
    rescue Exception => e
      abort GET_REPO_FAILURE_MSG
    end
  end

end

{'fraction_non_alphanumeric': 0.11924725172349543, 'fraction_numerical': 0.002981181293087386, 'mean_word_length': 2.5062050947093404, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5590151', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1679, 'n_words': 470}