rails-contributors icon indicating copy to clipboard operation
rails-contributors copied to clipboard

Contributors API

Open zzak opened this issue 2 years ago • 0 comments

It would be nice if I could fetch the total number of contributors in a time window.

Currently, I'm parsing using Nokogiri, but I guess that's fine.

Sample code fwiw:

class Contributors
  attr_accessor :body, :start_date, :end_date, :total
  def initialize(start_date, end_date)
    @start_date = start_date.strftime("%Y%m%d")
    @end_date = end_date.strftime("%Y%m%d")
    @body = fetch
    @total = extract_total
  end

  def url
    "https://contributors.rubyonrails.org/contributors/in-time-window/#{@start_date}-#{@end_date}"
  end

  def fetch
    uri = URI.parse(url)
    body = uri.open.read
    return Nokogiri::HTML(body)
  end

  def extract_total
    xpath = "//span[@class=\"listing-total\"][1]"
    text = @body.xpath(xpath.to_s).first.content
    return text.match(/\d+/)
  end
end

zzak avatar Feb 02 '23 10:02 zzak