Class: ReplacerBot::Replacer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReplacer

Returns a new instance of Replacer.



5
6
7
8
9
# File 'lib/replacer_bot/replacer.rb', line 5

def initialize
  @config = Config.instance.config
  @search_term = @config.search_term
  @client = TwitterClient.client
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/replacer_bot/replacer.rb', line 3

def config
  @config
end

#resultsObject (readonly)

Returns the value of attribute results.



3
4
5
# File 'lib/replacer_bot/replacer.rb', line 3

def results
  @results
end

Instance Method Details

#saveObject



32
33
34
35
36
37
38
# File 'lib/replacer_bot/replacer.rb', line 32

def save
  if search.first
    File.open @config.save_file, 'w' do |file|
      Marshal.dump search.first.id, file
    end
  end
end

#search(count = 20) ⇒ Object



11
12
13
14
15
# File 'lib/replacer_bot/replacer.rb', line 11

def search count = 20
  @results ||= begin
    results = ReplacerBot.filter @client.search(ReplacerBot.encode(@search_term), result_type: 'recent').take(count)
  end
end

#tweetObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/replacer_bot/replacer.rb', line 21

def tweet
  tweets.each do |tweet|
    puts "Tweeting: #{tweet}"
    @client.update tweet
    puts "Sleeping #{@config.interval} seconds"
    sleep @config.interval
  end

  save
end

#tweetsObject



17
18
19
# File 'lib/replacer_bot/replacer.rb', line 17

def tweets
  search.map { |r| ReplacerBot.truncate ReplacerBot.replace r.text }
end