├── .gitignore ├── Gemfile ├── README ├── Rakefile ├── bin └── ruby_chopped ├── lib ├── ruby_chopped.rb └── ruby_chopped │ └── version.rb └── ruby_chopped.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Specify your gem's dependencies in ruby_chopped.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | RubyChopped 2 | 3 | Modeled after (but certainly not affiliated with) the Chopped show on the Food Network, this gem delivers you a Ruby project (or basket) with 2 gems (ingredients) chosen at random. Your challenge: Make something cool with the given gems. 4 | 5 | Usage 6 | 7 | gem install ruby_chopped 8 | 9 | then to create a new basket that pull 2 of the 50 most popular gems on rubygems.org 10 | 11 | ruby_chopped --name whyday_basket 12 | 13 | 14 | or to get more than 2 gems 15 | 16 | ruby_chopped -n whyday_basket2 --limit 4 17 | 18 | 19 | or to choose from your locally installed gems instead of rubygems.org 20 | 21 | ruby_chopped -n whyday_basket2 --installed 22 | 23 | 24 | or to view more of the great options 25 | 26 | ruby_chopped 27 | 28 | 29 | Contributors 30 | 31 | markmcspadden 32 | 33 | badboy 34 | agile 35 | smnickerson 36 | jwarzech -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler::GemHelper.install_tasks 3 | -------------------------------------------------------------------------------- /bin/ruby_chopped: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') 4 | require 'ruby_chopped' 5 | require 'optparse' 6 | 7 | @options = {} 8 | parser = OptionParser.new do |o| 9 | # Option: Set Name 10 | o.on('-n', '--name [name]', 'name of project to create') do |n| 11 | @options[:name] = n 12 | end 13 | # Option: Force overwrite 14 | o.on('-f', '--force', 'force overwriting existing project') do 15 | @options[:force] = true 16 | end 17 | o.on('-l', '--limit [limit]', 'number of gems to add to the basket') do |l| 18 | @options[:limit] = l 19 | end 20 | # Option: Select only from locally installed gems 21 | o.on('-i', '--installed', 'only select gems that are locally installed') do 22 | @options[:installed_gems] = true 23 | end 24 | # Option: Select from all gems 25 | o.on('-a', '--all', 'select from all existing gems') do 26 | @options[:all_gems] = true 27 | end 28 | end 29 | parser.parse! 30 | 31 | # Allow for old syntax of ruby_chopped new name 32 | # At some point we'll do away with this entirely 33 | unless @options[:name] 34 | if ARGV.shift == "new" && ARGV.first 35 | @options[:name] = ARGV.first 36 | end 37 | end 38 | 39 | unless @options[:name] 40 | puts parser 41 | exit 1 42 | end 43 | 44 | RubyChopped.create(@options) 45 | -------------------------------------------------------------------------------- /lib/ruby_chopped.rb: -------------------------------------------------------------------------------- 1 | require 'rest-client' 2 | require 'fileutils' 3 | require 'json' 4 | 5 | module RubyChopped 6 | extend self 7 | 8 | def create(opts={}) 9 | 10 | # Set pool of gems to fetch from 11 | if opts[:installed_gems] 12 | @gem_pool = :installed 13 | elsif opts[:all_gems] 14 | @gem_pool = :all 15 | else 16 | @gem_pool = :popular 17 | end 18 | 19 | folder = opts[:name] 20 | if File.exist?(folder) 21 | unless opts[:force] 22 | puts "#{folder} already exists, use -f/--force option to recreate it" 23 | exit 1 24 | end 25 | end 26 | 27 | puts "Creating #{folder} basket..." 28 | FileUtils.mkdir_p("#{folder}/lib") 29 | 30 | File.open("#{folder}/lib/#{folder}.rb", "w") {|f| f.puts "# Where the magic happens!" } 31 | 32 | File.open("#{folder}/Gemfile", "wb+") do |f| 33 | f << RubyChopped.gemfile_string(opts[:limit] || 2) 34 | end 35 | 36 | puts "" 37 | puts "Your basket is ready. Open the Gemfile to see what you'll be working with today." 38 | 39 | puts "" 40 | puts "You'll want to cd into #{folder} and run 'bundle install' first" 41 | puts "Enjoy!" 42 | end 43 | 44 | def gemfile_array(limit) 45 | gas = [] 46 | gas << "source \"http://rubygems.org\"" 47 | gas << "" 48 | 49 | gems = random_gems(limit) 50 | gems.each do |g| 51 | # Fetch detailed information for selected gems 52 | info_json = JSON.parse(RestClient.get("http://rubygems.org/api/v1/gems/#{g}.json", :accepts => :json)) 53 | number = info_json["version"] 54 | summary = info_json["info"].gsub("\n","\n# ") 55 | project_uri = info_json["project_uri"] 56 | documentation_uri = info_json["documentation_uri"] 57 | wiki_uri = info_json["wiki_uri"] 58 | mailing_list_uri = info_json["mailing_list_uri"] 59 | 60 | gas << "# #{g}: #{summary}" 61 | gas << "# - Project page: #{project_uri}" unless project_uri.nil? || project_uri.empty? 62 | gas << "# - Documentation: #{documentation_uri}" unless documentation_uri.nil? || documentation_uri.empty? 63 | gas << "# - Wiki: #{wiki_uri}" unless wiki_uri.nil? || wiki_uri.empty? 64 | gas << "# - Mailing List: #{mailing_list_uri}" unless mailing_list_uri.nil? || mailing_list_uri.empty? 65 | gas << "gem \"#{g}\", \"#{number}\"" 66 | gas << "" 67 | end 68 | 69 | gas << "# ENJOY!" 70 | 71 | gas 72 | end 73 | 74 | def gemfile_string(limit) 75 | gemfile_array(limit).join("\n") 76 | end 77 | 78 | def random_gems(limit) 79 | gems = fetch_gems 80 | gems = pick_gems(gems, limit) 81 | end 82 | 83 | def pick_gems(gems, limit) 84 | limit.to_i.times.collect do 85 | g = gems.delete_at(rand(gems.size)) 86 | 87 | # Skip bundler and rails 88 | if g.match(/bundler|rails/) 89 | pick_gems(gems, 1).first 90 | else 91 | g 92 | end 93 | end 94 | end 95 | 96 | def fetch_gems 97 | case @gem_pool 98 | when :installed 99 | fetch_local_gems 100 | when :all 101 | fetch_all_gems 102 | when :popular 103 | fetch_popular_gems 104 | end 105 | end 106 | 107 | def fetch_all_gems 108 | puts "This might take awhile..." 109 | gems = Array.new 110 | gem_list = `gem list --remote` 111 | 112 | gem_list.split("\n").each { |g| gems << g.split(' ').first } 113 | gems 114 | end 115 | 116 | def fetch_local_gems 117 | gems = Array.new 118 | gem_list = `gem list` 119 | 120 | gem_list.split("\n").each { |g| gems << g.split(' ').first } 121 | gems 122 | end 123 | 124 | def fetch_popular_gems 125 | gems = Array.new 126 | gems_json = JSON.parse(RestClient.get("http://rubygems.org/api/v1/downloads/top.json", :accepts => :json)) 127 | 128 | gems_json["gems"].each { |g| gems << g.first["full_name"].split(/-\d\.\d\.\d/).first } 129 | gems.uniq! 130 | end 131 | end 132 | -------------------------------------------------------------------------------- /lib/ruby_chopped/version.rb: -------------------------------------------------------------------------------- 1 | module RubyChopped 2 | VERSION = "0.0.6" 3 | end 4 | -------------------------------------------------------------------------------- /ruby_chopped.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "ruby_chopped/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "ruby_chopped" 7 | s.version = RubyChopped::VERSION 8 | s.platform = Gem::Platform::RUBY 9 | s.authors = ["Mark McSpadden"] 10 | s.email = ["markmcspadden@gmail.com"] 11 | s.homepage = "" 12 | s.summary = %q{Creates a ruby project with two random gems} 13 | s.description = %q{Creates a ruby project with two random gems from rubygems.org top downloads of the day} 14 | 15 | s.rubyforge_project = "ruby_chopped" 16 | 17 | s.files = `git ls-files`.split("\n") 18 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 19 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 20 | s.require_paths = ["lib"] 21 | 22 | s.add_dependency 'rest-client', '1.6.3' 23 | end 24 | --------------------------------------------------------------------------------