summaryrefslogtreecommitdiff
path: root/fake-service/lib/pixelated_service/search/string_match.rb
blob: f9b323762e2aefb5a495221f4515c04b4a6215ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module PixelatedService
  class Search
    class StringMatch
      attr_reader :str
      def initialize(data, quoted=false)
        @str = data
        @quoted = quoted
        @data = Regexp.new(Regexp.quote(self.match_string), Regexp::IGNORECASE)
        @exact_match = /^#{@data}$/
      end

      def match_string
        if @quoted
          @str[1..-2]
        else
          @str
        end
      end

      def to_s
        "String(#@data)"
      end

      def match_string?(str)
        Array(str).any? { |ff| !!(ff[@data]) }
      end

      def match_exact_string?(str)
        Array(str).any? { |ff| @exact_match.match ff }
      end

      def match?(mail)
        match_string? [mail.to, mail.from, mail.subject, mail.body]
      end
    end
  end
end