class Engagement::CommentCounter::Threaded

Public Class Methods

new(places) click to toggle source
# File lib/engagement/comment_counter/threaded.rb, line 5
def initialize(places)
  @places = places
  @mutex = Mutex.new
end

Public Instance Methods

comments_count(url) click to toggle source
# File lib/engagement/comment_counter/threaded.rb, line 10
def comments_count(url)
  comments_count = 0
  threads = []

  @places.each do |place|
    threads << Thread.new do
      count = place.comments_count(url)
      
      @mutex.synchronize do
        comments_count += count
      end
    end
  end

  threads.each(&:join)
  comments_count
end