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

@ -199,18 +199,11 @@ class PackagesController < ApplicationController
# Show an HTML partial with reviews of this package from the Ubuntu API
def reviews
expires_in 1.day, public: true
@reviews = Package.find_by_name!(params[:name]).ubuntu_reviews
# @reviews = Package.find_by_name!(params[:name]).ubuntu_reviews
@reviews = get_ubuntu_reviews :name
render '_reviews', layout: false
end
def my_uploads
# @current_users_screenshots gets filled in ApplicationController
#
# Get packages that the uploaded screenshots belong to.
@packages = Package.joins(:screenshots).where(
'screenshots.id' => @current_users_screenshots)
end
private
# Send a dummy thumbnail reading "No screenshot available. Sorry."
@ -264,4 +257,19 @@ class PackagesController < ApplicationController
session[:ip] ||= request.remote_ip
end
# Get reviews of this package from the Ubuntu API
def get_ubuntu_reviews(packagename)
# Use the URL defined in the configuration to get a JSON string
json = open(Rails.configuration.ubuntu_reviews_api_url % packagename).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
end

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.