Skip to content

Instantly share code, notes, and snippets.

@valarpirai
Created January 31, 2023 11:13
Show Gist options
  • Save valarpirai/2ad3762f115e9e8fc153dcc563e686f4 to your computer and use it in GitHub Desktop.
Save valarpirai/2ad3762f115e9e8fc153dcc563e686f4 to your computer and use it in GitHub Desktop.
SMS break messages | Twilio
MAX_LINE_LENGTH = 80
# Gets long string as input
# Retruns array of splitted strings with (1/3)
def split_msg(message)
length = message.length
return [message] if length < MAX_LINE_LENGTH
new_message = []
temp_start = temp_end = 0
length.times do |index|
if message[index].eql?(' ')
temp_end = index
elsif index - temp_start >= MAX_LINE_LENGTH
temp_end += 1
new_message << message[temp_start, (temp_end - temp_start)]
temp_start = temp_end
end
if index == length - 1
new_message << message[temp_start, index - temp_start + 1]
end
end
new_message.each.with_index(1) do |s, index|
s << " " if !s[-1].eql?(' ')
s << "(#{index}/#{new_message.length})" if s[-1].eql?(' ')
end
new_message
end
message = "Instead of routing texts through cell towers, your app sends messages via helllo to the phones of nearby users, passing each message along from one phone to the next until it reaches the intended recipient"
puts split_msg(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment