Class: SimpleRss

Inherits:
DynamicContent
  • Object
show all
Defined in:
app/models/simple_rss.rb

Constant Summary collapse

DISPLAY_NAME =
'RSS Feed'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.form_attributesObject

Simple RSS processing needs a feed URL and the format of the output content.



286
287
288
289
# File 'app/models/simple_rss.rb', line 286

def self.form_attributes
  attributes = super()
  attributes.concat([:config => [:url, :url_userid, :url_password, :output_format, :reverse_order, :max_items, :xsl, :sanitize_tags]])
end

.preview(data) ⇒ Object

return the first item for use as a preview data is a hash of the config



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'app/models/simple_rss.rb', line 345

def self.preview(data)
  begin
    o = SimpleRss.create()
    o.config['url'] = data[:url]
    o.config['url_userid'] = data[:url_userid]
    o.config['url_password'] = data[:url_password]
    o.config['output_format'] = data[:output_format]
    o.config['max_items'] = data[:max_items]
    o.config['reverse_order'] = data[:reverse_order]
    o.config['xsl'] = data[:xsl]
    o.config['sanitize_tags'] = data[:sanitize_tags]

    content = o.build_content
    results = content.first.data unless content.blank?
  rescue => e
    results = "Unable to preview.  #{e.message}"
  end

  return results
end

Instance Method Details

#build_contentObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/simple_rss.rb', line 40

def build_content
  contents = []

  url = self.config['url']
  unless url.blank?
    url_userid = self.config['url_userid']
    url_password = self.config['url_password']
    type, feed_title, rss, raw = fetch_feed(url, url_userid, url_password)
    
    if (["RSS", "ATOM"].include? type) && !feed_title.blank?
      # it is a valid feed
      if !self.config['reverse_order'].blank? && self.config['reverse_order'] == '1'
        rss.items.reverse!
      end
      feed_items = rss.items
      if !self.config['max_items'].blank? && self.config['max_items'].to_i > 0
        feed_items = feed_items.first(self.config['max_items'].to_i)
      end
      case self.config['output_format']
      when 'headlines'
        feed_items.each_slice(5).with_index do |items, index|
          htmltext = HtmlText.new()
          htmltext.name = "#{feed_title} (#{index+1})"
          htmltext.data = sanitize("<h1>#{feed_title}</h1> #{items_to_html(items, type)}")
          contents << htmltext
        end
      when 'detailed'
        feed_items.each_with_index do |item, index|
          htmltext = HtmlText.new()
          htmltext.name = "#{feed_title} (#{index+1})"
          htmltext.data = sanitize(item_to_html(item, type))
          contents << htmltext
        end
      when 'xslt'
        require 'rexml/document'
        require 'xml/xslt'

        #XML::XSLT.registerErrorHandler { |string| puts string }
        xslt = XML::XSLT.new()
        begin
          xslt.xml = REXML::Document.new(raw)
        rescue REXML::ParseException => e
          Rails.logger.error("Unable to parse incoming feed: #{e.message}")
          raise "Unable to parse incoming feed. "
        rescue => e
          raise e
        end

        begin
          xslt.xsl = REXML::Document.new(self.config['xsl'])
        rescue REXML::ParseException => e
          Rails.logger.error("Unable to parse Xsl: #{e.message}")
          # fmt is <rexml::parseexception: message :> trace ... so just pull out the message
          s = e.message
          msg_stop = s.index(">")
          s = s.slice(23, msg_stop - 23) if !msg_stop.nil?
          raise "Unable to parse Xsl.  #{s}"
        rescue => e
          raise e
        end

        # add a replace [gsub] function for more powerful transforms.  You can use this in a transform
        # by adding the bogus namespace http://concerto.functions
        # A nodeset comes in as an array of REXML::Elements 
        XML::XSLT.registerExtFunc("http://concerto.functions", "replace") do |nodes, pattern, replacement|
          result = xslt_replace(nodes, pattern, replacement)
          result
        end

        XML::XSLT.registerExtFunc("http://schemas.concerto-signage.org/functions", "replace") do |nodes, pattern, replacement|
          result = xslt_replace(nodes, pattern, replacement)
          result
        end

        data = xslt.serve()
        # xslt.serve does always return a string with ASCII-8BIT encoding regardless of what the actual encoding is
        data = data.force_encoding(xslt.xml.encoding) if data

        # try to load the transformed data as an xml document so we can see if there are 
        # mulitple content-items that we need to parse out, if we cant then treat it as one content item
        begin
          data_xml = REXML::Document.new('<root>' + data + '</root>')
          nodes = REXML::XPath.match(data_xml, "//content-item")
          # if there are no content-items then add the whole result (data) as one content
          if nodes.count == 0
            htmltext = HtmlText.new()
            htmltext.name = "#{feed_title}"
            htmltext.data = sanitize(data)
            contents << htmltext
          else
            # if there are any content-items then add each one as a separate content
            # and strip off the content-item wrapper
            nodes.each do |n|
              htmltext = HtmlText.new()
              htmltext.name = "#{feed_title}"
              htmltext.data = sanitize(n.to_s.gsub(/^\s*\<content-item\>/, '').gsub(/\<\/content-item\>\s*$/,''))
              contents << htmltext
            end
          end
        rescue => e
          # maybe the html was not xml compliant-- this happens frequently in rss feed descriptions
          # look for another separator and use it, if it exists

          if data.include?("</content-item>")
            # if there are any content-items then add each one as a separate content
            # and strip off the content-item wrapper
            data.split("</content-item>").each do |n|
              htmltext = HtmlText.new()
              htmltext.name = "#{feed_title}"
              htmltext.data = sanitize(n.sub("<content-item>", ""))
              contents << htmltext if !htmltext.data.strip.blank?
            end

          else
            Rails.logger.error("unable to parse resultant xml, assuming it is one content item #{e.message}")
            # raise "unable to parse resultant xml #{e.message}"
            # add the whole result as one content
            htmltext = HtmlText.new()
            htmltext.name = "#{feed_title}"
            htmltext.data = sanitize(data)
            contents << htmltext
          end
        end
      else
        raise ArgumentError, 'Unexpected output format for RSS feed.'
      end
    elsif type == "ERROR"
      raise rss
    else
      Rails.logger.error("could not fetch #{type} feed for #{feed_title} at #{url}")
      raise "Unexpected feed format for #{url}."
    end
  end
  
  return contents
