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.


61
62
63
# File 'lib/audrey2.rb', line 61

def initialize(configfile)
  init_config(configfile)
end

Instance Method Details

#feed_me(recipe_name) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
# File 'lib/audrey2.rb', line 65

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("An exception occurred while running recipe \#{recipe_name}\n\nException: \#{e}\n\nBacktrace:\n\n\#{e.backtrace}\n"
      )
    else
      $stderr.puts "An exception occurred while running recipe #{recipe_name}:\n\n#{e}\n#{e.backtrace}"
      exit(1)
    end
  end
end