Skip to content

Instantly share code, notes, and snippets.

@arashm
Created August 5, 2021 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arashm/be888d91e1b0560e13a3fa98edce8570 to your computer and use it in GitHub Desktop.
Save arashm/be888d91e1b0560e13a3fa98edce8570 to your computer and use it in GitHub Desktop.
Fetch gems info from rubygems.org and pretty print it to terminal
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'rest-client'
gem 'colorize'
end
require 'rest-client'
require 'colorize'
require 'date'
require 'json'
class GemInfo
def initialize(args)
@endpoint = 'https://rubygems.org/api/v1/'
@args = args
end
# Iterate over provided GEM names
#
def search
@args.each do |gem|
begin
console_print get_info(gem)
rescue RestClient::ResourceNotFound
puts "No gem called \"#{gem.colorize(:red)}\" found.\n\n"
next
end
end
end
# Get GEMs information through rubygems.com API
#
def get_info(gem)
thrs = []
thrs << Thread.new { @general = JSON.parse RestClient.get @endpoint + 'gems/' + gem + '.json' }
thrs << Thread.new { @versions = JSON.parse RestClient.get @endpoint + 'versions/' + gem + '.json' }
thrs.each(&:join)
{ general: @general, versions: @versions }
end
# Pretty print information from GEMs
#
def console_print(data)
puts [
"#{data[:general]['name'].colorize(:yellow).bold} - #{data[:general]['version'].colorize(:green)} (#{Date.parse(data[:versions].first['built_at']).strftime('%d %B %Y').colorize(:green)}):",
word_wrap(data[:general]['info']).to_s,
"\t[#{data[:general]['source_code_uri'] || data[:general]['homepage_uri']}]"
].join("\n")
puts "\n"
end
# Wrap long lines and preappend tabs
#
def word_wrap(text, options = {})
line_width = options.fetch(:line_width, 80)
line = text.split("\n").map(&:strip).join(' ')
line.length > line_width ? "\t" + line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\t\\1\n").strip : "\t" + line
end
end
GemInfo.new(ARGV).search
@arashm
Copy link
Author

arashm commented Aug 5, 2021

─$ ./geminfo.rb rails sidekiq
rails - 6.1.4 (24 June 2021):
	Ruby on Rails is a full-stack web framework optimized for programmer happiness
	and sustainable productivity. It encourages beautiful code by favoring
	convention over configuration.
	[https://github.com/rails/rails/tree/v6.1.4]

sidekiq - 6.2.1 (07 April 2021):
	Simple, efficient background processing for Ruby.
	[https://github.com/mperham/sidekiq]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment