Skip to content

Commit

Permalink
[api] show also remote distributions from all remote projects
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianschroeter committed Dec 20, 2012
1 parent 0b7bbc5 commit ace6b06
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/api/app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ class DistributionsController < ApplicationController
# GET /distributions
# GET /distributions.xml
def index
@distributions = Distribution.all
if request.env['REQUEST_URI'].gsub(/\.xml$/, "") == "/distributions/include_remotes"

This comment has been minimized.

Copy link
@coolo

coolo Dec 20, 2012

Member

You can't be serious ;(

Revert that quick

@distributions = Distribution.all_including_remotes
else
@distributions = Distribution.all
end

respond_to do |format|
format.xml
Expand Down
37 changes: 36 additions & 1 deletion src/api/app/models/distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,47 @@ def self.parse(xmlhash)
DistributionIcon.delete_all
xmlhash.elements('distribution') do |d|
db = Distribution.create(vendor: d['vendor'], version: d['version'], name: d['name'], project: d['project'],
reponame: d['reponame'], repository: d['repository'],link: d['link'])
reponame: d['reponame'], repository: d['repository'], link: d['link'])
d.elements('icon') do |i|
dbi = DistributionIcon.find_or_create_by_url(width: i['width'], height: i['height'], url: i['url'])
db.icons << dbi
end
end
end
end

def self.all_including_remotes
local = self.all

remote = Rails.cache.fetch("remote_distribution_list") do
list = []
remote_projects = Project.where("NOT ISNULL(projects.remoteurl)")
remote_projects.each do |prj|
url = URI.parse( prj.remoteurl + "/distributions.xml" )
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
body = nil
if Rails.env.test?
body = File.open("#{Rails.root}/test/fixtures/backend/distributions.xml").read()
else
response = http.get(url.path)
body = response.body
end
xmlhash = Xmlhash.parse(body)
xmlhash.elements('distribution') do |d|
iconlist = []
d.elements('icon') do |i|
iconlist << { "width" => i['width'], "height" => i['height'], "url" => i['url'] }
end
list << {"vendor" => d['vendor'], "version" => d['version'], "name" => d['name'],
"project" => prj.name + ":" + d['project'], "icons" => iconlist,
"reponame" => d['reponame'], "repository" => d['repository'], "link" => d['link']}
end
end
list
end

return local + remote
end

end
38 changes: 27 additions & 11 deletions src/api/app/views/distributions/index.xml.builder
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
xml.distributions do
@distributions.each do |d|
xml.distribution(vendor: d.vendor, version: d.version, id: d.id) do
xml.name(d.name)
xml.project(d.project)
xml.reponame(d.reponame)
xml.repository(d.repository)
xml.link(d.link)
d.icons.each do |i|
attr = {url: i.url}
attr[:width] = i.width if i.width
attr[:height] = i.height if i.height
xml.icon(attr)
if d.class == Hash

This comment has been minimized.

Copy link
@coolo

coolo Dec 20, 2012

Member

why can't you make pull requests for reviews for such things? This is aweful! A subclass of Hash will already break this!

xml.distribution(vendor: d["vendor"], version: d["version"], id: d["id"]) do
xml.name(d["name"])
xml.project(d["project"])
xml.reponame(d["reponame"])
xml.repository(d["repository"])
xml.link(d["link"])
d["icons"].each do |i|
attr = {url: i["url"]}
attr[:width] = i["width"] unless i["width"].blank?
attr[:height] = i["height"] unless i["height"].blank?
xml.icon(attr)
end
end
else
xml.distribution(vendor: d.vendor, version: d.version, id: d.id) do
xml.name(d.name)
xml.project(d.project)
xml.reponame(d.reponame)
xml.repository(d.repository)
xml.link(d.link)
d.icons.each do |i|
attr = {url: i.url}
attr[:width] = i.width if i.width
attr[:height] = i.height if i.height
xml.icon(attr)
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions src/api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@
### /distributions

match '/distributions' => 'distributions#upload', via: :put
match '/distributions/include_remotes' => 'distributions#index'
# update is missing here
resources :distributions, only: [:index, :show, :create, :destroy]

Expand Down
21 changes: 21 additions & 0 deletions src/api/test/fixtures/backend/distributions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<distributions>
<distribution vendor="openSUSE" version="Factory" id="1">
<name>openSUSE Factory</name>
<project>openSUSE:Factory</project>
<reponame>openSUSE_Factory</reponame>
<repository>snapshot</repository>
<link>http://www.opensuse.org/</link>
<icon url="https://static.opensuse.org/distributions/logos/opensuse-Factory-8.png" width="8" height="8"/>
<icon url="https://static.opensuse.org/distributions/logos/opensuse-Factory-16.png" width="16" height="16"/>
</distribution>
<distribution vendor="openSUSE" version="12.2" id="2">
<name>openSUSE 12.2</name>
<project>openSUSE:12.2</project>
<reponame>openSUSE_12.2</reponame>
<repository>standard</repository>
<link>http://www.opensuse.org/</link>
<icon url="https://static.opensuse.org/distributions/logos/opensuse-12.2-8.png" width="8" height="8"/>
<icon url="https://static.opensuse.org/distributions/logos/opensuse-12.2-16.png" width="16" height="16"/>
</distribution>
</distributions>

11 changes: 11 additions & 0 deletions src/api/test/functional/distributions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,16 @@ class DistributionsControllerTest < ActionController::IntegrationTest
prepare_request_with_user "tom", "thunder"
get "/distributions"
assert_response :success
assert_no_xml_tag :tag => "project", :content => "RemoteInstance:openSUSE:12.2"

get "/distributions/include_remotes"
assert_response :success
# validate rendering and modifications of a remote repo
assert_xml_tag :tag => "name", :content => "openSUSE 12.2"
assert_xml_tag :tag => "project", :content => "RemoteInstance:openSUSE:12.2"
assert_xml_tag :tag => "reponame", :content => "openSUSE_12.2"
assert_xml_tag :tag => "repository", :content => "standard"
assert_xml_tag :tag => "link", :content => "http://www.opensuse.org/"
assert_xml_tag :tag => "icon", :attributes => { :url => "https://static.opensuse.org/distributions/logos/opensuse-12.2-8.png", :width => "8", :height => "8" }
end
end

0 comments on commit ace6b06

Please sign in to comment.