Class: Robotwitter::Robot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, section, &getter) ⇒ Robot

getter should be a lambda - function which returns string example of getter

SQLITE_GETTER = lambda do
  db = SQLite3::Database.new("database.db")
  db.get_first_row( "select * from table" )
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/robotwitter.rb', line 24

def initialize(path, section, &getter)
  @getter, @path, @followers_ids, @following_ids = getter, path, nil, nil

  @logger = Logger.new('tweelog.txt', 'weekly')


  path ||= ''
  path += '/' if path != ''

  yml = YAML.load_file(path + "settings.yml")
  Twitter.configure do |config|
    config.consumer_key       = yml[section]['consumer_key']
    config.consumer_secret    = yml[section]['consumer_secret']
    config.oauth_token        = yml[section]['oauth_token']
    config.oauth_token_secret = yml[section]['oauth_token_secret']
  end
  @client = Twitter::Client.new
  @search_client = Twitter
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/robotwitter.rb', line 14

def path
  @path
end

Instance Method Details

#follow_all_backObject

follow who follows me



45
46
47
48
49
50
51
# File 'lib/robotwitter.rb', line 45

def follow_all_back
  follow_them = get_followers_ids - get_following_ids
  follow_them.each do |id|
    @client.follow(id)
    @logger.info 'following' + id.to_s
  end
end

#follow_users_tweets_about(word) ⇒ Object

follow who tweet about word



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/robotwitter.rb', line 74

def follow_users_tweets_about(word)
  users = search_users_tweets_about(word)

  get_followers_ids
  get_following_ids
  @logger.info(users)
  users.each do |user|
    id = user['from_user_id']
    name = user['from_user']
    if (not @followers_ids.include?(id)) and (not @following_ids.include?(id))
      @client.follow(name)
      @logger.info(name)
    end
  end
end

#retweet_about(word) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/robotwitter.rb', line 62

def retweet_about(word)
  search = search_users_tweets_about(word, 2)
  init_db
  search.each do |result|
    next if @db.retweeted?(result)
    retweet(result)
    @db.save_retweet(result)
    @logger.info(result['id'])
  end
end

#send_message(pattern) ⇒ Object

string ‘msg somth’



54
55
56
57
58
59
60
# File 'lib/robotwitter.rb', line 54

def send_message(pattern)
  phrase = get_phrase
  return if phrase == ''
  send = pattern.gsub('_msg_', phrase)
  @client.update(send)
  @logger.info(send)
end

#unfollow_usersObject

unfollow who not following me



91
92
93
94
95
96
97
# File 'lib/robotwitter.rb', line 91

def unfollow_users
  unfollow_them = get_following_ids - get_followers_ids
  unfollow_them.each do |id|
    @client.unfollow(id)
    @logger.info(id)
  end
end