Skip to content

Instantly share code, notes, and snippets.

@geewiz
Created August 24, 2020 10:13
Show Gist options
  • Save geewiz/cb7b7902a44fd470e97fd29bc8bea411 to your computer and use it in GitHub Desktop.
Save geewiz/cb7b7902a44fd470e97fd29bc8bea411 to your computer and use it in GitHub Desktop.
Julian's challenge, solution 1
#! env ruby
require "rspec/autorun"
require "byebug"
class SMScarrier
def self.deliver(text, to, from)
puts "From: #{from}"
puts "To: #{to}"
puts "Message: #{text}"
puts "Message length: #{text.length}"
puts "---"
end
end
class SMSsplitter
MAXLEN = 160
def initialize(text)
@text = text.chomp
end
def call
if @text.length <= 160
return [@text]
end
words = @text.split(" ")
break_up_text(words)
end
def break_up_text(words)
parts = []
chunk = ""
chunk_counter = 1
words.each do |word|
new_chunk = "#{chunk} #{word}"
if number_chunk(new_chunk, chunk_counter).length < MAXLEN - 3
chunk = new_chunk
else
parts << number_chunk(new_chunk, chunk_counter)
chunk_counter = chunk_counter + 1
chunk = word # FIXME: Huge words break things
end
end
parts << number_chunk(chunk, chunk_counter)
parts.map { |chunk| "#{chunk} #{chunk_counter}" }
end
def number_chunk(chunk, counter)
format("%s - message %d of", chunk, counter)
end
end
def send_sms_message(text, to, from)
deliver_message_via_carrier(text, to, from)
end
def deliver_message_via_carrier(text, to, from)
SMSsplitter.new(text).call.each do |message|
SMScarrier.deliver(message, to, from)
end
end
RSpec.describe SMSsplitter do
it "does not split a short message" do
msg = <<-EOMSG
This is just a short message.
EOMSG
chunks = SMSsplitter.new(msg).call
expect(chunks.length).to eq 1
expect(chunks[0]).to eq "This is just a short message."
end
it "does split a medium-length message" do
msg = <<-EOMSG
This is a message that will have to be split because it slightly exceeds the 160 character limit imposed by the SMS system. It will result in two messages with a counter.
EOMSG
chunks = SMSsplitter.new(msg).call
expect(chunks.length).to eq 2
expect(chunks[0].length).to be <= 160
expect(chunks[0]).to match(/message 1 of 2$/)
expect(chunks[1].length).to be <= 160
expect(chunks[1]).to match(/message 2 of 2$/)
end
it "does split a long message" do
msg = <<-EOMSG
"True happiness is to enjoy the present, without anxious dependence upon the future, not to amuse ourselves with either hopes or fears but to rest satisfied with what we have, which is sufficient, for he that is so wants nothing. The greatest blessings of mankind are within us and within our reach. A wise man is content with his lot, whatever it may be, without wishing for what he has not." -- Seneca
EOMSG
chunks = SMSsplitter.new(msg).call
expect(chunks.length).to eq 3
expect(chunks[0].length).to be <= 160
expect(chunks[0]).to match(/message 1 of 3$/)
expect(chunks[1].length).to be <= 160
expect(chunks[1]).to match(/message 2 of 3$/)
expect(chunks[2].length).to be <= 160
expect(chunks[2]).to match(/message 3 of 3$/)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment