Class: Gem::Commands::StatsCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems/commands/stats_command.rb

Instance Method Summary collapse

Constructor Details

#initializeStatsCommand

Returns a new instance of StatsCommand.



5
6
7
# File 'lib/rubygems/commands/stats_command.rb', line 5

def initialize
  super 'stats', 'View authors, dependencies, etc for rubygems'
end

Instance Method Details

#argumentsObject



9
10
11
# File 'lib/rubygems/commands/stats_command.rb', line 9

def arguments
  '[gem name]'
end

#executeObject



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
73
# File 'lib/rubygems/commands/stats_command.rb', line 47

def execute
  begin
    gem = get_one_gem_name
  rescue Gem::CommandLineError
    puts 'Please specify a gem name (e.g. `gem stats sinatra`).'
    exit
  end

  stats = get_stats(gem)

  puts "#{stats['name']} by #{stats['authors']}"
  puts "\e[34m#{stats['project_uri']}\e[0m\n\n"

  puts "Total Downloads   \e[32m#{stats['downloads'].to_s}\e[0m"
  puts "Version Downloads \e[32m#{stats['version_downloads'].to_s}\e[0m"
  puts "Current Version   \e[32m#{stats['version']}\e[0m"

  unless stats['dependencies']['runtime'].empty?
    puts "\nRuntime Dependencies"
    sexify(stats['dependencies']['runtime'])
  end

  unless stats['dependencies']['development'].empty?
    puts "\nDevelopment Dependencies"
    sexify(stats['dependencies']['development'])
  end
end

#get_stats(gem) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/rubygems/commands/stats_command.rb', line 17

def get_stats gem
  begin
    open "http://rubygems.org/api/v1/gems/#{gem}.json" do |results|
      JSON.parse(results.read)
    end
  rescue OpenURI::HTTPError
    puts "Whoops, seems as if that gem doesn't exist!"
    exit
  end
end

#sexify(dependencies) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubygems/commands/stats_command.rb', line 28

def sexify dependencies
  lengths = []
  dependencies.each do |dependency|
    lengths.push(dependency['name'].length)
  end

  dependencies.each do |dependency|
    name   = dependency['name']
    req    = dependency['requirements']
    space  = ' '
    
    (lengths.max - name.length).times do |x|
      space += ' '
    end

    puts "#{name + space}\e[33m#{req}\e[0m"
  end
end

#usageObject



13
14
15
# File 'lib/rubygems/commands/stats_command.rb', line 13

def usage
  "#{program_name} [gem-name]"
end