Sha256: e23e17ff51cba52402730db3ece1c061650a7db61afab1bb1d33801e0ca82c1a

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 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)
        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

      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

4 entries across 4 versions & 1 rubygems

Version Path
usaidwat-1.5.0 lib/usaidwat/filter.rb
usaidwat-1.4.5 lib/usaidwat/filter.rb
usaidwat-1.4.4 lib/usaidwat/filter.rb
usaidwat-1.4.3 lib/usaidwat/filter.rb