end

#fetch_feed(url, url_userid, url_password) ⇒ Object

fetch the feed, return the type, title, and contents (parsed) and raw feed (unparsed)



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'app/models/simple_rss.rb', line 204

def fetch_feed(url, url_userid, url_password)
  require 'encryptor'
  require 'rss'
  require 'open-uri'

  type = 'UNKNOWN'
  title = ''
  rss = nil
  feed = nil

  unless url.blank?
    begin
      # cache same url for 1 minute to alleviate redundant calls when previewing
      feed = Rails.cache.fetch(url, :expires_in => 1.minute) do
        if url_userid.blank? or url_password.blank?
          open(url).read()
        else
          open(url, http_basic_authentication: [url_userid, url_password]).read()
        end
      end

      rss = RSS::Parser.parse(feed, false, true)
      raise "feed could not be parsed" if rss.nil?
    rescue => e
      # cant parse rss or url is bad
      Rails.logger.debug("unable to fetch or parse feed - #{url}, #{e.message}")
      rss = e.message
      type = "ERROR"
    else
      type = rss.feed_type.upcase

      case type
      when "RSS"
        title = rss.channel.title
      when "ATOM"
        title = rss.title.content
      else
        #title = "unknown feed type"
      end
    end
  end

  return type, title, rss, feed
end

#item_to_html(item, type) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'app/models/simple_rss.rb', line 249

def item_to_html(item, type)
  case type
  when "RSS"
    title = item.title
    description = item.description
  when "ATOM"
    title = item.title.content

    # seems like the hard way, but the only way I could figure out to get the 
    # contents without it being html encoded.  most likely a prime candidate for optimizing
    require 'rexml/document'
    entry_xml = REXML::Document.new(item.to_s)
    content_html = ""
    if (first = REXML::XPath.first(entry_xml, "entry/content"))
      content_html = first.text
    end

    description = (item.summary.nil? ? content_html : item.summary.content)
  end

  description ||= ""
  return "<h1>#{title}</h1><p>#{description.html_safe}</p>"
end

#items_to_html(items, type) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
# File 'app/models/simple_rss.rb', line 273

def items_to_html(items, type)
  return items.collect {|item| 
    case type
    when "RSS"
      title = item.title
    when "ATOM"
      title = item.title.content
    end

    "<h2>#{title}</h2>" }.join(" ")
end

#load_configObject

