Created
July 1, 2016 02:21
-
-
Save wbednarski/78fc5f7850d56afd4a0d3fe5863ae7ce to your computer and use it in GitHub Desktop.
Multi-part SMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SMS_LENGTH = 160 | |
MPM_SIZE_LONG = 16 | |
MPM_SIZE_SHORT = 14 | |
MPM_SHORT_LIMIT = 1314 | |
def send_sms_message(text, to, from) | |
unless text.length > SMS_LENGTH | |
deliver_message_via_carrier(text, to, from) | |
else | |
parts = text.scan(/.{1,#{SMS_LENGTH - (text.length > MPM_SHORT_LIMIT ? MPM_SIZE_LONG : MPM_SIZE_SHORT)}}/) | |
parts.to_enum.with_index(1) do |message_part, index| | |
deliver_message_via_carrier("#{message_part} - Part #{index} of #{parts.length}", to, from) | |
end | |
end | |
end | |
def deliver_message_via_carrier(text, to, from) | |
SMS_CARRIER.deliver_message(text, to, from) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment