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(feeder_class, tweeter_module = Twuckoo::TwitterOauth, args = []) ⇒ Runner

Returns a new instance of Runner.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/twuckoo/runner.rb', line 38

def initialize(feeder_class, tweeter_module=Twuckoo::TwitterOauth, args=[])
  # tweeter_module should be a class instance instead. Then everything falls into place
  @tweeter_module = tweeter_module

  #TODO: Fail if feeder_class is nil or better: parse options first
  unless feeder_class.nil?
    feeder_class = self.class.get_feeder_class(feeder_class)
    @feeder = feeder_class.new(@tweeter_module)
  end

  #TODO: This should be stored in the tweeter, not the runner
  @tweets_sent = 0

  @options = OpenStruct.new
  parse_options!(args)
  @feeder.setup
end

Instance Attribute Details

#tweets_sentObject

Returns the value of attribute tweets_sent.



36
37
38
# File 'lib/twuckoo/runner.rb', line 36

def tweets_sent
  @tweets_sent
end

Instance Method Details

#configObject



91
92
93
# File 'lib/twuckoo/runner.rb', line 91

def config
  @config ||= ::Twuckoo::Config.new
end

#get_config_values_from_file(file = 'config/twuckoo.yml') ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/twuckoo/runner.rb', line 95

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



72
73
74
# File 'lib/twuckoo/runner.rb', line 72

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

#parse_options!(args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/twuckoo/runner.rb', line 56

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.parse!(args)
end

#runObject

the idea is to instantiate a feeder with a well-defined API:

  • setup

  • load_tweets (TODO: merge into setup)

  • next

  • store(tweet)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/twuckoo/runner.rb', line 19

def run
  setup_from_file
  @feeder.load_tweets
  next_tweet = @feeder.next
  #TODO: Fetching @feeder.next twice seems too complicated
  while (next_tweet and !tweet_limit_reached?) do
    tweet(next_tweet)
    wait if wait_between_tweets?
    next_tweet = @feeder.next
    if next_tweet.nil?
      send_email(name, config)
      @feeder.reset
      next_tweet = @feeder.next
    end
  end
end

#setup {|config| ... } ⇒ Object

Yields:



105
106
107
# File 'lib/twuckoo/runner.rb', line 105

def setup
  yield config
end

#setup_from_fileObject



109
110
111
112
113
114
115
# File 'lib/twuckoo/runner.rb', line 109

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



85
86
87
88
89
# File 'lib/twuckoo/runner.rb', line 85

def tweet(message)
  send_tweet(message, config)
  inc_tweet_counter
  @feeder.store(message)
end

#waitObject



80
81
82
83
# File 'lib/twuckoo/runner.rb', line 80

def wait
  seconds_to_sleep = Twuckoo::DurationString.to_seconds(config[:time_to_sleep])
  sleep(seconds_to_sleep)
end

#wait_between_tweets?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/twuckoo/runner.rb', line 76

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