Class: USaidWat::Application::Posts

Inherits:
Command
  • Object
show all
Includes:
CountCommand, FilterCommand
Defined in:
lib/usaidwat/commands/posts.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 included from CountCommand

#algorithm, #partition

Methods inherited from Command

inherited, #quit, subclasses

Methods included from Pager

#page

Constructor Details

#initialize(prog) ⇒ Posts

Returns a new instance of Posts.



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

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

    c.command(:log) do |s|
      s.description "Show a user's submitted posts"
      s.option 'oneline', '--oneline', 'Output log in a more compact form'
      s.action do |args, options|
        dispatch_process(:process_log, options, args)
      end
    end

    c.command(:tally) do |s|
      s.description "Tally a user's posts by subreddit"
      s.option 'count', '-c', '--count', 'Sort output by number of comments'
      s.action do |args, options|
        dispatch_process(:process_tally, options, args)
      end
    end
  end
  super
end

Instance Method Details

#process(options, args) ⇒ Object



38
39
40
# File 'lib/usaidwat/commands/posts.rb', line 38

def process(options, args)
  quit "Do you want to tally or log posts?", :usage
end

#process_log(options, args) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/usaidwat/commands/posts.rb', line 42

def process_log(options, args)
  raise ArgumentError.new('You must specify a username') if args.empty?
  oneline = !!options['oneline']
  username = args.shift
  subreddits = args.subreddits

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

  res = filter_entries('posts', redditor, posts, subreddits) >>
        lambda { |r| ensure_entries('posts', redditor, r.value) }

  quit res.value if res.left?
  posts = res.value

  formatter = (oneline ? USaidWat::CLI::CompactPostFormatter : USaidWat::CLI::PostFormatter).new
  ENV['LESS'] = 'RS' if oneline
  page
  posts.each { |p| print formatter.format(p) }
end

#process_tally(options, args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/usaidwat/commands/posts.rb', line 63

def process_tally(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 posts." if redditor.posts.empty?
  partition_data = partition(redditor.posts, 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