Sha256: 623db3aed23e8ce2a562a8a2f4801f7d30159017547d4a1efd7a79a89bc35cb3

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'usaidwat/either'

module USaidWat
  module Application
    module FilterCommand
      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

      def grep_entries(noun, redditor, entries, grep, subreddits = [])
        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}/"
          unless subreddits.empty?
            prefixed_subs = subreddits.map(&:prefix_subreddit)
            msg += " in " + prefixed_subs.or_join
          end
          msg += "."
          USaidWat::Left.new(msg)
        else
          USaidWat::Right.new(entries)
        end
      end

      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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
usaidwat-1.5.1 lib/usaidwat/filter.rb