Skip to content

Commit

Permalink
Now I don't want to kill myself when I read the Status code anymore.
Browse files Browse the repository at this point in the history
TODO: test  build_twitter_message and build_short_link and find a way to stub API calls.
  • Loading branch information
Frédéric de Villamil committed Aug 19, 2013
1 parent 8f10197 commit c4a3743
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/statuses_controller.rb
Expand Up @@ -51,7 +51,7 @@ def new_or_edit
if @status.save
flash[:notice] = _("Status was successfully %s.", message)
if params[:status][:push_to_twitter] and params[:status][:push_to_twitter] != "0" and (@status.twitter_id.nil? or @status.twitter_id.empty?)
unless @status.send_to_twitter(current_user)
unless @status.send_to_twitter
flash[:error] = _("Oooops something wrong happened")
flash[:notice] = nil
end
Expand Down
67 changes: 43 additions & 24 deletions app/models/status.rb
Expand Up @@ -15,12 +15,16 @@ class Status < Content
belongs_to :user
validates_presence_of :body
validates_uniqueness_of :permalink
attr_accessor :push_to_twitter
attr_accessor :push_to_twitter, :twitter_message

after_create :set_permalink, :shorten_url

default_scope order("published_at DESC")

TWITTER_FTP_URL_LEGTH = 19
TWITTER_HTTP_URL_LENGTH = 20
TWITTER_HTTPS_URL_LENGTH = 21

def set_permalink
self.permalink = "#{self.id}-#{self.body.to_permalink[0..79]}" if self.permalink.nil? or self.permalink.empty?
self.save
Expand All @@ -45,28 +49,20 @@ def initialize(*args)
end
end

def send_to_twitter(user)
blog = Blog.default
return if blog.twitter_consumer_key.nil? or blog.twitter_consumer_secret.nil?
return unless user.twitter_configured?
def send_to_twitter
return false unless self.push_to_twitter # Then, what are we doing here?!
return false unless Blog.default.has_twitter_configured?
return false unless self.user.has_twitter_configured?

twitter = Twitter::Client.new(
:consumer_key => blog.twitter_consumer_key,
:consumer_secret => blog.twitter_consumer_secret,
:oauth_token => user.twitter_oauth_token,
:oauth_token_secret => user.twitter_oauth_token_secret
:consumer_key => Blog.default.twitter_consumer_key,
:consumer_secret => Blog.default.twitter_consumer_secret,
:oauth_token => self.user.twitter_oauth_token,
:oauth_token_secret => self.user.twitter_oauth_token_secret
)

message = self.body.strip_html

length = calculate_real_length(message)
short_permalink = build_short_link(length)
build_twitter_message

if length > 114
message = "#{message[0..113]}#{short_permalink}"
else
message = "#{message}#{short_permalink}"
end

begin
options = {}

Expand All @@ -75,12 +71,12 @@ def send_to_twitter(user)
self.in_reply_to_message = twitter.status(self.in_reply_to_status_id).to_json
end

tweet = twitter.update(message, options)
self.twitter_id = tweet.attrs[:id_str]
tweet = twitter.update(self.message, options)
self.twitter_id = tweet.attrs[:id_str]

self.save

user.update_twitter_profile_image(tweet.attrs[:user][:profile_image_url])
self.user.update_twitter_profile_image(tweet.attrs[:user][:profile_image_url])
return true
rescue
return false
Expand All @@ -104,10 +100,21 @@ def permalink_url(anchor=nil, only_path=false)
end

private
def calculate_real_length(message)
def calculate_real_length
message = self.twitter_message

uris = URI.extract(message, ['http', 'https', 'ftp'])
uris.each do |uri|
message = message.gsub(uri, "---------------------")
case uri.split(":")[0]
when "http"
payload = "-" * TWITTER_HTTP_URL_LENGTH
when "https"
payload = "-" * TWITTER_HTTPS_URL_LENGTH
when "ftp"
payload = "-" * TWITTER_FTP_URL_LEGTH
end

message = message.gsub(uri, payload)
end

return message.length
Expand All @@ -124,4 +131,16 @@ def build_short_link(length)
return " (#{prefix} #{path})"
end
end

def build_twitter_message
self.twitter_message = self.body.strip_html

length = calculate_real_length

if length > 114
self.twitter_message = "#{self.twitter_message[0..113]}#{build_short_link(length)}"
else
self.twitter_message = "#{self.twitter_message}#{build_short_link(length)}"
end
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -203,7 +203,7 @@ def generate_password!
self.password = newpass
end

def twitter_configured?
def has_twitter_configured?
return false if self.twitter_oauth_token.nil? or self.twitter_oauth_token.empty?
return false if self.twitter_oauth_token_secret.nil? or self.twitter_oauth_token_secret.empty?
true
Expand Down
77 changes: 77 additions & 0 deletions spec/models/status_spec.rb
Expand Up @@ -112,3 +112,80 @@ def valid_attributes
@status.default_text_filter.name.should == Blog.default.text_filter
end
end

describe "Checking Twitter message length..." do
it "A twitter message without URL should not be changed" do
status = FactoryGirl.build(:status, twitter_message: "A message without URL")
status.instance_eval{ calculate_real_length }.should == 21
end

