Class: Audrey2::Aggregator

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

Instance Method Summary collapse

Constructor Details

#initialize(configfile) ⇒ Aggregator

Returns a new instance of Aggregator.



83
84
85
# File 'lib/audrey2.rb', line 83

def initialize(configfile)
  init_config(configfile)
end

Instance Method Details

#feed_me(recipe_name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/audrey2.rb', line 87

def feed_me(recipe_name)
  begin
    # Load recipe and theme and make sure everything is in order
    recipe = load_recipe(recipe_name)
    output_file = recipe[:output_file]
    verify_output_file(output_file)
    max_entries = recipe[:max_entries] || 1000

    # Load theme
    init_theme(recipe[:theme])

    # Download and parse the feeds
    entry_sources = {}
    feeds = recipe[:feeds].collect { |feed| parse_feed(feed, entry_sources) }

    # Aggregate and sort the entries
    entries = []
    feeds.each { |feed| entries += feed.entries }
    entries.sort! &entry_sort_comparator

    # Prepare template evaluation scope including any helper code defined in the theme
    scope = Object.new
    scope.instance_eval(@helper_code) if @helper_code

    # Output the aggregated entries
    output = ''
    engine ||= Haml::Engine.new(@entry_template)

    entries[0..max_entries - 1].each do |entry|
      output << engine.render(scope, :entry => entry, :source => entry_sources[entry])
    end

    File.open(output_file, 'w') { |f| f << output }
  rescue Exception => e
    # NOTE: This also catches SystemExit as can be raise by Kernel#exit when recipes
    # and themes are loaded and verified. Is it good to handle those events here? Perhaps ...
    if @email
      email(<<-EOF
An exception occurred while running recipe #{recipe_name}

Exception: #{e}

Backtrace:

#{e.backtrace}
EOF
      )
    else
      $stderr.puts "An exception occurred while running recipe #{recipe_name}:\n\n#{e}\n#{e.backtrace}"
      exit(1)
    end
  end
end