post view count service: switch to Danbooru::Http.

This commit is contained in:
evazion
2020-06-13 21:50:06 -05:00
parent 9e29326b96
commit 1846133cd6
2 changed files with 25 additions and 102 deletions

View File

@@ -1,38 +1,25 @@
class PostViewCountService
def self.enabled?
Danbooru.config.reportbooru_server.present?
attr_reader :http, :reportbooru_server
def initialize(http: Danbooru::Http.new, reportbooru_server: Danbooru.config.reportbooru_server)
@reportbooru_server = reportbooru_server
@http = http
end
def initialize
if !PostViewCountService.enabled?
raise NotImplementedError.new("the Reportbooru service isn't configured. Post views are not available.")
end
end
def fetch_count(post_id)
url = URI.parse("#{Danbooru.config.reportbooru_server}/post_views/#{post_id}")
response = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(timeout: 6))
if response.success?
return JSON.parse(response.body)
else
return nil
end
def enabled?
reportbooru_server.present?
end
def fetch_rank(date = Date.today)
url = URI.parse("#{Danbooru.config.reportbooru_server}/post_views/rank?date=#{date}")
response = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(timeout: 6))
if response.success?
return JSON.parse(response.body)
else
return nil
end
rescue JSON::ParserError
nil
raise NotImplementedError, "Reportbooru not configured, post views not available." unless enabled?
response = http.get("#{reportbooru_server}/post_views/rank?date=#{date}")
return [] if response.status != 200
JSON.parse(response.to_s)
end
def popular_posts(date = Date.today)
ranking = fetch_rank(date) || []
ranking = fetch_rank(date)
ranking.slice(0, 50).map {|x| Post.find(x[0])}
end
end

View File

@@ -2,88 +2,24 @@ require 'test_helper'
class PostViewCountServiceTest < ActiveSupport::TestCase
def setup
super
CurrentUser.user = FactoryBot.create(:user)
CurrentUser.ip_addr = "127.0.0.1"
PostViewCountService.stubs(:enabled?).returns(true)
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:1234")
@post = FactoryBot.create(:post)
@service = PostViewCountService.new(reportbooru_server: "http://localhost:1234")
@post = create(:post)
@date = "2000-01-01"
end
def teardown
super
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
subject { PostViewCountService.new }
context "#popular_posts" do
setup do
subject.stubs(:fetch_rank).returns([[@post.id, 1]])
should "return the list of popular posts on success" do
body = "[[#{@post.id},100.0]]"
@service.http.expects(:get).with("http://localhost:1234/post_views/rank?date=#{@date}").returns(HTTP::Response.new(status: 200, body: body, version: "1.1"))
posts = @service.popular_posts(@date)
assert_equal([@post], posts)
end
should "return the posts" do
posts = subject.popular_posts
assert_equal(@post.id, posts[0].id)
end
end
should "return nothing on failure" do
@service.http.expects(:get).with("http://localhost:1234/post_views/rank?date=#{@date}").returns(HTTP::Response.new(status: 500, body: "", version: "1.1"))
context "#fetch_rank" do
context "success" do
setup do
@date = "2000-01-01"
@body = "[[1,1.0],[2,2.0]]"
stub_request(:get, "localhost:1234/post_views/rank").with(query: {"date" => @date}).to_return(body: @body)
end
should "return a list" do
json = subject.fetch_rank(@date)
assert(json.is_a?(Array))
assert_equal(1, json[0][0])
assert_equal(2, json[1][0])
end
end
context "failure" do
setup do
@date = "2000-01-01"
stub_request(:get, "localhost:1234/post_views/rank").with(query: {"date" => @date}).to_return(body: "", status: 400)
end
should "return nil" do
json = subject.fetch_rank(@date)
assert_nil(json)
end
end
end
context "#fetch_count" do
context "success" do
setup do
@body = "[[1,5],[2,20]]"
stub_request(:get, "localhost:1234/post_views/#{@post.id}").to_return(body: @body)
end
should "return a list" do
json = subject.fetch_count(@post.id)
assert(json.is_a?(Array))
assert_equal(1, json[0][0])
assert_equal(2, json[1][0])
end
end
context "failure" do
setup do
stub_request(:get, "localhost:1234/post_views/#{@post.id}").to_return(body: "", status: 400)
end
should "return nil" do
json = subject.fetch_count(@post.id)
assert_nil(json)
end
assert_equal([], @service.popular_posts(@date))
end
end
end