Class: Virility::Strategy

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Supporter
Defined in:
lib/virility/strategy.rb

Direct Known Subclasses

Facebook, Linkedin, Pinterest, PlusOne, Reddit, StumbleUpon

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) ⇒ Strategy

Returns a new instance of Strategy.



8
9
10
11
12
# File 'lib/virility/strategy.rb', line 8

def initialize url
  @original_url = url
  @url = encode(url)
  @results = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



82
83
84
85
86
87
88
# File 'lib/virility/strategy.rb', line 82

def method_missing(name, *args, &block)
  if result_exists?(name)
    get_result(name)
  else
    0
  end
end

Instance Attribute Details

#original_urlObject

Returns the value of attribute original_url.



6
7
8
# File 'lib/virility/strategy.rb', line 6

def original_url
  @original_url
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/virility/strategy.rb', line 6

def response
  @response
end

#resultsObject

Returns the value of attribute results.



6
7
8
# File 'lib/virility/strategy.rb', line 6

def results
  @results
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/virility/strategy.rb', line 6

def url
  @url
end

Instance Method Details

#call_strategyObject

Call Strategy



39
40
41
# File 'lib/virility/strategy.rb', line 39

def call_strategy
  @response = census
end

#censusObject

Abstract Methods - Delete eventually



18
19
20
# File 'lib/virility/strategy.rb', line 18

def census
  raise "Abstract Method census called on #{self.class} - Please define this method"
end

#collect_resultsObject

Results



47
48
49
50
51
52
53
# File 'lib/virility/strategy.rb', line 47

def collect_results
  if respond_to?(:outcome)
    @results = valid_response_test ? outcome : {}
  else
    @results = valid_response_test ? @response.parsed_response : {}
  end
end

#countObject



22
23
24
# File 'lib/virility/strategy.rb', line 22

def count
  raise "Abstract Method count called on #{self.class} - Please define this method"
end

#get_result(key) ⇒ Object

Dynamic Methods



70
71
72
73
74
75
76
# File 'lib/virility/strategy.rb', line 70

def get_result key
  if result_exists?(key)
    results[key.to_s]
  else
    0
  end
end

#pollObject

Poll



30
31
32
33
# File 'lib/virility/strategy.rb', line 30

def poll
  call_strategy
  collect_results
end

#result_exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/virility/strategy.rb', line 78

def result_exists? key
  !results[key.to_s].nil?
end

#valid_response_testObject

Parsed Response Test - Overwrite if needed



94
95
96
# File 'lib/virility/strategy.rb', line 94

def valid_response_test
  @response.respond_to?(:parsed_response) and @response.parsed_response.is_a?(Hash)
end