class GhTrend::Trend

Constants

BASE_URL
DSP_POS

Attributes

repo_desc[R]
repo_nm[R]

Public Class Methods

new(options) click to toggle source
# File lib/gh-trend/trend.rb, line 13
def initialize(options)
  @options = options
  @repo_nm = []
  @repo_url = []
  @repo_desc = []
  @client = GhTrend::GithubApi.new
end

Public Instance Methods

brows?() click to toggle source
# File lib/gh-trend/trend.rb, line 49
def brows?
  return false unless @options[:brows]
  Launchy.open "https://github.com" + @repo_url[@options[:brows].to_i - 1]
  true
end
get_trend() click to toggle source
# File lib/gh-trend/trend.rb, line 69
def get_trend
  doc = Nokogiri::HTML(HTTParty.get(geturl))
  doc.css('li[class="repo-list-item"]').each do |html|
    repo_url = html.css('h3 > a').attribute('href').value
    m = repo_url.match(/\/(.+)\/(.+)/)
    @repo_nm << m[1] + "/" + m[2]
    @repo_url << repo_url
    @repo_desc << html.css('p[class="repo-list-description"]').text.strip
  end
end
show() click to toggle source
# File lib/gh-trend/trend.rb, line 21
def show
  cnt = 1

  title_dsp

  @repo_nm.each do |t|

    if @client.user_authenticated?
      if @client.check_star?(t)
        star_status = "unstar"
      else
        star_status = "star"
      end
    else
      star_status = ""
    end

    puts "#{cnt.to_s}: " + t + " " * make_pos(cnt, t.length) + star_status

    if @options[:desc]
      puts make_spaces(cnt) + @repo_desc[cnt - 1]
    end
    cnt += 1

    break if limit_number?(cnt)
  end
end
star?() click to toggle source
# File lib/gh-trend/trend.rb, line 55
def star?
  return false unless @options[:star]
  @client.star(@repo_nm[@options[:star].to_i - 1])
  @repo_nm[@options[:star].to_i - 1]
  true
end
unstar?() click to toggle source
# File lib/gh-trend/trend.rb, line 62
def unstar?
  return false unless @options[:unstar]
  @client.unstar(@repo_nm[@options[:unstar].to_i - 1])
  @repo_nm[@options[:unstar].to_i - 1]
  true
end

Private Instance Methods

geturl() click to toggle source
# File lib/gh-trend/trend.rb, line 81
def geturl
  @options[:lang] ? BASE_URL + "?l=" + @options[:lang] : BASE_URL
end
limit_number?(n) click to toggle source
# File lib/gh-trend/trend.rb, line 89
def limit_number?(n)
  return false if @options[:num].nil?
  @options[:num].to_i < n
end
make_pos(cnt, len) click to toggle source
# File lib/gh-trend/trend.rb, line 94
def make_pos(cnt, len)
  cnt < 10 ? DSP_POS - (3 + len) : DSP_POS - (4 + len)
end
make_spaces(n) click to toggle source
# File lib/gh-trend/trend.rb, line 85
def make_spaces(n)
  n < 10 ? " " * 3 : " " * 4
end
title_dsp() click to toggle source
# File lib/gh-trend/trend.rb, line 98
def title_dsp
  puts "Trending #{@options[:lang].capitalize} repositories on GitHub today"
  puts "-" * 56
end