replace fake data with the real API calls
[opensuse:software-o-o.git] / app / controllers / download_controller.rb
1 class DownloadController < ApplicationController
2
3   verify :params => [:prj, :pkg]
4   before_filter :prepare
5
6   def prepare
7     @prj = params[:prj]
8     @pkg = params[:pkg]
9
10     api_result = get_from_api("/search/published/binary/id?match=project='#{@prj}'+and+package='#{@pkg}'")
11     if api_result
12       @data = Hash.new
13       api_result.elements.each("/collection/binary") { |e|
14         distro = e.attributes[:repository]
15         if not data.has_key?(distro)
16           data[distro] = {
17             :repo => "http://download.opensuse.org/repositories/#{@prj}/#{distro}/",
18             :pkg => Hash.new
19           }
20           case e.attributes[:baseproject]
21             when /^openSUSE:/
22               data[distro][:flavor] = 'openSUSE'
23               data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@prj}/#{distro}/#{@pkg}.ymp"
24             when /^SUSE:SLE-/
25               data[distro][:flavor] = 'SLE'
26             when /^Fedora:/
27               data[distro][:flavor] = 'Fedora'
28             when /^RedHat:RHEL-/
29               data[distro][:flavor] = 'RHEL'
30             when /^CentOS:CentOS-/
31               data[distro][:flavor] = 'CentOS'
32             when /^Mandriva:/
33               data[distro][:flavor] = 'Mandriva'
34             when /^Mageia:/
35               data[distro][:flavor] = 'Mageia'
36             when /^Debian:/
37               data[distro][:flavor] = 'Debian'
38             when /^Ubuntu:/
39               data[distro][:flavor] = 'Ubuntu'
40             else
41               data[distro][:flavor] = 'Unknown'
42           end
43         end
44         filename = e.attributes[:filename]
45         filepath = e.attributes[:filepath]
46         data[distro][:pkg][filename] = 'http://download.opensuse.org/repositories/' + filepath
47       }
48       # collect distro types from data
49       @flavors = @data.values.collect { |i| i[:flavor] }.uniq.sort{|x,y| x.downcase <=> y.downcase }
50     else
51       head :forbidden
52     end
53   end
54
55   # /download.html?prj=name&pkg=name
56   def html
57     render :html, :layout => false
58   end
59
60   # /download.js?prj=name&pkg=name
61   def js
62     render :js, :layout => false
63   end
64
65   # /download.json?prj=name&pkg=name
66   def json
67     # needed for rails < 3.0 to support JSONP
68     render_json @data.to_json
69   end
70
71   private
72
73   def get_from_api(path)
74     begin
75       req = Net::HTTP::Get.new(path)
76       req['x-username'] = ICHAIN_USER
77       host, port = API_HOST.split(/:/)
78       port ||= 80
79       res = Net::HTTP.new(host, port).start do |http|
80         http.request(req)
81       end
82       doc = REXML::Document.new res.body
83     rescue
84       nil
85     end
86   end
87
88 end