Loading of Ubuntu reviews slightly refactored

This commit is contained in:
Christoph Haas 2017-04-05 16:27:14 +02:00
parent 29360a3086
commit 8e2ebe3cf6
2 changed files with 17 additions and 23 deletions

View file

@ -66,20 +66,6 @@ class Package < ApplicationRecord
self.screenshots.to_a.sort { |x,y| version_compare(x.version,y.version) }
end
# Get reviews of this package from the Ubuntu API
def ubuntu_reviews
# Use the URL defined in the configuration to get a JSON string
json = open(Rails.configuration.ubuntu_reviews_api_url % self.name).read
# Turn JSON into a Ruby data structure
json = JSON.parse(json)
# Only show english reviews
# TODO: Support further languages
json = json.select {|x| x['language']=='en'}
# Sort by 'usefulness_total' (how many people found this review useful)
json = json.sort { |x,y| y['usefulness_total'].to_i <=> x['usefulness_total'].to_i}
return json
end
# Return the newest screenshot that is not newer than the given version.
# This algorithm collects all image
# versions of a package and determines the (second) newest version.