Sha256: fe4b4a9b16077662b59bd41fb89ce1dc27c5ba1e0e099c8aeb527a4ec76fee1f

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module ContributionCount
  class Client
    class NoAccountError < StandardError; end
    class NoDataError < StandardError; end

    attr_reader :url
    URL = "https://github.com/users/"
    def initialize(account_name)
      @url = URL + account_name + "/contributions"
    end

    def today
      today = Date.today.to_s
      count_hash[today]
    end

    def contribution_today?
      return false if today == 0
      true
    end

    def yesterday
      yesterday = (Date.today - 1).to_s
      count_hash[yesterday]
    end

    def all
      count_hash
    end

    def date(date)
      return if date.nil?
      count = count_hash[date]
      raise NoDataError if count.nil?
      count
    end

    def contribution?(date_str)
      return false if date(date_str) == 0
      true
    end

    private
      def html
        begin
          Nokogiri::HTML.parse(fetch_html, nil, nil)
        rescue OpenURI::HTTPError
          raise NoAccountError
        end
      end

      def fetch_html
        open(url) do |f|
          charset = f.charset
          f.read
        end
      end

      def count_hash
        html.css('.day').map{|row|
          date = row.attribute('data-date').value
          count = row.attribute('data-count').value.to_i
          [date, count]
        }.to_h
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contribution_count-0.1.3 lib/contribution_count/client.rb
contribution_count-0.1.2 lib/contribution_count/client.rb