it "A twitter message with a short http URL should have its URL expanded to 20 chars" do
status = FactoryGirl.build(:status, twitter_message: "A message with a short URL http://foo.com")
status.instance_eval{ calculate_real_length }.should == 47
end

it "A twitter message with a short https URL should have its URL expanded to 21 chars" do
status = FactoryGirl.build(:status, twitter_message: "A message with a short URL https://foo.com")
status.instance_eval{ calculate_real_length }.should == 48
end

it "A twitter message with a short https URL should have its URL expanded to 19 chars" do
status = FactoryGirl.build(:status, twitter_message: "A message with a short URL ftp://foo.com")
status.instance_eval{ calculate_real_length }.should == 46
end

it "A twitter message with a long http URL should have its URL shortened to 20 chars" do
status = FactoryGirl.build(:status, twitter_message: "A message with a long URL http://foobarsomething.com?blablablablabla")
status.instance_eval{ calculate_real_length }.should == 46
end

it "A twitter message with a short https URL should have its URL expanded to 21 chars" do
status = FactoryGirl.build(:status, twitter_message: "A message with a long URL https://foobarsomething.com?blablablablabla")
status.instance_eval{ calculate_real_length }.should == 47
end

it "A twitter message with a short https URL should have its URL expanded to 19 chars" do
status = FactoryGirl.build(:status, twitter_message: "A message with a long URL ftp://foobarsomething.com?blablablablabla")
status.instance_eval{ calculate_real_length }.should == 45
end
end

describe 'Pushing a status to Twitter' do
before :each do
Blog.delete_all
end

it 'A status without push to twitter defined should not push to Twitter' do
FactoryGirl.create(:blog)
status = FactoryGirl.build(:status, :push_to_twitter => 0)
status.send_to_twitter.should == false
end

it 'a non configured blog and non configured user should not send a status to Twitter' do
FactoryGirl.create(:blog)
status = FactoryGirl.create(:status)
status.send_to_twitter.should == false
end

it 'a configured blog and non configured user should not send a status to Twitter' do
FactoryGirl.build(:blog, twitter_consumer_key: "12345", twitter_consumer_secret: "67890")
user = FactoryGirl.build(:user)
status = FactoryGirl.build(:status, user: user)
status.send_to_twitter.should == false
end

it 'a non configured blog and a configured user should not send a status to Twitter' do
FactoryGirl.build(:blog)
user = FactoryGirl.build(:user, twitter_oauth_token: "12345", twitter_oauth_token_secret: "67890")
status = FactoryGirl.build(:status, user: user)
status.send_to_twitter.should == false
end

it 'a configured blog and a configured user should send a status to Twitter' do
FactoryGirl.build(:blog, twitter_consumer_key: "12345", twitter_consumer_secret: "67890")
user = FactoryGirl.build(:user, twitter_oauth_token: "12345", twitter_oauth_token_secret: "67890")
status = FactoryGirl.build(:status, user: user)
pending "Need to find a way to fake the Twitter API behavior"
# status.send_to_twitter.should == false
end
end
42 changes: 42 additions & 0 deletions spec/models/user_spec.rb
Expand Up @@ -327,4 +327,46 @@ def set_password(newpass)
it { expect(user.display_names).to eq([user.login, user.firstname, user.lastname, "#{user.firstname} #{user.lastname}"]) }
end
end

describe "User's Twitter configuration" do
it "A user without twitter_oauth_token or twitter_oauth_token_secret should not have Twitter configured" do
user = FactoryGirl.build(:user)
user.has_twitter_configured?.should == false
end

it "A user with an empty twitter_oauth_token and no twitter_oauth_token_secret should not have Twitter configured" do
user = FactoryGirl.build(:user, twitter_oauth_token: "")
user.has_twitter_configured?.should == false
end

it "A user with an empty twitter_oauth_token and an empty twitter_oauth_token_secret should not have Twitter configured" do
user = FactoryGirl.build(:user, twitter_oauth_token: "", twitter_oauth_token_secret: "")
user.has_twitter_configured?.should == false
end

it "A user with a twitter_oauth_token and no twitter_oauth_token_secret should not have Twitter configured" do
user = FactoryGirl.build(:user, twitter_oauth_token: "12345")
user.has_twitter_configured?.should == false
end

it "A user with a twitter_oauth_token and an empty twitter_oauth_token_secret should not have Twitter configured" do
user = FactoryGirl.build(:user, twitter_oauth_token: "12345", twitter_oauth_token_secret: "")
user.has_twitter_configured?.should == false
end

it "A user with a twitter_oauth_token_secret and no twitter_oauth_token should not have Twitter configured" do
user = FactoryGirl.build(:user, twitter_oauth_token_secret: "67890")
user.has_twitter_configured?.should == false
end

it "A user with a twitter_oauth_token_secret and an empty twitter_oauth_token should not have Twitter configured" do
user = FactoryGirl.build(:user, twitter_oauth_token_secret: "67890", twitter_oauth_token: "")
user.has_twitter_configured?.should == false
end

it "A user with a twitter_oauth_token and a twitter_oauth_token_secret should have Twitter configured" do
user = FactoryGirl.build(:user, twitter_oauth_token: "12345", twitter_oauth_token_secret: "67890")
user.has_twitter_configured?.should == true
end
end
end

0 comments on commit c4a3743

Please sign in to comment.