Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Re-add @ and # before saving Pantography's own tweets
Browse files Browse the repository at this point in the history
This maintains the database integrity and ensures that we calculate the
next pantograph properly.
  • Loading branch information
joegatt committed Dec 18, 2013
1 parent 127c387 commit 114fcaa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
30 changes: 19 additions & 11 deletions app/models/pantograph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ def self.total_duration
(total_texts / (((60 * 60) / Constant.pantography.frequency) * 24 * Constant.pantography.sidereal_year_in_days));
end

def self.sanitize(text)
text.truncate(Constant.pantography.max_length, omission: '')
.gsub(/"|“|”|\‘|\’/, "'")
.gsub(/\&/, '+')
.gsub(/\[\{/, '(')
.gsub(/\]\}/, ')')
.downcase
.gsub(/[^#{ Constant.pantography.alphabet_escaped }]/, '')
def self.sanitize(text, pantographer_id = nil)
text = text.truncate(Constant.pantography.max_length, omission: '')
.gsub(/"|“|”|\‘|\’/, "'")
.gsub(/\&/, '+')
.gsub(/\[\{/, '(')
.gsub(/\]\}/, ')')
.downcase
.gsub(/[^#{ Constant.pantography.alphabet_escaped }]/, '')
if !pantography_twitter_user.blank? && pantographer_id == pantography_twitter_user.id
text = self.spamify(text)
end
text
end

def self.publish_next
Expand All @@ -72,7 +76,7 @@ def self.publish_next
def self.tweet(text)
# Replace @ and # characters before tweeting to avoid spamming other users
# These characters are unchanged as far as Pantography itself goes; they are stored intact.
text = self.unspam(text)
text = self.unspamify(text)
tweet = authenticated_twitter_client.update(text)

if tweet.text == text
Expand All @@ -91,10 +95,14 @@ def self.tweet(text)
end
end

def self.unspam(text)
def self.unspamify(text)
text.gsub(/@/, 'A').gsub(/#/, 'H')
end

def self.spamify(text)
text.gsub(/A/, '@').gsub(/H/, '#')
end

def self.report_error(error_message)
PANTOGRAPHY_LOG.error error_message
PantographMailer.tweet_failed(error_message).deliver
Expand Down Expand Up @@ -245,7 +253,7 @@ def self.get_timeline(min_id)
authenticated_twitter_client.home_timeline(trim_user: true, min_id: min_id).each do |tweet|
user = Pantographer.where(twitter_user_id: tweet.user.id).first_or_create
create(
text: sanitize(tweet.text),
text: sanitize(tweet.text, user.id),
external_created_at: tweet.created_at,
tweet_id: tweet.id,
pantographer_id: user.id
Expand Down
8 changes: 6 additions & 2 deletions spec/models/pantograph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@
specify { Pantograph.sanitize(Constant.pantography.alphabet_escaped).should eq(Pantograph.alphabet) }
end

describe '.unspam' do
Pantograph.unspam('#hashtag @mention #more @more').should == 'Hhashtag Amention Hmore Amore'
describe '.unspamify' do
Pantograph.unspamify('#hashtag @mention #more @more').should == 'Hhashtag Amention Hmore Amore'
end

describe '.spamify' do
Pantograph.spamify('Hhashtag Amention Hmore Amore').should == '#hashtag @mention #more @more'
end

describe '.previous_pantograph' do
Expand Down

0 comments on commit 114fcaa

Please sign in to comment.