class Mako::FeedConstructor

Attributes

feed_data[R]
feed_url[R]

Public Class Methods

new(args) click to toggle source
# File lib/mako/feed_constructor.rb, line 7
def initialize(args)
  @feed_url = args.fetch(:feed_url)
  @feed_data = args.fetch(:feed_data)
end

Public Instance Methods

parse_and_create() click to toggle source

Parses raw XML feed and creates Feed and Article objects to be rendered. Returns false if feed cannot be parsed.

@return [Feed]

# File lib/mako/feed_constructor.rb, line 16
def parse_and_create
  parsed_feed = parse_feed
  return false unless parsed_feed

  feed = create_feed(parsed_feed)
  create_articles(feed, parsed_feed)
  feed
end

Private Instance Methods

create_articles(feed, parsed_feed) click to toggle source

Creates new Mako::Article objects from the parsed Feedjira::Feed object if the source article was published within the last day. Adds the Mako::Article objects to the Mako::Feed object's articles attribute.

@param [Mako::Feed] feed @param [Feedjira::Feed] parsed_feed

# File lib/mako/feed_constructor.rb, line 51
def create_articles(feed, parsed_feed)
  parsed_feed.entries.each do |entry|
    next unless entry.published >= (Time.now - 1.day).beginning_of_day

    feed.articles << Article.new(title: entry.title,
                                 published: entry.published,
                                 summary: entry_summary(entry),
                                 url: entry.url)
  end
end
create_feed(parsed_feed) click to toggle source

Creates new Mako::Feed object from the parsed Feedjira::Feed object

@param [Feedjira::Feed] @return [Mako::Feed]

# File lib/mako/feed_constructor.rb, line 41
def create_feed(parsed_feed)
  Feed.new(url: parsed_feed.url, title: parsed_feed.title)
end
entry_summary(entry) click to toggle source

Some feeds use summary for the full article body, other feeds use content. This prefers content, but falls back to summary.

@param [Feedjira::Feed] @return [String] an HTML string of the source article body

# File lib/mako/feed_constructor.rb, line 67
def entry_summary(entry)
  !entry.content || entry.content.empty? ? entry.summary : entry.content
end
parse_feed() click to toggle source

Takes raw XML and parses it into a Feedjira::Feed object

@return [Feedjira::Feed]

# File lib/mako/feed_constructor.rb, line 30
def parse_feed
  Feedjira::Feed.parse(feed_data)
rescue Feedjira::NoParserAvailable
  Mako.errors.add_error "Unable to parse #{feed_url}."
  false
end