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.0.14"

Class Method Summary collapse

Class Method Details

.article(string) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/replacer_bot/helpers.rb', line 86

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



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

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

  word
end

.despace(string) ⇒ Object



61
62
63
# File 'lib/replacer_bot/helpers.rb', line 61

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



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

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

.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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/replacer_bot/helpers.rb', line 117

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



101
102
103
104
105
106
107
108
# File 'lib/replacer_bot/helpers.rb', line 101

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



110
111
112
113
114
115
# File 'lib/replacer_bot/helpers.rb', line 110

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



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

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

  bits.join ''
end

.truncate(text) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/replacer_bot/helpers.rb', line 72

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
# 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] == '@'

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

  false
end