Getting TinyUrl’s in Rails

Rails, Tech No Comments

If you have used twitter I am sure you have noticed that if you post a long url in your update it gets turned in to a tinyurl for you. This is really cool functionality and I have created a helper in rails to do it for me.

First, let’s talk about how you get a tinyurl. Tinyurl has a really simple api that you can call that will return a tiny url. It is rediculous how simple it is. You just call this url with a GET: http://tinyurl.comapi-create.php?url=http://example.com

The returned page will be a single line with the tinyurl equivalent. It is that simple. Now on to the rails part.

In your application.rb helper you would add this:

require ‘net/http’
require ‘uri’

def tinyfy(newurl)
   url = URI.parse(’http://tinyurl.com/’)
   res = Net::HTTP.start(url.host, url.port) {|http|
   http.get(’/api-create.php?url=’ + newurl)
   }
   if res.body.empty?
      #tinyurl is not responding properly… Return the original url
      return newurl
   else
      return res.body
   end
end

Now if you call the tinyfy method in your app, let’s say in your view, with the url you want to tinyurlify as the parameter the return will be the tinyurl. I take this one step further and have the tinyfy method make the url a link by adding the ‘a’ tags in the method like so:

require ‘net/http’
require ‘uri’

def tinyfy(newurl)
   url = URI.parse(’http://tinyurl.com/’)
   res = Net::HTTP.start(url.host, url.port) {|http|
   http.get(’/api-create.php?url=’ + newurl)
   }
   if res.body.empty?
      #tinyurl is not responding properly… Return the original url
      return ‘<a href=’ + newurl + ‘>’ + newurl + ‘</a>’
   else
      return ‘<a href=’ + res.body + ‘>’ + res.body + ‘</a>’
   end
end

That will return a nice linked tinyurl in your view. That is all there is to it.

CTRL-Z and fg: my new(old) best friend

Tech No Comments

As an operations engineer, I get to touch a lot of systems and a lot of different apps on those systems on a daily basis. This means a lot of log following, debugging, fixing, and more log following. Something that I love but often forget to use is background jobs. Background jobs work like windows on your desktop but for the command line. You need to switch between your text editor (vi), the log tail, and the command prompt. You could open three connections to the machine or you could use the background/foreground functionality built in to linux/bsd. Here is what you do:

You need to follow /var/logs/app.log, you need to edit /etc/app/app.conf, and you need to execute /usr/bin/app -debug to fix your problem.

First you run tail -f /var/logs/app.log and then hit ‘ctrl+z’ to suspend this task and return to the prompt. You should see something like:
[1]+ Stopped tail -f /var/logs/app.log

The number at the beginning is the job number. That is how you get back to the process.

Now you run vi /etc/app/app.conf and hit ‘ctrl+z’ This will be assigned job number 2:
[2]+ Stopped vi /etc/app/app.conf

Now you can use the ‘fg’ command to ‘foreground’ a job. So if you want to pull the log back up you do ‘fg 1′ and the log job will come back. Use ‘ctrl+z’ to go back to the command line and run ‘fg 2′ to pull up your app.conf. It’s that easy. If you ever forget what jobs are which you can run ‘jobs’ to display all running jobs:

[1]+ Stopped tail -f /var/logs/app.log
[2]+ Stopped vi /etc/app/app.conf

It’s that freaking easy. Enjoy…