class Feedbook::Feed

Attributes

notifications[R]
urls[R]
variables[R]

Public Class Methods

new(opts = {}) click to toggle source

Initializes new Feed instance for given configuration @param opts = {} [Hash] Hash with configuration options for feed

@return [NilClass] nil

# File lib/feedbook/feed.rb, line 18
def initialize(opts = {})
  @urls          = opts.fetch(:urls, '').split
  @variables     = opts.fetch(:variables, {})
  @notifications = opts.fetch(:notifications, []).map do |notification|
    Notification.new(
      type:      notification['type'],
      template:  notification['template'],
      variables: variables
    )
  end
end

Public Instance Methods

fetch() click to toggle source

Fetches and parses all feed and merges into single array.

@return [Array] array of Posts

# File lib/feedbook/feed.rb, line 33
def fetch
  urls
  .map do |url|
    parse_feed(Feedjira::Feed.fetch_and_parse(url))
  end
  .inject :+
end
valid?() click to toggle source

Validates if given parameters are valid

@return [NilClass] nil @raise [Feedbook::Errors::InvalidVariablesFormatError] if variables parameter is not a Hash @raise [Feedbook::Errors::InvalidFeedUrlError] if url collection is not a empty and contains valid urls

# File lib/feedbook/feed.rb, line 46
def valid?
  if urls.empty? || urls.any? { |url| url !~ /\A#{URI::regexp}\z/ }
    raise Errors::InvalidFeedUrlError.new
  end

  unless variables.is_a? Hash
    raise Errors::InvalidVariablesFormatError.new
  end

  notifications.each { |notification| notification.valid? }
end

Private Instance Methods

on_failure(url) click to toggle source

Determines behavior of failure in fetching feeds @param url [String] requested url

@raise [Feedbook::Error::ParseFeedError] if fetching and parsing feed was unsuccessful

# File lib/feedbook/feed.rb, line 80
def on_failure(url)
  raise Error::ParseFeedError.new(url)
end
parse_feed(feed) click to toggle source

Parses feetched feed into Feedbook::Post @param feed [Feedjira::Parser::Atom] Atom/RSS feed

@return [Array] array of Posts created from feed entries

# File lib/feedbook/feed.rb, line 64
def parse_feed(feed)
  feed.entries.map do |entry|
    Post.new(
       author:     entry.author,
       published:  entry.published,
       url:        entry.url,
       title:      entry.title,
       feed_title: feed.title
    )
  end
end