Class: Twuckoo::Runner
- Inherits:
-
Object
- Object
- Twuckoo::Runner
- Defined in:
- lib/twuckoo/runner.rb
Instance Attribute Summary collapse
-
#tweets_sent ⇒ Object
Returns the value of attribute tweets_sent.
Instance Method Summary collapse
- #config ⇒ Object
- #get_config_values_from_file(file = 'config/twuckoo.yml') ⇒ Object
-
#initialize(module_name, twitter_module = nil, args = []) ⇒ Runner
constructor
A new instance of Runner.
- #name ⇒ Object
- #notify ⇒ Object
- #parse_options!(args) ⇒ Object
-
#run ⇒ Object
the idea is to include a module with a well-defined API with three methods: - load_tweets - next - store(tweet).
- #setup {|config| ... } ⇒ Object
- #setup_from_file ⇒ Object
- #tweet(message) ⇒ Object
- #wait ⇒ Object
- #wait_between_tweets? ⇒ Boolean
Constructor Details
#initialize(module_name, twitter_module = nil, args = []) ⇒ Runner
Returns a new instance of Runner.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/twuckoo/runner.rb', line 32 def initialize(module_name, twitter_module=nil, args=[]) unless module_name.nil? _module = self.class.get_module(module_name) class_eval { include _module } end @tweets_sent = 0 @twitter_module = twitter_module.nil? ? Twuckoo::TwitterOauth : twitter_module @options = OpenStruct.new (args) setup_for_module end |
Instance Attribute Details
#tweets_sent ⇒ Object
Returns the value of attribute tweets_sent.
30 31 32 |
# File 'lib/twuckoo/runner.rb', line 30 def tweets_sent @tweets_sent end |
Instance Method Details
#config ⇒ Object
96 97 98 |
# File 'lib/twuckoo/runner.rb', line 96 def config @config ||= ::Twuckoo::Config.new end |
#get_config_values_from_file(file = 'config/twuckoo.yml') ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/twuckoo/runner.rb', line 100 def get_config_values_from_file(file='config/twuckoo.yml') begin open(file, 'r') do |f| YAML.load(f.read) end rescue {} end end |
#name ⇒ Object
65 66 67 |
# File 'lib/twuckoo/runner.rb', line 65 def name @options.name || File.split(File.dirname(__FILE__)).last end |
#notify ⇒ Object
78 79 80 |
# File 'lib/twuckoo/runner.rb', line 78 def notify send_email(name, config) end |
#parse_options!(args) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/twuckoo/runner.rb', line 44 def (args) opts = OptionParser.new do |opts| opts. = "Usage: twuckoo [options] module_to_use" opts.on("-n name", "--name name", "name will be the name of this twuckoo instance (used for email notifs)") do |name| @options.name = name end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end # opts.on_tail("--version", "Show version") do # File.read(...) # exit # end opts.parse!(args) end |
#run ⇒ Object
the idea is to include a module with a well-defined API with three methods:
-
load_tweets
-
next
-
store(tweet)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/twuckoo/runner.rb', line 14 def run setup_from_file load_tweets next_tweet = self.next while (next_tweet and !tweet_limit_reached?) do tweet(next_tweet) wait if wait_between_tweets? next_tweet = self.next if next_tweet.nil? notify reset next_tweet = self.next end end end |
#setup {|config| ... } ⇒ Object
110 111 112 |
# File 'lib/twuckoo/runner.rb', line 110 def setup yield config end |
#setup_from_file ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/twuckoo/runner.rb', line 114 def setup_from_file setup do |config| get_config_values_from_file.each_pair do |attr, value| config[attr.to_sym] = value end end end |
#tweet(message) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/twuckoo/runner.rb', line 82 def tweet() unless .nil? or .empty? begin send_tweet(, config) rescue twitter_module.exception return else inc_tweet_counter store() end end end |
#wait ⇒ Object
73 74 75 76 |
# File 'lib/twuckoo/runner.rb', line 73 def wait seconds_to_sleep = DurationString.to_seconds(config[:time_to_sleep]) sleep(seconds_to_sleep) end |
#wait_between_tweets? ⇒ Boolean
69 70 71 |
# File 'lib/twuckoo/runner.rb', line 69 def wait_between_tweets? config[:time_to_sleep] != "0" end |