Comprehensive Ruby Programming by Jordan Hudgens

Comprehensive Ruby Programming by Jordan Hudgens

Author:Jordan Hudgens [Hudgens, Jordan]
Language: eng
Format: azw3
Tags: COM051410 - COMPUTERS / Programming Languages / Ruby, COM051210 - COMPUTERS / Programming / Object Oriented, COM051010 - COMPUTERS / Programming Languages / General
Publisher: Packt Publishing
Published: 2017-06-30T04:00:00+00:00


sms.send_sms

mail.send_mail

phone.place_call

xyz.does_something_else

That's how inheritance works in Ruby. The key to remember about inheritance is that it gives you the ability to clean up your codebase and create inherited classes that share common behavior.

Overview of private versus public methods

This is going to be a fun section, as we are going to talk about sending an SMS message. Also, you will learn about how Ruby works with private and public methods.

Now, I'll go back to the ApiConnector class and create a class called SmsConnector that inherits from the ApiConnector class. In this class, I will create a method called send_sms. Inside this method, I'm going to place a code that will run a script that contacts an API that I created; it will look like this:

class SmsConnector < ApiConnector

def send_sms

`curl -X POST -d "notification[title]=#{@title}" -d

"notification[url]=http://edutechional-resty.herokuapp.com/posts/1"

"#{@url}"`

end

end

This method will send a title and link to an API, which will, in turn, send an SMS message. You don't have to worry about the code that will be handling the actual SMS sending, which is part of the beauty of the implementation.

Hopefully, you'll notice that the URL in the method and the URL value we are passing are different. Essentially, the application we're sending the API request to will send the title and the URL of the post to my phone. This is cool because we don't have any logic inside our class to send the SMS message. Rather, we are simply processing the data and then connecting to a third-party application that manages SMS communication. This type of application workflow is called service-based architecture.

Now we can instantiate the SmsConnector class and call the send_sms message:

sms = SmsConnector.new(title: "Hey there!", url: "http://edutechional-smsy.herokuapp.com/notifications")

sms.send_sms

Running this code will contact the SMS API and send the message.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.