load distributions from api
[opensuse:software-o-o.git] / app / controllers / application_controller.rb
1 # Filters added to this controller apply to all controllers in the application.
2 # Likewise, all the methods added will be available for all controllers.
3
4 class ApplicationController < ActionController::Base
5
6   before_filter :set_distributions
7
8   init_gettext('software')
9
10   def rescue_action_in_public(exception)
11     @message = exception.message
12     if request.xhr?
13       render :template => "error", :layout => false, :status => 404
14     else
15       render :template => 'error', :layout => "application", :status => 404
16     end
17   end
18
19   private
20
21   def set_distributions
22     @distributions = Rails.cache.fetch('distributions', :expires_in => 120.minutes) do
23       load_distributions
24     end
25   end
26
27
28   # load available distributions
29   def load_distributions
30     uri = URI.parse("http://#{API_HOST}/distributions")
31     request = Net::HTTP::Get.new(uri.path)
32     logger.debug "Loading distributions from #{uri}"
33     @distributions = Array.new
34     begin
35       Net::HTTP.start(uri.host, uri.port) do |http|
36         http.read_timeout = 5
37         response = http.request(request)
38         unless( response.kind_of? Net::HTTPSuccess )
39           logger.error "Cannot load distributions: '#{response.code}', message: \n#{response.body}"
40         else
41           doc = REXML::Document.new response.body
42           doc.elements.each("distributions/distribution") { |element|
43             dist = [element.elements['name'].text, element.elements['project'].text]
44             @distributions << dist
45           }
46         end
47       end
48     rescue Exception => e
49       logger.error "Error while loading distributions from '#{uri}': " + e.to_s
50     end
51     @distributions << ["ALL", 'ALL']
52     return @distributions
53   end
54
55
56 end