Ruby Programming Tutorial - Lesson 22 - Creating Dummy Vote Bot

in #ruby7 years ago

In this Article we are going to create a class for Voting bot

This will be a Dummy class which does not actually cast votes.. but it can be extended to implement the functionality
We are exploring the beauty of Object Oriented Style of Programming :)

The Task

Create a Voting bot, for steemit which can be used to cast votes on People's posts

Lets think of a Object Oriented Solution ..
identify the properties

  • Voter Name, who casts the vote
  • Votee Name, who we cast the vote
  • Vote power, what percentage vote should have
  • Posting key, required to authorize
  • Post URL, which is to be voted up

Now that we have identified the properties of a vote.. we can create our Class
Note that this program is complete.. where I also identified the methods this Class can have.

class VotingBot
    def initialize(*properties)
        @auth_key    = properties[0]
        @voter_name  = properties[1]
        @votee_name  = properties[2]
        @vote_power  = properties[3]
        @post_url    = properties[4]
    end

    def authorize
        puts "Checking Posting Key .. "
        puts "Success :p "
    end

    def cast_vote       
        puts "#{@voter_name} has voted on #{@votee_name}\'s Post "
        puts "with #{@vote_power}% Voting power on #{@post_url}. "
    end
end

Our Class has 3 Methods, first one you are already familiar with, its constructor Method.
second Dummy method, is used to authorize or login to steemit ..

Third method is Used to cast the vote.
Screenshot from 2018-03-14 19-29-38.png

Once we have our Class ready, we can then use it inside our main.rb file.
to create an object out of it.. and use that to Cast a vote.

require_relative 'votingbot'

$voterName = "bilal-haider"

puts "Please insert your Posting Key: "
$postingKey = gets.chomp

puts "Who do you want to vote? "
$votee_name = gets.chomp

puts "Insert his post url: "
$postURL    = gets.chomp


# Note that we are giving 100% vote as default .. 
## you can create a method to calculate voting power. 
_votingbot = VotingBot.new($postingKey, $voterName, $votee_name, 100, $postURL)
_votingbot.authorize()
_votingbot.cast_vote()

We are taking several values as Inputs, Authorizing the user, and then Casting the Vote :)
Note the highlighted part. is the output of our program..

Screenshot from 2018-03-14 19-37-20.png

Congratulations :)

You can write Beautiful Ruby Classes that will help you do amazing things, You can take this Dummy Class and extend it to create an actual voting bot

This was just a Programming practice, More Classes we Create more we learn about Object Oriented Style of Coding.

Sort:  

Very good post!

I am glad you liked it :)

nice infomation

Thank you :)

Good post..

You are doing a great job here man. Well done @bilal-haider
toch20180311_195638.png