Giving EM-Smsified Some Server Love
I just pushed the next release of my EM-SMSified gem to Github and Rubygems. This release (0.3.0) adds an EventMachine HTTP server to make it easy to react to SMSified callbacks.
Installing is easy as always:
gem install em-smsified
Using it is equally easy. Here’s an example of a “pong” server that sends a pong back to any received text message:
require 'rubygems'
require 'yaml'
require 'em-smsified'
require 'eventmachine'
require 'evma_httpserver'
smsified = EventMachine::Smsified::OneAPI.new('username', 'password')
EM.run do
Signal.trap("INT") { EM.stop }
Signal.trap("TRAP") { EM.stop }
puts "Hit CTRL-C to stop"
puts "=================="
puts "Server started at " + Time.now.to_s
puts "Starting incoming SMSified callback server"
EM.start_server '0.0.0.0', 8080, EventMachine::Smsified::Server do |s|
s.on_incoming_message do |msg|
puts "Message received " + Time.now.to_s
puts "#{msg.sender_address} says '#{msg.message}' to #{msg.destination_address}"
smsified.send_sms( :message => 'Pong',
:address => msg.sender_address,
:sender_address => msg.destination_address) do |result|
puts "Pong sent " + Time.now.to_s
end
end
end
end
A more elaborate example is up on Github (examples/pong_server.rb).
The server supports incoming messages on which you need to set a subscription (SMSified – Receiving Messages) and delivery notifications. The latter is set up when you send an sms message by adding the :notify_url parameter.
Use cases
Having a server that’s easy to use from EventMachine makes it easy to implement more advanced text message scenarios:
- You could couple this with the em-websocket gem and add easy websocket callbacks from received text messages.
Source on Github as usual, and gem on Rubygems.
If you like my writing you should subscribe to my RSS feed.
Putting EventMachine In the SMSified Gem
I wanted to utilize EventMachine for something and real and since I’ve been tinkering with telephony stuff recently I thought something that sends text messages might be a good candidate. Instead of rewriting everything from scratch I started with the SMSified Ruby gem instead. SMSified is a service by Tropo that makes it really easy to send text messages. Since the service is still in beta sending text messages is free. Pretty neat. SMSified has a Ruby gem that comes with a test suite. Hence I thought it would be a good starting point.
Installing the new gem is easy
gem install em-smsified
Here’s some example code showing how to send an SMS via SMSified:
require 'rubygems'
require 'eventmachine'
require 'em-smsified'
oneapi = EventMachine::Smsified::OneAPI.new(:username => 'user', :password => 'password')
EM.run do
oneapi.send_sms(:address => '14155551212', :message => 'Hi there!', :sender_address => '13035551212') do |result|
puts result.inspect
end
end
The original gem uses HTTParty to do HTTP requests. To mock these in the spec suite the gem used FakeWeb. FakeWeb doesn’t work with EventMachine and therefore my first step was to replace FakeWeb with WebMock, which works with a number of Ruby HTTP frameworks. After that I DRY’ed up the code a bit to contain where HTTP requests were being made. Then I added EventMachine via the EM-HTTP-Request gem. To EM’ify the new library I had to modify the original interface to take an anonymous block. This block gets called when the request to SMSified returns. This is where you can check what was returned and perform any updates. This is shown in the code sample above.
There are more examples in the source tree and there’s also some YARD documentation.
Jason Goecke and John Dyer wrote the original SMSified gem making my job so much easier.
em-smsified on RubyGems.
Source on Github.
If you like my writing you should subscribe to my RSS feed.
I am a self employed independent software development consultant at