Class: Feedbook::Feed

Inherits:
Object
  • Object
show all
Defined in:
lib/feedbook/feed.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ NilClass

Initializes new Feed instance for given configuration

Parameters:

  • opts (defaults to: {})

    {} [Hash] Hash with configuration options for feed



18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#notificationsObject (readonly)

Returns the value of attribute notifications.



12
13
14
# File 'lib/feedbook/feed.rb', line 12

def notifications
  @notifications
end

#urlsObject (readonly)

Returns the value of attribute urls.



12
13
14
# File 'lib/feedbook/feed.rb', line 12

def urls
  @urls
end

#variablesObject (readonly)

Returns the value of attribute variables.



12
13
14
# File 'lib/feedbook/feed.rb', line 12

def variables
  @variables
end

Instance Method Details

#fetchArray

Fetches and parses all feed and merges into single array.

Returns:

  • (Array)

    array of Posts



33
34
35
36
37
38
39
# 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?NilClass

Validates if given parameters are valid

Returns:

  • (NilClass)

    nil

Raises:



46
47
48
49
50
51
52
53
54
55
56
# 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