Class: Feedalizer::Feed
- Inherits:
-
Object
- Object
- Feedalizer::Feed
- Defined in:
- lib/feedalizer.rb
Constant Summary collapse
- IDENTIFIER =
"Feedalizer/#{Feedalizer::VERSION} (https://github.com/michaelrigart/feedalizer)"
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Instance Method Summary collapse
- #debug! ⇒ Object
- #feed ⇒ Object
- #grab_page(url) ⇒ Object
-
#initialize(url, &block) ⇒ Feed
constructor
A new instance of Feed.
- #output ⇒ Object
- #output!(target = STDOUT) ⇒ Object
- #scrape_items(query, limit = 15) ⇒ Object
Constructor Details
#initialize(url, &block) ⇒ Feed
Returns a new instance of Feed.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/feedalizer.rb', line 12 def initialize(url, &block) @page = grab_page(url) @rss = RSS::Maker::RSS20.new feed.generator = IDENTIFIER feed.link = url feed.description = "Scraped from #{url}" instance_eval(&block) if block end |
Instance Attribute Details
#page ⇒ Object (readonly)
Returns the value of attribute page.
10 11 12 |
# File 'lib/feedalizer.rb', line 10 def page @page end |
Instance Method Details
#debug! ⇒ Object
48 49 50 51 52 |
# File 'lib/feedalizer.rb', line 48 def debug! @rss.items.each do |item| STDERR.puts [item.title, item.date, item.link].join('; ') end end |
#feed ⇒ Object
23 24 25 |
# File 'lib/feedalizer.rb', line 23 def feed @rss.channel end |
#grab_page(url) ⇒ Object
36 37 38 |
# File 'lib/feedalizer.rb', line 36 def grab_page(url) open(url) { |io| Oga.parse_html(io) } end |
#output ⇒ Object
40 41 42 |
# File 'lib/feedalizer.rb', line 40 def output @rss.to_feed.to_s end |
#output!(target = STDOUT) ⇒ Object
44 45 46 |
# File 'lib/feedalizer.rb', line 44 def output!(target = STDOUT) target << output end |
#scrape_items(query, limit = 15) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/feedalizer.rb', line 27 def scrape_items(query, limit = 15) elements = @page.xpath(query) elements.first(limit).each do |html_element| rss_item = @rss.items.new_item yield rss_item, html_element end end |