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/seen_tweets.rb,
lib/replacer_bot/twitter_client.rb

Defined Under Namespace

Classes: CLI, Config, Replacer, SeenTweets, TwitterClient

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.article(string) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/replacer_bot/helpers.rb', line 97

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



64
65
66
67
68
69
70
# File 'lib/replacer_bot/helpers.rb', line 64

def self.dehash word
  if is_hashtag word
    return word[1..-1]
  end

  word
end

.despace(string) ⇒ Object



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

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

.encode(term:) ⇒ Object



15
16
17
# File 'lib/replacer_bot/helpers.rb', line 15

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

.encode_entities(string) ⇒ Object



23
24
25
26
# File 'lib/replacer_bot/helpers.rb', line 23

def self.encode_entities string
  coder = HTMLEntities.new
  coder.decode string
end

.filter(list:, ignore_spaces: true) ⇒ Object



49
50
51
52
# File 'lib/replacer_bot/helpers.rb', line 49

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

.has_extra_terms(string:) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/replacer_bot/helpers.rb', line 54

def self.has_extra_terms string:
  return true unless Config.instance.config.extra_search_terms

  Config.instance.config.extra_search_terms.each do |term|
    return true if string.downcase.index term.downcase
  end

  false
end

.is_hashtag(word) ⇒ Object



19
20
21
# File 'lib/replacer_bot/helpers.rb', line 19

def self.is_hashtag word
  word[0] == '#'
end

.last_tweetObject



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

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/replacer_bot/helpers.rb', line 128

def self.replace string:, subs: Config.instance.config.replacements
  # Something about a frozen string
  our_string = string.dup
  subs.map { |s| self.replacement_caser(s) }.flatten.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, replace
      end
    end
  end

  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_caser(replacement) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/replacer_bot/helpers.rb', line 112

def self.replacement_caser replacement
  l = []
  l.push({replacement.first[0].upcase => replacement.first[1].upcase})
  l.push({replacement.first[0].downcase => replacement.first[1]})
  l.push({replacement.first[0].titlecase => replacement.first[1].titlecase})

  l.uniq
end

.replacement_extender(replacement) ⇒ Object



121
122
123
124
125
126
# File 'lib/replacer_bot/helpers.rb', line 121

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

.title_case(string) ⇒ Object



76
77
78
79
80
81
# File 'lib/replacer_bot/helpers.rb', line 76

def self.title_case string
  bits = string.split ''
  bits[0] = UnicodeUtils.upcase(bits[0])

  bits.join ''
end

.truncate(text) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/replacer_bot/helpers.rb', line 83

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



38
39
40
41
42
43
44
45
46
47
# File 'lib/replacer_bot/helpers.rb', line 38

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] == '@'
  return false unless self.has_extra_terms(string: string)

  term = term.gsub ' ', ' ?' if ignore_spaces
  return true if string.index(/#{term}/i) && SeenTweets.validate(string)

  false
end