Class: USaidWat::Application::Tally

Inherits:
Command
  • Object
show all
Includes:
CountCommand
Defined in:
lib/usaidwat/commands/tally.rb

Instance Attribute Summary

Attributes inherited from Command

#client

Instance Method Summary collapse

Methods included from CountCommand

#algorithm, #partition

Methods inherited from Command

inherited, #quit, subclasses

Methods included from Pager

#page

Constructor Details

#initialize(prog) ⇒ Tally

Returns a new instance of Tally.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/usaidwat/commands/tally.rb', line 10

def initialize(prog)
  prog.command(:tally) do |c|
    c.alias :t
    c.option 'count', '-c', '--count', 'Sort output by number of comments'

    c.action do |args, options|
      process(options, args)
    end
  end
  super
end

Instance Method Details

#process(options, args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/usaidwat/commands/tally.rb', line 22

def process(options, args)
  raise ArgumentError.new('You must specify a username') if args.empty?
  raise ArgumentError.new('You cannot specify a subreddit when tallying comments') if args.count > 1
  username = args.first

  redditor = client.new(username)
  quit "#{redditor.username} has no comments." if redditor.comments.empty?
  partition_data = partition(redditor.comments, options['count'])
  formatter = USaidWat::CLI::TallyFormatter.new
  print formatter.format(partition_data)
rescue USaidWat::Client::NoSuchUserError
  quit "No such user: #{username}", :no_such_user
end