sjmog / ralyxa

A Ruby framework for interacting with Amazon Alexa.
MIT License
180 stars 24 forks source link

When using Ralyxa as part of a Rails application, calling `rewind` on a requests body causes an error. #9

Closed mattrayner closed 6 years ago

mattrayner commented 6 years ago

We should try to call rewind only if rewind exists to prevent this error 👎

This is because the Rails request object handles reading differently

sjmog commented 6 years ago

It's cause Sinatra is poorly-behaved, I think. I'm not sure why it doesn't rewind the request for you.

Before releasing any more (after this fix), let's make sure we've got an example Rails app in addition to the Sinatra one. Ralyxa's primarily designed for Sinatra, but if Rails is a big use case let's support it properly.

mattrayner commented 6 years ago

The issue is essentially caused by ActionDispatch::Request.

Within Sinatra, you receive a Rack::Request wrapped in a Sinatra::Request object. Rack::Request's body object is an IO-like object which once read must be rewound just like a StringIO.

Rails on the other hand does some Rails'y magic and ActionDispatch::Request doesn't directly wrap Rack::Request so calling request.body generates a fresh IO-like object, meaning multiple calls to request.body don't need to be rewound.

When building Alexa_validator I encountered a number of edge cases that were caused by rewinding rails requests - calling rewind in instances where there is no request body raises an error for example

sjmog commented 6 years ago

Makes sense, thanks!

Ujjwal1987 commented 6 years ago

Can you please provide an example to implement ralyxa on rails?

mattrayner commented 6 years ago

@Ujjwal1987 I'll dig out mine now and upload it

mattrayner commented 6 years ago

@Ujjwal1987 please remember this is a proof of concept, and I have only used it for testing: https://github.com/mattrayner/ralyxa-on-rails