Showing reviews on package's details page from Ubuntu

This commit is contained in:
Christoph Haas 2016-08-16 12:50:03 +02:00
parent fc95fc9d9a
commit 339bb0427f
10 changed files with 89 additions and 1 deletions

View file

@ -1,3 +1,6 @@
require 'open-uri' # allows to load URLs using open()
require 'json'
class Package < ActiveRecord::Base
# PostgreSQL-based full-text search:
# https://github.com/Casecommons/pg_search
@ -57,6 +60,20 @@ class Package < ActiveRecord::Base
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.