class EndiFeed::News
This class cleans up and formats XML/RSS feed.
Public Class Methods
Public Instance Methods
Convenience method for feed channel
# File lib/endi_feed/news.rb, line 46 def channel @channel ||= @news_feed.channel end
Applies formatting to header text @return [Array<String>] formatted header text
# File lib/endi_feed/news.rb, line 75 def format_header_text(headlines) sub_title = "Última actualización: #{ last_update }" header_title = "#{ title } (#{ site_url })" headlines.unshift(header_title, sub_title) end
Factory method that gets the news
@see EndiFeed::Util.convert_date
@see format_header_text
@see process_news
@see format_headline
@param total [Integer] total of news to fetch @return [Array<String>] news or an error message
# File lib/endi_feed/news.rb, line 30 def get_headlines(total) process_news(total) format_header_text(@headlines) rescue 'Problem retrieving news headlines.' end
Convenience method for items in feed
# File lib/endi_feed/news.rb, line 51 def items @news_feed.items end
Returns the last update time of the feed @return [String] last update time
# File lib/endi_feed/news.rb, line 69 def last_update convert_time(@channel.pubDate) end
Loads news into instance variable
@see EndiFeed::Util.parse_feed
@return [RSS] parsed XML feed or nil
# File lib/endi_feed/news.rb, line 41 def news_feed @news_feed ||= parse_feed end
Returns the site’s url from the feed @return [String] newspaper site URL
# File lib/endi_feed/news.rb, line 63 def site_url @channel.link end
Returns the title from the feed @return [String] newspaper site title
# File lib/endi_feed/news.rb, line 57 def title @channel.title end
Private Instance Methods
Applies formatting to headline and shrinks urls @return [String] formatted news headline
# File lib/endi_feed/news.rb, line 85 def format_headline(item, num) ''.concat("#{ num.next }. #{ item.title } ") .concat("(#{ convert_date(item.pubDate) })") .concat(" - #{ LinkShrink.shrink_url(item.link) }") end
Handles iteration of each headline @return [Array<String>] news headlines
# File lib/endi_feed/news.rb, line 93 def process_news(total = 25) items.map.with_index do |item, num| @headlines << format_headline(item, num) if total_met?(total) end.compact || nil end
Returns true when total is reached @return [TrueClass, FalseClass]
# File lib/endi_feed/news.rb, line 101 def total_met?(total) @headlines.length < Integer(total) end