Class: Exoteric::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/exoteric/counter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Counter

Returns a new instance of Counter.

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/exoteric/counter.rb', line 19

def initialize(options = {})
  @url, @options = options[:url], options
  raise ArgumentError, "Site url not specified" if @url.to_s.empty?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/exoteric/counter.rb', line 8

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/exoteric/counter.rb', line 8

def url
  @url
end

Class Method Details

.<<(counter) ⇒ Object



10
11
12
13
# File 'lib/exoteric/counter.rb', line 10

def self.<<(counter)
  send :include, counter
  counters << counter.id
end

.countersObject



15
16
17
# File 'lib/exoteric/counter.rb', line 15

def self.counters
  @counters ||= []
end

Instance Method Details

#count(*counters) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/exoteric/counter.rb', line 24

def count(*counters)
  if counters.size == 0 || counters.first == :all
    counters = self.class.counters
  end
  
  res = {}
  sem = Mutex.new
  threads = []

  counters.each do |name|
    threads << Thread.new do 
      sem.synchronize { res[name] = send("#{name}_count") }
    end
  end

  Timeout.timeout(10) do
    threads.each(&:join)
  end
rescue Timeout::Error
  # nothing to do...
ensure
  return res.clone
end