Class: Twuckoo::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/twuckoo/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
  parse_options!(args)
  setup_for_module
end

Instance Attribute Details

#tweets_sentObject

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

#configObject



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

#nameObject



65
66
67
# File 'lib/twuckoo/runner.rb', line 65

def name
  @options.name || File.split(File.dirname(__FILE__)).last
end

#notifyObject



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 parse_options!(args)
  opts = OptionParser.new do |opts|
    opts.banner = "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

#runObject

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

Yields:



110
111
112
# File 'lib/twuckoo/runner.rb', line 110

def setup
  yield config
end

#setup_from_fileObject



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(message)
  unless message.nil? or message.empty?
    begin
      send_tweet(message, config)
    rescue twitter_module.exception
      return message
    else
      inc_tweet_counter
      store(message)
    end
  end
  message
end

#waitObject



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

Returns:

  • (Boolean)


69
70
71
# File 'lib/twuckoo/runner.rb', line 69

def wait_between_tweets?
  config[:time_to_sleep] != "0"
end