Module: RubyChopped

Extended by:
RubyChopped
Included in:
RubyChopped
Defined in:
lib/ruby_chopped.rb,
lib/ruby_chopped/version.rb

Constant Summary collapse

VERSION =
"0.0.6"

Instance Method Summary collapse

Instance Method Details

#create(opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_chopped.rb', line 8

def create(opts={})
  
  # Set pool of gems to fetch from
  if opts[:installed_gems]
    @gem_pool = :installed
  elsif opts[:all_gems]
    @gem_pool = :all
  else
    @gem_pool = :popular
  end
  
  folder = opts[:name]
  if File.exist?(folder)
    unless opts[:force]
      puts "#{folder} already exists, use -f/--force option to recreate it"
      exit 1
    end
  end

  puts "Creating #{folder} basket..."
  FileUtils.mkdir_p("#{folder}/lib")

  File.open("#{folder}/lib/#{folder}.rb", "w") {|f| f.puts "# Where the magic happens!" }

  File.open("#{folder}/Gemfile", "wb+") do |f|
    f << RubyChopped.gemfile_string(opts[:limit] || 2)
  end
  
  puts ""
  puts "Your basket is ready. Open the Gemfile to see what you'll be working with today."

  puts ""
  puts "You'll want to cd into #{folder} and run 'bundle install' first"
  puts "Enjoy!"
end

#fetch_all_gemsObject



107
108
109
110
111
112
113
114
# File 'lib/ruby_chopped.rb', line 107

def fetch_all_gems
  puts "This might take awhile..."
  gems = Array.new
  gem_list = `gem list --remote`

  gem_list.split("\n").each { |g| gems << g.split(' ').first }
  gems
end

#fetch_gemsObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_chopped.rb', line 96

def fetch_gems
  case @gem_pool
  when :installed
    fetch_local_gems
  when :all
    fetch_all_gems
  when :popular
    fetch_popular_gems
  end
end

#fetch_local_gemsObject



116
117
118
119
120
121
122
# File 'lib/ruby_chopped.rb', line 116

def fetch_local_gems
  gems = Array.new
  gem_list = `gem list`
  
  gem_list.split("\n").each { |g| gems << g.split(' ').first }
  gems
end


124
125
126
127
128
129
130
# File 'lib/ruby_chopped.rb', line 124

def fetch_popular_gems
  gems = Array.new
  gems_json = JSON.parse(RestClient.get("http://rubygems.org/api/v1/downloads/top.json", :accepts => :json))
  
  gems_json["gems"].each { |g| gems << g.first["full_name"].split(/-\d\.\d\.\d/).first }
  gems.uniq!
end

#gemfile_array(limit) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_chopped.rb', line 44

def gemfile_array(limit)
  gas = []
  gas << "source \"http://rubygems.org\""
  gas << ""
  
  gems = random_gems(limit)
  gems.each do |g|
    # Fetch detailed information for selected gems
    info_json = JSON.parse(RestClient.get("http://rubygems.org/api/v1/gems/#{g}.json", :accepts => :json))
    number = info_json["version"]
    summary = info_json["info"].gsub("\n","\n# ")
    project_uri = info_json["project_uri"]
    documentation_uri = info_json["documentation_uri"]
    wiki_uri = info_json["wiki_uri"]
    mailing_list_uri = info_json["mailing_list_uri"]
    
    gas << "# #{g}: #{summary}"
    gas << "#    - Project page: #{project_uri}" unless project_uri.nil? || project_uri.empty?
    gas << "#    - Documentation: #{documentation_uri}" unless documentation_uri.nil? || documentation_uri.empty?
    gas << "#    - Wiki: #{wiki_uri}" unless wiki_uri.nil? || wiki_uri.empty?
    gas << "#    - Mailing List: #{mailing_list_uri}" unless mailing_list_uri.nil? || mailing_list_uri.empty?
    gas << "gem \"#{g}\", \"#{number}\""
    gas << ""
  end
  
  gas << "# ENJOY!"
  
  gas
end

#gemfile_string(limit) ⇒ Object



74
75
76
# File 'lib/ruby_chopped.rb', line 74

def gemfile_string(limit)
  gemfile_array(limit).join("\n")
end

#pick_gems(gems, limit) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_chopped.rb', line 83

def pick_gems(gems, limit)
  limit.to_i.times.collect do 
    g = gems.delete_at(rand(gems.size)) 

    # Skip bundler and rails
    if g.match(/bundler|rails/)
      pick_gems(gems, 1).first
    else
      g
    end
  end
end

#random_gems(limit) ⇒ Object



78
79
80
81
# File 'lib/ruby_chopped.rb', line 78

def random_gems(limit)
  gems = fetch_gems
  gems = pick_gems(gems, limit)
end