Load a configuration hash. Converts the JSON data stored for the content into the configuration. Called during ‘after_find`.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/simple_rss.rb', line 11

def load_config
  j = JSON.load(self.data)
  
  # decrypt fields
  unless j.blank?
    encrypted_userid = Base64.decode64(j['url_userid_enc']) unless j['url_userid_enc'].blank?
    encrypted_password = Base64.decode64(j['url_password_enc']) unless j['url_password_enc'].blank?

    j['url_userid'] = (encrypted_userid.blank? ? "" : encrypted_userid.decrypt)
    j['url_password'] = (encrypted_password.blank? ? "" : encrypted_password.decrypt)
  end 

  self.config = j
end

#sanitize(html) ⇒ Object



366
367
368
369
370
371
372
373
374
# File 'app/models/simple_rss.rb', line 366

def sanitize(html)
  if self.config.include?('sanitize_tags') and !self.config['sanitize_tags'].empty?
    whitelist = ActionView::Base.sanitized_allowed_tags 
    blacklist = self.config['sanitize_tags'].split(" ")

    html = ActionController::Base.helpers.sanitize(html, :tags => (whitelist - blacklist))
  end
  html
end

#save_configObject

Prepare the configuration to be saved. Compress the config hash back into JSON to be stored in the database. Called during ‘before_validation`.



29
30
31
32
33
34
35
36
37
38
# File 'app/models/simple_rss.rb', line 29

def save_config
  j = self.config.deep_dup

  # encrypt fields
  j['url_userid_enc'] = (j['url_userid'].blank? ? "" : Base64.encode64(j['url_userid'].encrypt))
  j['url_password_enc'] = (j['url_password'].blank? ? "" : Base64.encode64(j['url_password'].encrypt))
  j.delete 'url_userid'
  j.delete 'url_password'
  self.data = JSON.dump(j)
end

#validate_configObject



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'app/models/simple_rss.rb', line 308

def validate_config
  if self.config['url'].blank?
    errors.add(:base, "URL can't be blank")
  end

  if !['headlines', 'detailed', 'xslt'].include?(self.config['output_format'])
    errors.add(:base, "Display Format must be Headlines or Articles or XSLT")
  end

  if self.config['output_format'] == 'xslt'
    if self.config['xsl'].blank?
      errors.add(:base, "XSL Markup can't be blank when using the XSLT Display Format")
    else
      url = self.config['url']
      url_userid = self.config['url_userid']
      url_password = self.config['url_password']
      unless url.blank? 
        require 'rexml/document'
        require 'xml/xslt'

        type, title, rss, raw = fetch_feed(url, url_userid, url_password)
        if ["RSS", "ATOM"].include? type
          begin
            xslt = XML::XSLT.new()
            xslt.xml = REXML::Document.new(raw)
            xslt.xsl = REXML::Document.new(self.config['xsl'])
          rescue XML::XSLT::ParsingError
            errors.add(:base, "XSL Markup could not be parsed")
          end
        end
      end
    end
  end
end

#validate_feedObject

if the feed is valid we store the title in config



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'app/models/simple_rss.rb', line 292

def validate_feed
  url = self.config['url']
  url_userid = self.config['url_userid']
  url_password = self.config['url_password']
  unless url.blank? 
    Rails.logger.debug("looking up feed title for #{url}")

    type, title = fetch_feed(url, url_userid, url_password)
    if (["RSS", "ATOM"].include? type) && !title.blank?
      self.config['title'] = title
    else
      errors.add(:base, "URL does not appear to be an RSS feed")
    end
  end
end

#xslt_replace(nodes, pattern, replacement) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/simple_rss.rb', line 177

def xslt_replace(nodes, pattern, replacement)
  #Rails.logger.debug("pattern = #{pattern}")
  #Rails.logger.debug("replacement = #{replacement}")
  result = []
  begin
    # this will only work with nodesets for now
    re_pattern = Regexp.new(pattern, Regexp::MULTILINE)
    if nodes.is_a?(Array) && nodes.count > 0 && nodes.first.is_a?(REXML::Element)
      nodes.each do |node|
        s = node.to_s
        r = s.gsub(re_pattern, replacement)
        result << REXML::Document.new(r)
      end
    elsif nodes.is_a?(String)
      result = nodes.gsub(re_pattern, replacement)
    else
      # dont know how to handle this
      Rails.logger.info "I'm sorry, but the xsl external function replace does not know how to handle this type #{nodes.class}"
    end
  rescue => e
    Rails.logger.error "there was a problem replacing #{pattern} with #{replacement} - #{e.message}"
  end

  result
end