罗泽轩
罗泽轩

Reputation: 1673

How can I list all gems in rubygems.org?

Recently I am diving into a tool about gems.

A feature requires to get the repos' address with given gems list. I want to fetch all gems info in rubygems.org, and store them local, instead of searching them each time. Searching with a list of gem name will cost too much time.

However, I could not find response api in the api page of rubygems.org. There are apis to search with specific gem, or list all gems owned by some guys. But not the one lists all gems info in rubygems.org.

So, how can I list all gems in rubygems.org? Is there any 3-party api to do that?

Upvotes: 0

Views: 671

Answers (1)

spickermann
spickermann

Reputation: 107142

Calling gem search without an argument returns the full list of all gems on RubyGems.org. The URLs on RubyGems follow a simple pattern, therefore, you can start playing around with gem names with a simple class like this:

class GemRepo
  attr_reader :names

  def initialize
    @names = []
    load_names
  end

  def size
    @names.size
  end

  def urls
    @names.map { |name| "https://rubygems.org/gems/#{name}" }
  end

private
  def load_names
    `gem search`.each_line do |line|
      next if line.empty? || line.match(/\*/)
      @names << line.split().first
    end
  end
end

gem_repo = GemRepo.new

gem_repo.size
# => 88679
gem_repo.names
# => ['-', '0mq', ..., 'Zzzzz', 'zzzzzz']
gem_repo.urls
# => ['https://rubygems.org/gems/-', 'https://rubygems.org/gems/0mq', ..., 'https://rubygems.org/gems/Zzzzz', 'https://rubygems.org/gems/zzzzzz']

Upvotes: 2

Related Questions