module Bitly module API VERSION = '2.0.1' def self.get(action,query={}) response = RestClient.get path(action,query) JSON.parse(response)['results'] end def self.path(path,query) query_string = query.inject('') { |string,array| string.concat("&#{array[0]}=#{array[1]}") } "http://api.bit.ly/#{path}?version=#{VERSION}#{query_string}&login=#{LOGIN}&apiKey=#{KEY}" end #-------------------------------------------------------------------------- # Shorten a URL #-------------------------------------------------------------------------- # returns Bitly::URL for given URL # currently only works for one url at a time def self.shorten(url) get('shorten','longUrl' => url)[url] end #-------------------------------------------------------------------------- # Expand a Bitly::URL #-------------------------------------------------------------------------- def self.expand(url) bitly_hash = url.split('/').last get('expand','shortUrl' => url)[bitly_hash]['longUrl'] end end end