#!

路上に就職!

githubのcontribute数をCLIに表示するコマンドを作った。

githubのcontribute数をCLIに表示するコマンドを作成しました。
以下は自分のアカウントの1年のcontribute数です。全然少ない…
f:id:x6d61:20210113171420p:plain gem化してrubygem.orgで公開してます。

rubygems.org また、コードは以下のリポジトリに置いています。 github.com

簡単に説明するとgithubのcontribute数をスクレイピングして色を指定している部分を抜き出しています。

    html = URI(url)
    body = html.read
    body.scan(/<rect.*--color-calendar-graph-day-(bg|L1|L2|L3|L4).*>/)

スクレイピングした結果を各カラーに変更しながら2次元配列に追加しています。

    0.step(commit_data.length - 1, 7) do |index|
      oneweek_tile = commit_data.slice(index, 7)
      oneweek_tile.each_with_index do |item, index|
        @tile_table[index] << TILE_COLOR[item.to_sym]
      end
    end

各カラーに変更する処理はRainbowというgemを使いました。

TILE_COLOR = { bg: Rainbow("[]").color(235, 237, 240).background(235, 237, 240),
                 L1: Rainbow("[]").color(155, 233, 168).background(155, 233, 168),
                 L2: Rainbow("[]").color(111, 197, 100).background(111, 197, 100),
                 L3: Rainbow("[]").color(90, 162, 79).background(90, 162, 79),
                 L4: Rainbow("[]").color(59, 111, 58).background(59, 111, 58),
                 dummy: Rainbow("[]").color(0, 0, 0).background(0, 0, 0) }

CLI部分はThorというgemに頼りました。

 class CLI < Thor
        desc "show user","Shows the number of commits in git"
        option "year",aliases:"y",desc: 'Specify the year to display'
        def show(user)
            git_scrap = GitScrap.new(user)
            git_scrap.year = options["year"].to_i if options["year"]
            git_scrap.make_grass_table()
            git_scrap.tile_table.each { |t| puts t.join }
        end
    end

感想

gemを作ってrubygem.orgに上げる手順がわかってよかったです(^o^)