- Read Tutorial
- Watch Guide Video
In this section, we are going to start working on our SMS module that will send SMS messages.
There are many ways to do it. I like to put custom behaviors like this one inside of lib
directory because it sits outside of the core application. This way, we can treat it like a helper module or even a gem.
The first step is to open application.rb
file and add another config block to include the lib
directory. This is how you can add it.
# config/application.rb require File.expand_path('../boot', __FILE__) require 'rails/all' Bundler.require(*Rails.groups) module Overtime class Application < Rails::Application config.active_record.raise_in_transactional_callbacks = true config.autoload_paths << Rails.root.join("lib") end end
Next, create a file called sms_tool.rb
in lib
directory. Here, we are going to create a module called SmsTool
. It's important to follow the same naming convention if you're following me. Since the file name is sms_tool
, your module name should be SmsTool
. If you want to use a different name, make sure you change it everywhere.
Inside this module, let's create a method called send_sms
that takes in the number and message as its arguments, and prints out the values for us.
module SmsTool def self.send_sms(number:, message:) puts "Sending SMS..." puts "#{message} to #{number}" end end
To test this, start a rails console session and call this method with the command.
SmsTool.send_sms(number: 5555555, message: "my message")
This prints out our message, so it's working!
We'll continue building in the actual functional in subsequent lessons.