开发者

Is a framework required for Ruby to accept POST requests?

开发者 https://www.devze.com 2023-04-01 19:46 出处:网络
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?

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 $_REQUEST[] 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 t开发者_JAVA技巧o my PHP file. The PHP file uses the $_REQUEST[] array to use the message that was received. (It seems like the Ruby equivalent to this is params[].)

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

<?php
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.";
}
else{
        $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";
?>
<Response>
    <Sms><?php echo $SMSresponse;?></Sms>
</Response>

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"
else
    smsreply = "You do not know the code"

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

# 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...


You don't need to use a framework, and based on your description Rails would definitely be overkill for you. However, using a lightweight framework can make some aspects a bit nicer. I'd recommend looking at Camping if you haven't already - it is intended for single file apps.


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:

  • Use Passenger to let Apache run a Rack-based Ruby app (like Camping, Sinatra, or Rack)
  • Use a pure Ruby web server like Unicorn or Thin
  • Call the Ruby script via PHP's passthru function

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.


How do you execute that code? As far as I know the params hash is Rails specific, you can't use it in a simple script like that.

Coming to your question you can't simply drop a ruby file inside your server and expect that it will be executed. It will be simply returned as a text file to the browser.

To execute ruby code in a webserver you need at least rack and a server capable of executing rack applications or an apache module which process ruby. This is not a simple setup like php.

You have two choices here:

  1. Play with ruby console and/or ruby command line, i.e. from the shell run ruby your-script-name to execute it or type irb to start a ruby console. It's one of most powerful ruby feature, one of the thing that make me love ruby.
  2. If you really want to execute your script in a webserver context and you have php installed, you can exec your ruby script calling it from php.

This is an example on how to do that supposing your file is named program.rb, add the shebang line as first line and use the ARGV array instead of the params hash:

#!/usr/bin/env ruby
^^^^ Add this line as first line of your script ^^^^
require "rubygems"
require "twilio-ruby"
...
smsbody = ARGV[0] # ARGV[0] is the first command line parameter

Make it executable with chmod +x program.rb, and then call it from your php script with

passthru("/path/to/your/ruby/program.rb ". escapeshellarg($_REQUEST['Body']));

in this way the output from passthru (the output of your ruby program) will be sent to the browser.

0

精彩评论

暂无评论...
验证码 换一张
取 消