Module: ReplacerBot

Defined in:
lib/replacer_bot/cli.rb,
lib/replacer_bot/config.rb,
lib/replacer_bot/helpers.rb,
lib/replacer_bot/version.rb,
lib/replacer_bot/replacer.rb,
lib/replacer_bot/twitter_client.rb

Defined Under Namespace

Classes: CLI, Config, Replacer, TwitterClient

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.article(string) ⇒ Object



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

def self.article string
  string = self.dehash string
  specials = [ 'hotel' ]

  if specials.include? string.downcase
    return 'an'
  end

  if 'aeiou'.include? string[0].downcase
    return 'an'
  end

  'a'
end

.dehash(word) ⇒ Object



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

def self.dehash word
  if word[0] == '#'
    return word[1..-1]
  end

  word
end

.despace(string) ⇒ Object



39
40
41
# File 'lib/replacer_bot/helpers.rb', line 39

def self.despace string
  string.gsub(/\n+/, ' ').gsub(/ +/, ' ').strip
end

.encode(term) ⇒ Object



2
3
4
# File 'lib/replacer_bot/helpers.rb', line 2

def self.encode term
  URI.encode "\"#{term}\""
end

.filter(list) ⇒ Object



26
27
28
29
# File 'lib/replacer_bot/helpers.rb', line 26

def self.filter list
  list.select { |i| self.validate i.text, Config.instance.config.search_term }.
    select { |i| i.id > self.last_tweet}
end

.last_tweetObject



6
7
8
9
10
11
12
13
14
# File 'lib/replacer_bot/helpers.rb', line 6

def self.last_tweet
  begin
    Marshal.load File.read Config.instance.config.save_file
  rescue ArgumentError
    0
  rescue Errno::ENOENT
    0
  end
end

.replace(string, subs = Config.instance.config.replacements) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/replacer_bot/helpers.rb', line 79

def self.replace string, subs = Config.instance.config.replacements
  # Something about a frozen string
  our_string = string.dup
  subs.each do |substitute|
    (self.replacement_extender(substitute) << substitute).each do |s|
      s.each do |search, replace|
        our_string = self.despace our_string
        our_string.gsub! /#{search}/i, replace
      end
    end
  end

  our_string
end

.replacement_extender(replacement) ⇒ Object



72
73
74
75
76
77
# File 'lib/replacer_bot/helpers.rb', line 72

def self.replacement_extender replacement
  [
    replacement.map { |k, v| {"#{self.article k} #{k}" => "#{self.article v} #{v}"} }[0],
    replacement.map { |k, v| {"#{self.article k} ##{k}" => "#{self.article v} ##{v}"} }[0]
  ]
end

.truncate(text) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/replacer_bot/helpers.rb', line 43

def self.truncate text
  return_text = ''
  text.split.each do |word|
    new_text = "#{return_text} #{word}".strip
    if new_text.length > TWITTER_LIMIT
      return return_text
    else
      return_text = new_text
    end
  end

  return_text
end

.validate(string, term = Config.instance.config.search_term, ignore_spaces = true) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/replacer_bot/helpers.rb', line 16

def self.validate string, term = Config.instance.config.search_term, ignore_spaces = true
  return false if string[0...2] == 'RT'
  return false if string[0] == '@'

  term = term.gsub ' ', ' ?' if ignore_spaces
  return true if string.index /#{term}/i

  false
end