Module: Gmail::Base::List::ClassMethods

Defined in:
lib/gmail/base/list.rb

Instance Method Summary collapse

Instance Method Details

#all(filters = {}, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gmail/base/list.rb', line 5

def all(filters={}, opts={})
  max_results = filters[:maxResults] || 100
  opts[:items] ||= []

  if max_results == -1
    filters.merge!({maxResults: 100})
  end
  response = Gmail.request(base_method.send("list"), filters)
  items = response["#{class_name.downcase}s".to_sym] || []
  next_page_token = response[:nextPageToken]
  opts[:items] = opts[:items] + items

  if items.count < 100 || items.count < max_results
    Util.convert_to_gmail_object(opts[:items], class_name.downcase)
  else
    max_results = (max_results == -1)?-1:max_results-items.count
    all(filters.merge({maxResults: max_results, pageToken: next_page_token}), opts)
  end
end

#search(q = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gmail/base/list.rb', line 25

def search(q={})
  if q.is_a? String
    all({q: q})
  else
    query = ""
    [:from, :to, :subject].each do |prop|
      query += "#{prop.to_s}:(#{q[prop].downcase}) "  unless q[prop].nil?
      q.delete(prop)
    end
    [:in, :before, :after].each do |prop|
      query += "#{prop.to_s}:#{q[prop]} " unless q[prop].nil?
      q.delete(prop)
    end

    query += "#{q[:has_words]} " unless q[:has_words].nil?
    query += "-{#{q[:has_not_words]}}" unless q[:has_not_words].nil?
    q.delete(:has_words)
    q.delete(:has_not_words)

    all(q.merge({q: query}))
  end
end