Skip to content

Commit

Permalink
[api] use ActiveXML::Transport for loading remote urls
Browse files Browse the repository at this point in the history
This way we can simply ignore remote instances that are unavailable
  • Loading branch information
coolo committed Dec 22, 2012
1 parent 3dbd84a commit 1f26917
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/api/app/models/distribution.rb
Expand Up @@ -19,16 +19,6 @@ def self.parse(xmlhash)
end
end

def self.load_distributions_from_remote(prj)
# TODO: add proxy handling
url = URI.parse( prj.remoteurl + "/distributions.xml" )
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
response = http.get(url.path)
# TODO: fix response != 200
return response.body
end

def to_hash
res = self.attributes
res["icons"] = []
Expand All @@ -40,7 +30,7 @@ def to_hash

def self.all_as_hash
res = []
Distribution.all.each { |d| res << d.to_hash }
Distribution.includes(:icons).each { |d| res << d.to_hash }
return res
end

Expand All @@ -51,8 +41,9 @@ def self.all_including_remotes
list = []
remote_projects = Project.where("NOT ISNULL(projects.remoteurl)")
remote_projects.each do |prj|
xmlhash = Xmlhash.parse(self.load_distributions_from_remote(prj))
logger.debug "AIR #{prj} #{xmlhash}"
body = ActiveXML.transport.load_external_url(prj.remoteurl + "/distributions.xml")
next if body.blank? # don't let broken remote instances break us
xmlhash = Xmlhash.parse(body)
xmlhash.elements('distribution') do |d|
iconlist = []
d.elements('icon') do |i|
Expand Down
13 changes: 13 additions & 0 deletions src/api/test/functional/distributions_controller_test.rb
Expand Up @@ -2,6 +2,10 @@

class DistributionsControllerTest < ActionController::IntegrationTest
fixtures :all

teardown do
WebMock.reset!
end

test "should show distribution" do
get distribution_path(id: distributions(:one).to_param)
Expand Down Expand Up @@ -69,12 +73,21 @@ class DistributionsControllerTest < ActionController::IntegrationTest

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

test "we survive remote instances timeouts" do
stub_request(:get, "http://localhost:3200/distributions.xml").to_timeout
get "/distributions/include_remotes"
assert_response :success
puts @response.body
end
end

0 comments on commit 1f26917

Please sign in to comment.