Class: PolyrexFeedReader

Inherits:
Object
  • Object
show all
Defined in:
lib/polyrex-feed-reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(polyrex) ⇒ PolyrexFeedReader

Returns a new instance of PolyrexFeedReader.



29
30
31
32
33
# File 'lib/polyrex-feed-reader.rb', line 29

def initialize(polyrex)

  @polyrex = polyrex

end

Instance Method Details

#fetch_feedsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/polyrex-feed-reader.rb', line 35

def fetch_feeds()

  feeds = @polyrex.xpath('//column/records/section/records/feed/summary')

  feeds.each do |feed|

    puts "fetching %s " % feed.text('rss_url').inspect

    rtd = RSStoDynarex.new feed.text('rss_url')
    dynarex = rtd.to_dynarex
    dynarex.save "%s.xml" % feed.text('title')\
                        .downcase.gsub(/\s/,'').gsub(/\W/,'_')
  end
end

#refreshObject Also known as: update_doc



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
# File 'lib/polyrex-feed-reader.rb', line 50

def refresh

  @polyrex.records.each do |column|

    column.records.each do |section| 
 
      section.records.each do |feed|

        filename = "%s.xml" % feed.title\
                            .downcase.gsub(/\s/,'').gsub(/\W/,'_')

        d = Dynarex.new filename
        feed.last_accessed = datetimestamp()
        feed.last_modified = datetimestamp() if feed.last_modified.empty?

        items = d.to_h[0..2]

        if feed.records.length > 0 and \
                                items.first[:title] == feed.item[0].title then
          feed.recent = recency(feed.last_modified)
          next 
        end

        feed.recent = '1hot'
        feed.records.remove_all
        items.each.with_index do |x, i|

          h = {title: x[:title], link: x[:link]}

          if i == 0 then

            raw_desc = CGI.unescapeHTML(x[:description]).gsub(/<\/?[^>]*>/, "")
            desc = raw_desc.length > 300 ? raw_desc[0..296] + ' ...' : raw_desc
            h[:description] = desc
          end

          feed.create.item h
        end

        feed.last_modified = datetimestamp()
      end
    end
  end
end

#save_css(filepath = 'feeds.css') ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/polyrex-feed-reader.rb', line 97

def save_css(filepath='feeds.css')

  lib = File.dirname(__FILE__)
  css_buffer = File.read(lib + '/feeds.css')
  #css_buffer = File.read('feeds.css')

  File.write filepath, css_buffer
end

#save_html(filepath = 'feeds.html') ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/polyrex-feed-reader.rb', line 106

def save_html(filepath='feeds.html')

  lib = File.dirname(__FILE__)
  xsl_buffer = File.read(lib + '/feeds.xsl')
  #xsl_buffer = File.read('feeds.xsl')

  xslt  = Nokogiri::XSLT(xsl_buffer)
  html = xslt.transform(Nokogiri::XML(@polyrex.to_xml)).to_s
  File.write filepath, html
end

#save_xml(filepath = 'feeds.xml') ⇒ Object



117
118
119
# File 'lib/polyrex-feed-reader.rb', line 117

def save_xml(filepath='feeds.xml')
  @polyrex.save filepath, pretty: true
end