James Chevalier James Chevalier - 6 months ago 4x
Ruby Question

Is a framework required for Ruby to accept POST requests?

Is there a way to use a flat .rb file to accept POST requests, or do I need to use a framework like Rails or Sinatra to accept the request?

I'm thinking along the lines of how I can use a flat .php file to accept POST requests, and use the

variable to access passed data.

Even more specifically, I'm trying to learn a bit of Ruby by porting over one of my Twilio apps from PHP. The app accepts SMS, processes the message sent, and sends a reply based on the body of the message received.

While using PHP, I can set the SMS Request URL in the Twilio site to my PHP file. The PHP file uses the
array to use the message that was received. (It seems like the Ruby equivalent to this is

Here's a quick example of the PHP version of what I'm talking about:

require "twilio.php"; // Twilio Library
$ApiVersion = "2010-04-01"; // Twilio API Version
$AccountSid = "SID"; // Twilio SID
$AuthToken = "TOKEN"; // Twilio Token

// Instantiate a new Twilio Rest Client
$client = new TwilioRestClient($AccountSid, $AuthToken);

// Get message body & who it's from, for the SMS that was just received
$SMSbody = mysql_real_escape_string($_REQUEST['Body']);

if ($SMScode == "codeword"){
$SMSresponse = "You know the code.";
$SMSresponse = "You do not know the code.";

// Twilio response to the sender
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
<Sms><?php echo $SMSresponse;?></Sms>

Here's my attempt at a Ruby equivalent, which is probably offensively bad:

require "rubygems"
require "twilio-ruby"

@account_sid = "SID"
@auth_token = "TOKEN"
smsbody = params['body']

@client = Twilio::REST::Client.new(@account_sid, @auth_token)
@account = @client.accounts.get(@account_sid)

if smsbody == "codeword"
smsreply = "You know the code"
smsreply = "You do not know the code"

response = Twilio::TwiML::Response.new do |r|
r.Sms smsreply

# print the result
puts response.text

This results in the Twilio debugging dashboard stating that the reply was more than 160 characters. This is because the reply is the full Ruby code, not the result of having ran the Ruby code. This makes me think that the POST request isn't being accepted correctly...


Based on the existing answers, and all the other research I've done, it doesn't seem like there's one single Answer to this question. It's another one of those TIMTOWTDI situations. Here's a summary of what I've learned so far, though…

The ability to drop PHP files into Apache & have it work properly is made possible by mod_php, which is enabled by default (making it seem seamless).

The params[] array is actually a Rails-specific helper. To get the same functionality, one would have to parse the request body (STDIN) on their own (perhaps with the CGI.parse function provided by the CGI module).

There seem to be a few options in my case:

The way forward for someone who wants to stick to strictly-Ruby (not using the PHP passthru function) without straying too far away from the familiarity of Apache might be to use Passenger with either Camping or Sinatra.