Class: ImapGuard::Query

Inherits:
Array
  • Object
show all
Defined in:
lib/imap_guard/query.rb

Overview

Note:

All methods return self so they can be chained.

Query is a neat DSL to help you generate IMAP search queries.

Instance Method Summary collapse

Instance Method Details

#before(date) ⇒ self

Messages whose internal date (disregarding time and timezone) is earlier than the specified date.

Parameters:

  • date

    Depending of its type:

    • [String]: uses it as is

    • [Fixnum]: n days before today

    • [Date]: uses this date

Returns:

  • (self)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/imap_guard/query.rb', line 106

def before(date)
  date = case date
         when String
           date
         when Integer
           (Date.today - date).strftime "%e-%b-%Y"
         when Date
           date.strftime "%e-%b-%Y"
         else
           raise ArgumentError, "#{date.inspect} is invalid."
         end

  self << "BEFORE" << date
end

#cc(string) ⇒ self

Messages that contain the specified string in the envelope structure’s CC field.

Returns:

  • (self)


95
96
97
# File 'lib/imap_guard/query.rb', line 95

def cc(string)
  self << "CC" << string
end

#deletedself

Messages with the \Deleted flag set.

Returns:

  • (self)


33
34
35
# File 'lib/imap_guard/query.rb', line 33

def deleted
  self << "DELETED"
end

#from(string) ⇒ self

Messages that contain the specified string in the envelope structure’s FROM field.

Returns:

  • (self)


81
82
83
# File 'lib/imap_guard/query.rb', line 81

def from(string)
  self << "FROM" << string
end

#not(search_key = nil) ⇒ self

Messages that do not match the specified search key.

Examples:

not.deleted   #=> ["NOT", "DELETED"]
not(:deleted) #=> ["NOT", "DELETED"]

Parameters:

  • search_key (defaults to: nil)

    Optional search key to pass to NOT

Returns:

  • (self)


65
66
67
68
69
# File 'lib/imap_guard/query.rb', line 65

def not(search_key = nil)
  self << "NOT"
  send(search_key) if search_key
  self
end

#or(search_key1 = nil, search_key2 = nil) ⇒ self

Note:

Reverse polish notation is expected, i.e. OR <search-key1> <search-key2>

Messages that match either search key.

Examples:

or.unanswered.unflagged     #=> ["OR", "UNANSWERED", "UNFLAGGED"]
or(:unanswered, :unflagged) #=> ["OR", "UNANSWERED", "UNFLAGGED"]

Parameters:

  • search_key1 (defaults to: nil)

    Optional search key to pass to OR

  • search_key2 (defaults to: nil)

    Optional search key to pass to OR

Returns:

  • (self)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/imap_guard/query.rb', line 46

def or(search_key1 = nil, search_key2 = nil)
  self << "OR"

  if search_key1 && search_key2
    send(search_key1)
    send(search_key2)
  elsif search_key1 || search_key2
    raise ArgumentError, "You must give either zero or two arguments."
  end

  self
end

#seenself

Messages that have the \Seen flag set.

Returns:

  • (self)


9
10
11
# File 'lib/imap_guard/query.rb', line 9

def seen
  self << "SEEN"
end

#subject(string) ⇒ self

Messages that contain the specified string in the envelope structure’s SUBJECT field.

Returns:

  • (self)


74
75
76
# File 'lib/imap_guard/query.rb', line 74

def subject(string)
  self << "SUBJECT" << string
end

#to(string) ⇒ self

Messages that contain the specified string in the envelope structure’s TO field.

Returns:

  • (self)


88
89
90
# File 'lib/imap_guard/query.rb', line 88

def to(string)
  self << "TO" << string
end

#unansweredself

Messages that do not have the \Answered flag set.

Returns:

  • (self)


21
22
23
# File 'lib/imap_guard/query.rb', line 21

def unanswered
  self << "UNANSWERED"
end

#unflaggedself

Messages that do not have the \Flagged flag set.

Returns:

  • (self)


27
28
29
# File 'lib/imap_guard/query.rb', line 27

def unflagged
  self << "UNFLAGGED"
end

#unseenself

Messages that do not have the \Seen flag set.

Returns:

  • (self)


15
16
17
# File 'lib/imap_guard/query.rb', line 15

def unseen
  self << "UNSEEN"
end