MUSh, a multi-service URL shortener in Ruby
I have to tell the truth, I'm really lazy, if there is a way to avoid to do something I will do so.
What about avoiding to open a browser or create a new tab and click a bookmark or type the address of a URL shortener service when I want to shorten a URL? If I have an opened terminal with IRb and Bash almost the whole day, why don't use it to shorten URLs?
Using the most pure principles of LDD (Laziness-Driven Development) I've created a gem called MUSh, a Multi-service URL Shortener, hence the name.
Yes, I know there are thousands of API wrappers in Ruby for bit.ly, etc. but most of them are fully-featured API wrappers and I didn't want all of those features, I just wanted to shorten URLs, that's it, well, I wanted command-line utilities as well, but I didn't find them in any existing gem.
I've been using MUSh for almost a month now, and it really helps me, that's why I decided to release it as a gem, if you want to install it just do it this way:
$ sudo gem install mush
It currently supports bit.ly, is.gd and u.nu services and it have one command-line utility per service:
$ isgd foo.raflabs.com $ bitly -l your_login -k your_apikey -u foo.raflabs.com $ owly -k your_apikey -u foo.raflabs.com
Hum, the last command doesn't help that much. I know. MUSh currently doesn't support storing your credentials so I use an alias.
In your ~/.bash_profile or ~/.bashrc:
alias bitly='bitly -l your_login -k your_apikey' alias owly='owly -k your_apikey'
after adding your alias issue the following command to refresh your terminal or close it an open another one:
$ source ~/.bash_profile (or whatever is your file called)
and now you'll be able to use the bitly and owly commands without specifying your credentials, even without specifying the -u option.
$ bitly foo.raflabs.com $ owly foo.raflabs.com
But what if I want to shorten an URL with a service MUSh doesn't support? If the service you want to use has an API that supports sending parameters via GET and returns only the shortened URL without any HTML code then you're in luck because you can use custom services in MUSh, let's see it:
From command-line:
$ shorten -s "http://service.url?params&url={{url}}" -u long_url.com
From a Ruby script
custom = Mush::Services::Custom.new
custom.set_service = "http://service.url?params&url={{url}}"
custom.shorten "long_url.com"
The {{url}} piece of code is a placeholder to let MUSh know where the service is expecting the long url, it's that easy.
If you're in a Mac and want to copy the shortened URL in one step, just use the pbcopy command:
$ bitly foo.raflabs.com | pbcopy
Then paste it with Cmd-V or with the pbpaste command.
MUSh was made in Ruby, so you can use it in your Ruby applications as well.
Bit.ly
bitly = Mush::Services::Bitly.new bitly.login = "your_login" bitly.apikey = "your_apikey" bitly.shorten "http://foo.raflabs.com" # => http://bit.ly/bJO5IH
Is.gd
isgd = Mush::Services::IsGd.new isgd.shorten "http://foo.raflabs.com" # => http://is.gd/eFZXc
ow.ly
owly = Mush::Services::Owly.new owly.apikey = 'your_api_key' owly.shorten "http://foo.raflabs.com"
Besides, If you want to shorten a URL in your Ruby or Rails console in a fast way add the following to your ~/.irbrc:
require 'mush' def by(url) bitly = Mush::Services::Bitly.new bitly.login = "your_login" bitly.apikey = "your_apikey" bitly.shorten url end def is(url) Mush::Services::IsGd.new.shorten url end def ow(url) owly = Mush::Services::Owly.new owly.apikey = 'your_api_key' owly.shorten url end
An use it like this
by "google.com" is "google.com" ow "google.com"
It's a nice set of shortcuts, I'd say.
Again, if you're in a Mac and want to copy the shortened URL in one step, just use my pasteboaRb gem:
$ sudo gem install pasteboaRb
Basically, pasteboaRb adds to_pb and paste methods to the Object class.
is("foo.raflabs.com").to_pb # => http://is.gd/eFZXc
Then paste it in any Mac application with Cmd-V, in the Terminal with the pbpaste command or in a Ruby terminal with the paste method.
paste # => http://is.gd/eFZXc
Thanks for reading.
Installing a gem fork from Github source
Usually, installing a gem is as easy as issuing:
gem install gem_name
but, what if you want to install a fork of a gem?
As you may know Github no longer hosts gems, so doing the following won't work:
$ sudo gem install technoweenie-grit --source http://gems.github.com
NOTE: Actually it'd work if the gem you're trying to install is in this list: http://gems.github.com/list.html
Disecting the "Download Source" button
Let's suppose we want to install the Techno Weenie's fork of Grit from Github:
Main repo => http://github.com/mojombo/grit
Techno Weenie's fork => http://github.com/technoweenie/grit
Github has a "Download Source" button, if you click it you'll be presented with two big images (say buttons): "ZIP" and "TAR", clicking these buttons will start downloading the code of the master branch in the selected format; below those buttons you'll find the list of tags of the repository, you'll be able to click one of the tags and again you'll be presented with "ZIP" and "TAR" to download the source of such tag.
How does this works?
This is how the URL is built:
http://github.com/USER/REPOSITORY/FORMAT/BRANCH_OR_TAG
"ZIP" => http://github.com/technoweenie/grit/zipball/master
"TAR" => http://github.com/technoweenie/grit/tarball/master
said this:
$ wget http://github.com/technoweenie/grit/zipball/master $ unzip technoweenie-grit-v2.0.0-12-g0bd0c5f.zip $ cd technoweenie-grit-0bd0c5f
Installing the gem
Gems source code has a special file called gem_name.gemspec, this is like the DNA of the gem and it's used to build it:
$ gem build grit.gemspec
Issuing this command will generate a gem_name-version.gem file which is the gem itself, let's install it:
$ sudo gem install grit-2.0.0.gem
Some projects doesn't have a .gemspec file but rake tasks, in that case you just need to do:
$ rake build $ rake install
If any of these methods work, you'd have to look at the Rakefile for tasks to generate the .gemspec file.
...and we're done!
NOTE: 'gem build' will not take in consideration special and external dependencies the gem might require, so you must take care of the dependencies manually. Check in the code all the 'require' lines to see dependencies.
Hope it helps.
Does anyone knows an easier method?