Skip to content

Commit

Permalink
Reuse post-analyzer and cooked-document when requesting multiple post…
Browse files Browse the repository at this point in the history
… stats
  • Loading branch information
stephankaag committed Jul 22, 2013
1 parent ad2b667 commit ebd5fa7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
7 changes: 4 additions & 3 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@ def self.white_listed_image_classes
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


Expand Down
11 changes: 4 additions & 7 deletions app/models/post_analyzer.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -110,6 +103,10 @@ def link_count

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?
Expand Down

0 comments on commit ebd5fa7

Please sign in to comment.