Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ruby 3 compatibility #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

magic4dev
Copy link

What?

I've added support for ruby 3. It includes RSpec tests for all modified files.

Why?

I've used this gem for a variety of rails projects with Ruby version <= 2.7 and it works like a charm. The problem occurs with a Ruby version >= 3.
In this case, when Ralyxa::Skill.handle(request) is invoked in a controller action, an ArgumentError: wrong number of arguments (given 1, expected 0 ) is raised. This is typical behavior after Ruby 3's decision to separate positional and keyword arguments.
This decision can be found in this helpful ruby-lang article:
https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/

How?

For complete Ruby 3 support, I created a module called Ralyxa::RubyVersionManager that is automatically included in the Ralyxa::ResponseBuilder and Ralyxa::Handler classes (these two classes are affected by the big changes in Ruby 3). After being included, this module makes available a helper method called "manage_ruby_version_for" which takes 3 arguments:

  1. the class where full support between Ruby 2 and Ruby 3 is required
  2. the method to execute for this class
  3. the data (in this case the hash to be returned to the Amazon server for a voice response).

This method is smart enough to detect which version of Ruby is installed on the system and dynamically handle the incompatibility between Ruby 2 and 3 without any ArgumentError.

All the Ralyxa::RubyVersionManager technical details can be founded in /lib/ralyxa/ruby_version_manager.rb.

An example of the Ralyxa::RubyVersionManager's manage_ruby_version_for method can be found in lib/ralyxa/handler.rb at line 23.
Instead of response_builder.build(options) run manage_ruby_version_for(response_builder, method: :build, data: options).

Testing?

I've write all RSpec tests for the Ralyxa::RubyVersionManager and updade all previous Ralyxa tests for the complete compatibility between ruby 2 and 3. Feel free to modifiy each test file if necessary.

Anything Else?

I also added a #locale method to the Ralyxa::ResponseEntities::Request class and documented it in the README.md file.
As described in the README, this method returns the local value from the Amazon request.
The idea of ​​introducing this method can be found in this discussion on StackOverflow: https://stackoverflow.com/questions/59913122/extending-a-ralyxa-to-manage-different-locales-and-directives

As in the previous testing section, feel free to suggest any changes.

All the best!
magic4dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant