Class: QiitaPicks::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/qiita_picks/mailer.rb

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ Mailer

Returns a new instance of Mailer.



10
11
12
# File 'lib/qiita_picks/mailer.rb', line 10

def initialize(email, password)
  @gmail = Gmail.new(email, password)
end

Instance Method Details

#deliver(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/qiita_picks/mailer.rb', line 14

def deliver(params={})
  return if params[:add_files].compact.empty?
  @gmail.deliver {
    to      params[:to]
    subject params[:subject]
    body    params[:body]
    params[:add_files].each {|f| add_file(f)}
  }
end


24
25
26
27
28
29
# File 'lib/qiita_picks/mailer.rb', line 24

def weekly_popular(limit=5)
  @gmail.inbox.emails(:unread, from: "[email protected]").last(limit).inject([]) do |data, mail|
    url = mail.text_part.decoded.scan(/^\d+位.+\((http.+?)\)/).flatten
    data << Hash[:subject, mail.subject, :date, mail.date, :url, url]
  end
end

#weekly_stocked(limit = 5) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/qiita_picks/mailer.rb', line 31

def weekly_stocked(limit=5)
  @gmail.inbox.emails(:unread, from: "[email protected]").last(limit).inject([]) do |data, mail|
    next unless /^.+?さんが今週Qiitaでストックした記事$/ =~ mail.subject
    doc = Nokogiri::HTML NKF.nkf('-w', NKF.nkf('-w', mail.body.decoded))
    url = doc.css('.stockedArticlesTable a').inject([]){|list, elem|
      list << elem.attr("href").match(/(^http.+?)\?/)[1]
    }.uniq
    data << Hash[:subject, mail.subject, :date, mail.date, :url, url]
  end
end