If you aren’t familiar with gems they are the Ruby on Rails equivalent to WordPress plugins. I wanted to learn how to write my own so I found this great tutorial at Quick Left.
I had an idea for a gem that I wanted to make quickly so I didn’t build the exact same gem as the tutorial. Instead I just wanted to make a simple gem so I could lookup info about a visitor on a website.
My gem is called browserinfo. I was able to submit it to Ruby Gems and my GitHub account no problem. Unfortunately, I couldn’t seem to get it to run in my rails app. I tried a dozen different solutions but I wasn’t exactly familiar with creating my own gem, so this was more of a brute force attack. Here are some of the useful things I learned:
1) Use “irb”!
- I have never used this awesome terminal command before, but it is just like running the Python interpreter. Originally I kept pushing my gem to GitHub and RubyGems to test it in my Rails app.
- With irb to test my gem I simply had to type:
require ‘browserinfo’ and then the commands I was trying to test. This was after running gem install GEMNAME.
2) Difference Between Class and Instance Methods
- Class methods look like “def self.myname”
- Instance Methods look like “def myname”
- If the class name was “Hello” you would call class methods by typing “Hello.myname”
- Instance methods are called by writing “Hello.new.myname”
3) How to make calls on my own gem
- I’m embarrassed to say it took me quite a while to figure out how to actually make my gem useful. Here’s the code and then we can talk about it.
My gem – /browserinfo.rb
# Your code goes here...
content = Hash.new
content[:browser] = 'Chrome'
content[:browser] = 'Firefox'
content[:browser] = 'Safari'
content[:browser] = 'Internet Explorer'
content[:browser] = 'Not recognized'
In the html.erb file I want to use the information for example /app/views/site/index.html.erb:
<%= Browserinfo::GetInfo.process(request.env["HTTP_USER_AGENT"]) %>
Now, this is just the very beginnings of my gem and not how I plan to actually use it, but it gets the point across. As you can see I have a module called Browserinfo (like the gem name).
Inside of that is a class called GetInfo which has a ‘class method’ inside of it called self.process and accepts the argument (info) in it.
It took me a while to figure out the correct syntax for this but to make a call to this you first called the module, followed by the class and then the class method with the arguments required inside. The arguments inside of process() is a Ruby Array called Request Env. This array contains information about visitors and server environments. The array is accessible on all pages hosted on a Rails site and has over a dozen different key/value pairs. The one that contains what browser they are using is the HTTP_USER_AGENT. Here is a list of all the variables in Request Env.
Now this gem isn’t necessarily useful. You could just type:
<%= request.env["HTTP_USER_AGENT"] %>
and the entire user agent string would be shown. However, I plan on building this gem out more so that it will parse the information in a better format that hopefully make this gem useful for someone.