Reuse post-analyzer and cooked-document when requesting multiple post stats

This commit is contained in:
Stephan Kaag 2013-07-22 22:24:47 +02:00
parent ad2b667504
commit ebd5fa76c5
2 changed files with 8 additions and 10 deletions

View File

@ -83,17 +83,18 @@ class Post < ActiveRecord::Base
end
def post_analyzer
@post_analyzer = PostAnalyzer.new(raw, topic_id)
@post_analyzers ||= {}
@post_analyzers[raw_hash] ||= PostAnalyzer.new(raw, topic_id)
end
%w{raw_mentions linked_hosts image_count attachment_count link_count raw_links}.each do |attr|
define_method(attr) do
PostAnalyzer.new(raw, topic_id).send(attr)
post_analyzer.send(attr)
end
end
def cook(*args)
PostAnalyzer.new(raw, topic_id).cook(*args)
post_analyzer.cook(*args)
end

View File

@ -1,17 +1,10 @@
class PostAnalyzer
attr_accessor :cooked, :raw
def initialize(raw, topic_id)
@raw = raw
@topic_id = topic_id
end
def cooked_document
@cooked = cook(@raw, topic_id: @topic_id)
@cooked_document = Nokogiri::HTML.fragment(@cooked)
end
# What we use to cook posts
def cook(*args)
cooked = PrettyText.cook(*args)
@ -110,6 +103,10 @@ class PostAnalyzer
private
def cooked_document
@cooked_document ||= Nokogiri::HTML.fragment(cook(@raw, topic_id: @topic_id))
end
def link_is_a_mention?(l)
html_class = l.attributes['class']
return false if html_class.nil?