OSX service to shorten URLs


/ Published in: Ruby
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.