<img src="http://www.66infra-strat.com/79881.png" style="display:none;">

My First Ruby Gem

This is my first shot at blogging. I started this ‘project’ in order to write about my experience in creating my first ever Ruby GEM! (DAM DAM DAMMM). This was a fun and educating experience, and I was especially amazed by the great feedback I got from people on the ruby mailing list and the ruby IRC channel on freenode.

How did it all start?

One sunny morning an email arrived from Safe-T’s server developer, the email stated:
Our new product Safe-T Box has a new REST based API, it is your duty (mine alone…) to create a usage example of this new API using a language of your choice.
As I already had some experience with ruby (automation and scripting for our servers) the choice was clear.

Planning (or the lack thereof..)

The first thing I did was thin about what I need for this to work:

Now what?

Create a basic Ruby method to contact the server and output some data:

# Lets require the needed gem
require 'rest-client'

# Now, lets build a basic call using the supplied API

def iVerifyUserAccount
RestClient::Request.execute(
method: :post,
url: 'https://isafe-t.com/ui_api/login.aspx',
headers: {
servletRequestID: 'MethodRequest',
BusinessLogic: "{Username:'', Password:'', RoleID: '00003', ExtensionID: 'mobile', iVerifyUserAccount: ['test', '1234', true]}"
}
)
end

Running the above example resulted in the right output. Looks like Ruby is super easy, right? So, what now ? Apparently robotically recreate each method for each call to the API. This is what the code looked like

As I’m not an expert or even a formally educated programmer, I turned to the guys at ruby-talk for inputs. I recieved a lot of feedback such as:

  • Respect Ruby Conventions!
  • Use 2 space for indentation (I used 4)
  • Name methods with snake_case (I used original method names such as “iVerifyUserAccount”)
  • Optimize Code!
  • Get rid of the repeating code (reuse string, methods, trim the code)
  • Use initializer to get instance data (my .new didn’t allow any arguments to be passed)
  • Document!
  • Test!
Create one method which sends the required changing string:

def send_request(request_string)
RestClient::Request.execute(
method: :post,
url: @url,
headers: {
servletRequestID: 'MethodRequest',
BusinessLogic: "{Username:'', Password:'', RoleID: '#{@role_id}', ExtensionID: '#{@extension_id}', #{request_string}}"
}
)
end

Apply Ruby conventions !

def verify_user_account
send_request("iVerifyUserAccount: ['#{@user_name}', '#{@password}', true]")
end

Let the user initialize with arguments

def initialize(config_hash={})
@extension_id = config_hash[:extension_id]
@user_name = config_hash[:user_name]
@password = config_hash[:password]
@url = config_hash[:url]
@role_id = config_hash[:role_id]
end

This is shaping up to be a nice looking code right here: Nicely Done Ruby Code :)
By Bar.Hofesh Blog 0 Comments

About the Author: Bar.Hofesh

Bar.Hofesh

 

Subscribe to Email Updates