class EndiFeed::News

This class cleans up and formats XML/RSS feed.

Public Class Methods

new() click to toggle source

Loads initial data

sets @headlines ivar
and calls :news_feed, :channel methods

@see news_feed @see channel

# File lib/endi_feed/news.rb, line 15
def initialize
  @headlines = []
  news_feed
  channel
end

Public Instance Methods

channel() click to toggle source

Convenience method for feed channel

# File lib/endi_feed/news.rb, line 46
def channel
  @channel ||= @news_feed.channel
end
format_header_text(headlines) click to toggle source

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
get_headlines(total) click to toggle source

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
items() click to toggle source

Convenience method for items in feed

# File lib/endi_feed/news.rb, line 51
def items
  @news_feed.items
end
last_update() click to toggle source

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
news_feed() click to toggle source

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
site_url() click to toggle source

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
title() click to toggle source

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

format_headline(item, num) click to toggle source

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
process_news(total = 25) click to toggle source

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
total_met?(total) click to toggle source

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