class Feed2Email::Entry

Attributes

last_email_sent_at[RW]
data[RW]
feed_data[RW]
feed_uri[RW]

Public Instance Methods

process() click to toggle source
# File lib/feed2email/entry.rb, line 29
def process
  if missing_data?
    logger.warn 'Skipping entry with missing data...'
    return false
  end

  unless feed.old?
    logger.debug 'Skipping new feed entry...'
    save # record as seen
    return true
  end

  if old?
    logger.debug 'Skipping old entry...'
    return true
  end

  return send_mail
end

Private Instance Methods

apply_send_delay() click to toggle source
# File lib/feed2email/entry.rb, line 51
def apply_send_delay
  return if config['send_delay'] == 0 || config['send_method'] == 'file'

  return if last_email_sent_at.nil?

  secs_since_last_email = Time.now - last_email_sent_at
  secs_to_sleep = config['send_delay'] - secs_since_last_email

  return if secs_to_sleep <= 0

  logger.debug "Sleeping for #{secs_to_sleep} seconds..."
  sleep(secs_to_sleep)
end
author() click to toggle source
# File lib/feed2email/entry.rb, line 65
def author; data.author end
body_html() click to toggle source
# File lib/feed2email/entry.rb, line 67
def body_html
  %{
    <html>
    <body>
    <h1><a href="%{uri}">%{title}</a></h1>
    %{content}
    <p>%{published}</p>
    <p><a href="%{uri}">%{uri}</a></p>
    <p>--<br>
    Sent by <a href="https://github.com/agorf/feed2email">feed2email
    #{VERSION}</a> at #{Time.now}</p>
    </body>
    </html>
  }.gsub(/^\s+/, '') % {
    content:   content,
    published: published_line,
    title:     title.strip_html,
    uri:       uri.escape_html,
  }
end
body_text() click to toggle source
# File lib/feed2email/entry.rb, line 88
def body_text
  body_html.to_markdown
end
build_mail() click to toggle source
# File lib/feed2email/entry.rb, line 92
def build_mail
  Mail.new.tap do |m|
    m.from      = %{"#{feed_title}" <#{config['sender']}>}
    m.to        = config['recipient']
    m.subject   = title.strip_html
    m.html_part = build_mail_part('text/html', body_html)
    m.text_part = build_mail_part('text/plain', body_text)

    m.delivery_method(*delivery_method_params)
  end
end
build_mail_part(content_type, body) click to toggle source
# File lib/feed2email/entry.rb, line 104
def build_mail_part(content_type, body)
  part = Mail::Part.new
  part.content_type = "#{content_type}; charset=UTF-8"
  part.body = body
  part
end
content() click to toggle source
# File lib/feed2email/entry.rb, line 111
def content
  data.content || data.summary
end
delivery_method_params() click to toggle source
# File lib/feed2email/entry.rb, line 115
def delivery_method_params
  case config['send_method']
  when 'file'
    [:file, location: config['mail_path']]
  when 'sendmail'
    [:sendmail, location: config['sendmail_path']]
  when 'smtp'
    [:smtp_connection, connection: Feed2Email.smtp_connection]
  end
end
feed_title() click to toggle source
# File lib/feed2email/entry.rb, line 126
def feed_title; feed_data.title end
last_email_sent_at() click to toggle source
# File lib/feed2email/entry.rb, line 128
def last_email_sent_at; Entry.last_email_sent_at end
last_email_sent_at=(time) click to toggle source
# File lib/feed2email/entry.rb, line 130
def last_email_sent_at=(time)
  Entry.last_email_sent_at = time
end
missing_data?() click to toggle source
# File lib/feed2email/entry.rb, line 134
def missing_data?
  [content, feed_title, title, uri].include?(nil)
end
old?() click to toggle source
# File lib/feed2email/entry.rb, line 138
def old?
  feed.entries_dataset.where(uri: uri).any?
end
published() click to toggle source
# File lib/feed2email/entry.rb, line 142
def published; data.published end
published_line() click to toggle source
# File lib/feed2email/entry.rb, line 144
def published_line
  return nil unless author || published
  text = 'Published'
  text << " by #{author}" if author
  text << " at #{published}" if published
  text
end
send_mail() click to toggle source
# File lib/feed2email/entry.rb, line 152
def send_mail
  apply_send_delay

  logger.debug 'Sending new entry...'

  if build_mail.deliver!
    self.last_email_sent_at = Time.now
    save # record as seen
    return true
  end
end
title() click to toggle source
# File lib/feed2email/entry.rb, line 164
def title
  if data.title
    data.title.strip
  end
end
uri() click to toggle source
# File lib/feed2email/entry.rb, line 170
def uri
  return @uri if @uri

  @uri = data.url

  # Make relative entry URL absolute by prepending feed URL
  if @uri && @uri.start_with?('/') && !@uri.start_with?('//')
    @uri = URI.join(feed_uri[%r{https?://[^/]+}], @uri).to_s
  end

  @uri
end