ls = LineStream.new(true)
bl = ls.retrieve
line_msg = ls.messagize(bl, false) # long format
reformats = 0
valid_msg = nil
while reformats < 2
if ls.valid_for_twitter?(line_msg) then
valid_msg = true
break # break from while entirely
else
valid_msg = false
line_msg = ls.messagize(bl, true) # short format
reformats += 1
end
end
if !valid_msg then
ExceptionNotifier.notify(:error_msg => "Twitter Message Too Long - #{line_msg} is #{line_msg.length} chars")
end
# posting to Twitter...
Refactorings
No refactoring yet !
spaghetticode
June 25, 2010, June 25, 2010 22:10, permalink
Guess it's not what you expected, but maybe it can help you go in the right direction...
class LineStream
def initialize(bool)
# ...
end
def retrieve
# ...
end
def messagize(bl, bool)
if bool
Message.new('short_message'*7)
else
Message.new('message_too_long!!'*10)
end
end
def long_message
@long_message ||= messagize(retrieve, false)
end
def short_message
@short_message ||= messagize(retrieve, true)
end
end
class Message
attr_reader :body
def initialize(body)
@body = body
end
def size
body.size
end
def valid_for_twitter?
body.size <= 140
end
end
def twitter_message
ls = LineStream.new(true)
if ls.long_message.valid_for_twitter?
post ls.long_message.body
elsif ls.short_message.valid_for_twitter?
post ls.short_message.body
else
hoptoad_notify(ls.short_message)
end
end
def post(msg)
msg # posts message somewhere
end
def hoptoad_notify(msg)
ExceptionNotifier.notify(:error_msg => "Twitter Message Too Long - #{msg} is #{msg.size} chars")
end
arvanasse
June 30, 2010, June 30, 2010 13:41, permalink
Really like the way spaghetticode has broken this up. The only thing that I would change is the twitter_message method that seems to beg for a nice case statement....
def twitter_message
ls = LineStream.new(true)
case
when ls.long_message.valid_for_twitter?
post ls.long_message.body
when ls.short_message.valid_for_twitter?
post ls.short_message.body
else
hoptoad_notify(ls.short_message)
end
end
Josh
July 7, 2010, July 07, 2010 22:50, permalink
ls = LineStream.new(true)
bl = ls.retrieve
msg = [lambda {@long ||= ls.messagize(bl, false)}, lambda {@short ||= ls.messagize(bl, true)}]
msg = msg.find {|m| m.call.length < 140}
unless msg.nil
post msg.call
else
hoptoad_notify @short
end
def post(msg)
msg # posts message somewhere
end
def hoptoad_notify(msg)
ExceptionNotifier.notify(:error_msg => "Twitter Message Too Long - #{msg} is #{msg.size} chars")
end
I have an ActivityStream that sends messages to Twitter. There is a short and long format for each message. It's possible that both could be over 140 chars, but the short format has much lower probability. By default, I start with the long version, then if that is over 140 chars, I use the short version. Again, there's a chance the short could be over 140 chars. If it is, I need to send notification to hoptoad.
I could use some help on the logic and control structure to use to accomplish the following:
1. The streamer retrieves the long message format.
2. Checks whether it's valid size.
3. If so, message is posted.
4. If it's over 140 chars, then short message format is retrieved.
5. Checks whether it's valid size.
6. If so, message is posted.
7. If not, hoptoad notification is sent.