Class: RSS2Mail::Feed

Inherits:
Object
  • Object
show all
Defined in:
lib/rss2mail/feed.rb

Constant Summary collapse

HOST =
ENV['HOSTNAME'] || ENV['HOST'] || %x{hostname}.chomp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feed, options = {}) ⇒ Feed

Returns a new instance of Feed.

Raises:

  • (TypeError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rss2mail/feed.rb', line 44

def initialize(feed, options = {})
  raise TypeError, "Hash expected, got #{feed.class}" unless feed.is_a?(Hash)

  @feed    = feed
  @simple  = feed[:simple]
  @updated = feed[:updated]

  @reload  = options[:reload]
  @verbose = options[:verbose]
  @debug   = options[:debug]

  required = [:url, :to, :title]
  required.delete_if { |i| feed.has_key?(i) }

  raise ArgumentError, "Feed incomplete: #{required.join(', ')} missing" unless required.empty?
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



42
43
44
# File 'lib/rss2mail/feed.rb', line 42

def content
  @content
end

#debugObject (readonly)

Returns the value of attribute debug.



42
43
44
# File 'lib/rss2mail/feed.rb', line 42

def debug
  @debug
end

#feedObject (readonly)

Returns the value of attribute feed.



42
43
44
# File 'lib/rss2mail/feed.rb', line 42

def feed
  @feed
end

#reloadObject (readonly)

Returns the value of attribute reload.



42
43
44
# File 'lib/rss2mail/feed.rb', line 42

def reload
  @reload
end

#rssObject (readonly)

Returns the value of attribute rss.



42
43
44
# File 'lib/rss2mail/feed.rb', line 42

def rss
  @rss
end

#simpleObject (readonly)

Returns the value of attribute simple.



42
43
44
# File 'lib/rss2mail/feed.rb', line 42

def simple
  @simple
end

#updatedObject (readonly)

Returns the value of attribute updated.



42
43
44
# File 'lib/rss2mail/feed.rb', line 42

def updated
  @updated
end

#verboseObject (readonly)

Returns the value of attribute verbose.



42
43
44
# File 'lib/rss2mail/feed.rb', line 42

def verbose
  @verbose
end

Instance Method Details

#deliver(templates) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rss2mail/feed.rb', line 61

def deliver(templates)
  unless mail_cmd = File.which(_mail_cmd = 'mail')
    raise "Mail command not found: #{_mail_cmd}"
  end

  to = [*feed[:to]]

  if to.empty?
    log 'No one to send to'
    return
  end

  unless get && parse
    log 'Nothing to send'
    return
  end

  if rss.items.empty?
    log 'No new items'
    return
  end

  content_type = feed[:content_type] || 'text/html'
  encoding     = feed[:encoding]     || 'UTF-8'

  feed[:sent] ||= []

  content_type_header = "Content-type: #{content_type}; charset=#{encoding}"

  unless template = templates[content_type[/\/(.*)/, 1]]
    log "Template not found: #{content_type}"
    return
  end

  cmd = [
    mail_cmd,
    '-e',
    "-a '#{content_type_header}'",
    "-a 'From: rss2mail@#{HOST}'",
    "-s '[#{feed[:title]}] \#{subject}'",
    *to
  ].join(' ')

  sent = 0

  rss.items.each { |item|
    title       = item.title
    link        = item.link
    description = item.description(feed[:unescape_html])
    date        = item.date
    author      = item.author
    body        = item.body(feed[:body])
    subject     = item.subject

    log "#{title} / #{date} [#{author}]", debug
    log "<#{link}>", debug

    send_mail(cmd.evaluate(binding), ERB.new(template).result(binding)) {
      feed[:sent] << link
      sent += 1
    }
  }

  # only keep the last 100 entries
  feed[:sent].slice!(0...-100)

  log "#{sent} items sent"
  sent
end