Class: USaidWat::Application::Timeline

Inherits:
Command
  • Object
show all
Defined in:
lib/usaidwat/commands/timeline.rb

Instance Attribute Summary

Attributes inherited from Command

#client

Instance Method Summary collapse

Methods inherited from Command

inherited, #quit, subclasses

Methods included from Pager

#page

Constructor Details

#initialize(prog) ⇒ Timeline

Returns a new instance of Timeline.



4
5
6
7
8
9
10
11
# File 'lib/usaidwat/commands/timeline.rb', line 4

def initialize(prog)
  prog.command(:timeline) do |c|
    c.action do |args, options|
      dispatch_process(:process, options, args)
    end
  end
  super
end

Instance Method Details

#process(options, args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/usaidwat/commands/timeline.rb', line 13

def process(options, args)
  raise ArgumentError.new('You must specify a username') if args.empty?
  username = args.shift

  redditor = client.new(username)
  comments = redditor.comments

  quit "#{username} has no comments." if comments.empty?

  buckets = comments_by_days_and_hours(comments)
  times = Array.new(7) { Array.new(24, 0) }
  buckets.each do |v|
    d, h = v
    times[d][h] += 1
  end

  formatter = USaidWat::CLI::TimelineFormatter.new
  puts formatter.format(times)
rescue USaidWat::Client::NoSuchUserError
  quit "No such user: #{username}", :no_such_user
end