Module: USaidWat::Application::FilterCommand

Included in:
Log, Posts
Defined in:
lib/usaidwat/filter.rb

Instance Method Summary collapse

Instance Method Details

#ensure_entries(noun, redditor, entries) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/usaidwat/filter.rb', line 33

def ensure_entries(noun, redditor, entries)
  if entries.empty?
    USaidWat::Left.new("#{redditor.username} has no #{noun}.")
  else
    USaidWat::Right.new(entries)
  end
end

#filter_entries(noun, redditor, entries, subreddits) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/usaidwat/filter.rb', line 6

def filter_entries(noun, redditor, entries, subreddits)
  return USaidWat::Right.new(entries) if subreddits.empty?
  entries = entries.find_all { |e| subreddits.include?(e.subreddit.downcase) }
  if entries.empty?
    USaidWat::Left.new("No #{noun} by #{redditor.username} for #{subreddits.join(', ')}.")
  else
    USaidWat::Right.new(entries)
  end
end

#grep_entries(noun, redditor, entries, grep) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/usaidwat/filter.rb', line 16

def grep_entries(noun, redditor, entries, grep)
  return USaidWat::Right.new(entries) if grep.nil?
  entries = entries.select { |e| e.body =~ /#{grep}/i }
  if entries.empty?
    msg = "#{redditor.username} has no #{noun} matching /#{grep}/."
    USaidWat::Left.new(msg)
  else
    USaidWat::Right.new(entries)
  end
end

#limit_entries(noun, redditor, entries, n) ⇒ Object



27
28
29
30
31
# File 'lib/usaidwat/filter.rb', line 27

def limit_entries(noun, redditor, entries, n)
  return USaidWat::Right.new(entries) if n.nil?
  entries = entries[0...n.to_i]
  USaidWat::Right.new(entries)
end