Class: Virility::Excitation

Inherits:
Object
  • Object
show all
Includes:
Supporter
Defined in:
lib/virility/excitation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Supporter

#camelize, #encode, #escaped_url, #get_class_string, #symbolize_for_key, #underscore

Constructor Details

#initialize(url, strategies = []) ⇒ Excitation

Initialization



10
11
12
13
14
15
16
17
18
# File 'lib/virility/excitation.rb', line 10

def initialize(url, strategies = [])
  @url = url
  @strategies = {}
  @results = {}
  @counts = {}
  @filter_strategies = strategies || []
  collect_strategies
  filter_strategies
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/virility/excitation.rb', line 101

def method_missing(name, *args, &block)
  if strategy_exists?(name)
    get_strategy(name)
  else
    raise UnknownStrategy, "#{name} Is Not A Known Strategy"
  end
end

Instance Attribute Details

#countsObject

Return Collected Counts as a Hash



45
46
47
# File 'lib/virility/excitation.rb', line 45

def counts
  @counts
end

#resultsObject

Returns the value of attribute results.



5
6
7
# File 'lib/virility/excitation.rb', line 5

def results
  @results
end

#strategiesObject

Returns the value of attribute strategies.



5
6
7
# File 'lib/virility/excitation.rb', line 5

def strategies
  @strategies
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/virility/excitation.rb', line 5

def url
  @url
end

Instance Method Details

#attributesObject

Reflection



81
82
83
# File 'lib/virility/excitation.rb', line 81

def attributes
  {:url => @url, :available_strategies => @strategies.keys}
end

#collect_strategiesObject

Gather all of the Strategies



68
69
70
# File 'lib/virility/excitation.rb', line 68

def collect_strategies
  Dir["#{File.dirname(__FILE__)}/strategies/**/*.rb"].each { |klass| @strategies[get_class_string(klass).to_sym] = Virility.const_get(camelize(get_class_string(klass))).new(@url) }
end

#filter_strategiesObject



72
73
74
75
# File 'lib/virility/excitation.rb', line 72

def filter_strategies
  return if @filter_strategies.empty?
  @strategies.select! { |k, _v| @filter_strategies.include?(k) }
end

#get_response(strategy) ⇒ Object



37
38
39
# File 'lib/virility/excitation.rb', line 37

def get_response(strategy)
  @strategies[strategy].response if @strategies[strategy]
end

#get_strategy(strategy) ⇒ Object

Dynamic Methods



89
90
91
92
93
94
95
# File 'lib/virility/excitation.rb', line 89

def get_strategy strategy
  if strategy_exists?(strategy)
    @strategies[strategy.to_sym]
  else
    raise UnknownStrategy, "#{strategy} Is Not A Known Strategy"
  end
end

#pollObject

Get Virility from all of the Strategies



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/virility/excitation.rb', line 24

def poll
  if @results.empty?
    @strategies.each do |name, strategy|
      begin
        @results[symbolize_for_key(strategy)] = strategy.poll
      rescue => e
        puts "[virility#poll] #{strategy.class.to_s} => #{e}"
      end
    end
  end
  @results
end

#strategy_exists?(strategy) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/virility/excitation.rb', line 97

def strategy_exists? strategy
  !@strategies[strategy.to_sym].nil?
end

#total_virilityObject Also known as: total



59
60
61
# File 'lib/virility/excitation.rb', line 59

def total_virility
  counts.values.inject(0) { |result, count| result + count }
end