Posted By

insanedreamer on 01/29/11


Tagged

ruby osx Services


Versions (?)

OSX service to shorten URLs


 / Published in: Ruby
 

Place script into Automator (select "Run Shell Script" choose /usr/bin/ruby as the shell).

You'll need to create a bit.ly account which will give you your API key for authentication.

  1. #!/usr/bin/env ruby
  2. require 'uri'
  3. require 'net/http'
  4.  
  5. uri = STDIN.gets(nil).strip.delete("\n\r")
  6. unless uri.empty?
  7. begin
  8. escaped_uri = URI.escape("http://api.bit.ly/v3/shorten?login=yourlogin&apiKey=yourapikey&longUrl=#{uri}&format=txt")
  9. STDOUT << Net::HTTP.get_response(URI.parse(escaped_uri)).body
  10. rescue URI::InvalidURIError
  11. # Do nothing.
  12. end
  13. end

Report this snippet  

You need to login to post a comment.