Class: Botolo::API::Tweet

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/botolo/api/tweet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#twittersObject (readonly)

twitters is a twitter client array read from configuration file and used to access the social network for a given account.



10
11
12
# File 'lib/botolo/api/tweet.rb', line 10

def twitters
  @twitters
end

Instance Method Details

#authenticate(config) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/botolo/api/tweet.rb', line 13

def authenticate(config)
  @twitters = []
  config['accounts'].each do ||
    a=Hash.new
    a[:name] = ['name']
    begin
      a[:client] = Twitter::REST::Client.new do |config|
        config.consumer_key         = ['consumer_key']
        config.consumer_secret      = ['consumer_secret']

        config.access_token         = ['access_token']        unless ['access_token'].nil?
        config.access_token_secret  = ['access_token_secret'] unless ['access_token_secret'].nil?
      end
      $logger.ok "#{a[:name]} authenticated successfully"
    rescue Exception => e
      $logger.err "can't authenticate #{a[:name]} (#{e.message})"
    end

    @twitters << a
  end

  @twitters
end

#find_and_retweet_topic(limit = 5, topic) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/botolo/api/tweet.rb', line 58

def find_and_retweet_topic(limit = 5, topic)
  list = []
  @twitters.each do |tt|
    list << tt[:client].search(topic).to_a
  end

  unless list.nil?
    (0..limit-1).each do |l|
      index = SecureRandom.random_number(list[0].count)
      tweet = list[0][index]
      begin
        @twitters.each do |t|
          $logger.debug("retwetting: #{tweet.text}")
          t[:client].retweet(tweet)
        end
      rescue => e
        $logger.err("error tweeting #{tweet.text}: #{e.message}")
      end
      sleep(15)
    end
  end
end

#retweet(name = nil, msg) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/botolo/api/tweet.rb', line 50

def retweet(name=nil, msg)
  return nil if msg.empty?
  @twitters.each do |t|
    t[:client].retweet(msg) if (name.nil? or (!name.nil? and name == t[:name]))
  end
  return msg
end

#tweet(name = nil, msg) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/botolo/api/tweet.rb', line 37

def tweet(name=nil, msg)
  return nil if msg.empty?
  @twitters.each do |t|
    $logger.debug "#{t[:name]} sending #{msg}"
    begin
      t[:client].update(msg) if (name.nil? or (!name.nil? and name == t[:name]))
    rescue => e
      $logger.err "#{e.message}"
    end
  end
  return msg
end