Skip to content

Instantly share code, notes, and snippets.

@ryu39
Last active August 1, 2017 03:10
Show Gist options
  • Save ryu39/a8298ef890c9ebc26ab316c3b9ce1f20 to your computer and use it in GitHub Desktop.
Save ryu39/a8298ef890c9ebc26ab316c3b9ce1f20 to your computer and use it in GitHub Desktop.
require 'uri'
require 'net/http'
require 'json'
require 'ostruct'
require 'link_header' # Please execute 'gem install link_header'
class ApiClient
def initialize(access_token)
@access_token = access_token
end
def get(url)
url = URI.parse(url)
req = Net::HTTP::Get.new(url)
req.add_field("Authorization", "token #{@access_token}")
Net::HTTP.new(url.host, url.port).tap { |http| http.use_ssl = true}.start do |http|
http.request(req)
end
end
end
api_client = ApiClient.new(ENV['GITHUB_API_ACCESS_TOKEN'])
# Search github repositories which uses Ruby and has over 1000 stars.
API_URL = 'https://api.github.com/search/repositories?q=language:ruby%20stars:%3E3000&sort=star&order=desc'
repositories = Enumerator.new do |y|
repos = []
next_url = API_URL
loop do
if repos.empty?
break if next_url.nil?
response = api_client.get(next_url)
repos = JSON.parse(response.body)['items'].map { |item| OpenStruct.new(item) }
next_url = LinkHeader.parse(response['Link']).to_a.detect { |_, attrs| Hash[attrs]['rel'] == 'next' }&.first
end
y << repos.shift
end
end
LIMIT = 10
rails_using_repositories = []
repositories.each do |repo|
gemfile_url = repo.contents_url.sub('{+path}', 'Gemfile')
response = api_client.get(gemfile_url)
next unless response.is_a?(Net::HTTPSuccess)
gemfile_download_url = JSON.parse(response.body)['download_url']
gemfile = api_client.get(gemfile_download_url).body
next unless /gem\s+('rails'|"rails")/.match?(gemfile)
puts repo.name
rails_using_repositories << repo
break if rails_using_repositories.length >= LIMIT
end
p rails_using_repositories.map(&:name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment