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
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.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6823)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6553)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6419)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3994)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3785)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3729)
The Ultimate iOS Interview Playbook by Avi Tsadok(3704)
