module ReplacerBot

Constants

VERSION

Public Class Methods

article(string) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/replacer_bot/helpers.rb, line 72
def self.despace string
  string.gsub(/\n+/, ' ').gsub(/ +/, ' ').strip
end
encode(term: URI.encode "\" click to toggle source
# File lib/replacer_bot/helpers.rb, line 15
def self.encode term:
  URI.encode "\"#{term}\""
end
encode_entities(string) click to toggle source
# 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) click to toggle source
# 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: return true unless Config.instance.config.extra_search_terms) click to toggle source
# 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) click to toggle source
# File lib/replacer_bot/helpers.rb, line 19
def self.is_hashtag word
  word[0] == '#'
end
last_tweet() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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