basic support to search only in a special project
[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   before_filter :set_language
8   helper :all # include all helpers, all the time
9   require "rexml/document"
10
11   init_gettext('software')
12
13   def rescue_action_in_public(exception)
14     @message = exception.message
15     if request.xhr?
16       render :template => "error", :layout => false, :status => 400
17     else
18       render :template => 'error', :layout => "application", :status => 400
19     end
20   end
21
22   private
23
24   def set_language
25     if params[:lang].nil?
26       lang = request.compatible_language_from(LANGUAGES) || "en"
27     else
28       lang = params[:lang][0].gsub(/\-/, '_')
29     end
30     @lang = lang
31     GetText.locale = lang
32   end
33
34
35   def set_distributions
36     @distributions = Rails.cache.fetch('distributions', :expires_in => 120.minutes) do
37       load_distributions
38     end
39   end
40
41
42   # load available distributions
43   def load_distributions
44     uri = URI.parse("http://#{API_HOST}/distributions")
45     request = Net::HTTP::Get.new(uri.path)
46     logger.debug "Loading distributions from #{uri}"
47     @distributions = Array.new
48     begin
49       Net::HTTP.start(uri.host, uri.port) do |http|
50         http.read_timeout = 30
51         response = http.request(request)
52         unless( response.kind_of? Net::HTTPSuccess )
53           logger.error "Cannot load distributions: '#{response.code}', message: \n#{response.body}"
54         else
55           doc = REXML::Document.new response.body
56           doc.elements.each("distributions/distribution") { |element|
57             dist = [element.elements['name'].text, element.elements['project'].text]
58             @distributions << dist
59           }
60         end
61       end
62       @distributions << ["ALL Distributions", 'ALL']
63       return @distributions
64     rescue Exception => e
65       logger.error "Error while loading distributions from '#{uri}': " + e.to_s
66     end
67     return nil
68   end
69
70
71 end