Class: USaidWat::Application::Log

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

Instance Attribute Summary

Attributes inherited from Command

#client

Instance Method Summary collapse

Methods included from FilterCommand

#ensure_entries, #filter_entries, #grep_entries, #limit_entries

Methods inherited from Command

inherited, #quit, subclasses

Methods included from Pager

#page

Constructor Details

#initialize(prog) ⇒ Log

Returns a new instance of Log.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/usaidwat/commands/log.rb', line 12

def initialize(prog)
  prog.command(:log) do |c|
    c.alias :l
    c.option 'date', '--date FORMAT', 'Show dates in "absolute" or "relative" format'
    c.option 'grep', '--grep STRING', 'Show only comments matching STRING'
    c.option 'limit', '--limit LIMIT', '-n LIMIT', 'Only show n comments'
    c.option 'oneline', '--oneline', 'Output log in a more compact form'
    c.option 'raw', '--raw', 'Print raw comment bodies'

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

Instance Method Details

#process(options, args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/usaidwat/commands/log.rb', line 28

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

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

  res = filter_entries('comments', redditor, comments, subreddits) >>
        lambda { |r| grep_entries('comments', redditor, r.value, options['grep']) } >>
        lambda { |r| limit_entries('comments', redditor, r.value, options['limit']) } >>
        lambda { |r| ensure_entries('comments', redditor, r.value) }

  quit res.value if res.left?

  opts = {
    :date_format => (options['date'] || :relative).to_sym,
    :oneline => !options['oneline'].nil?,
    :pattern => options['grep'],
    :raw => !options['raw'].nil?,
  }
  list_comments(res.value, opts)
rescue USaidWat::Client::NoSuchUserError
  quit "No such user: #{username}", :no_such_user
end