Created
May 3, 2011 12:34
-
-
Save mfojtik/953252 to your computer and use it in GitHub Desktop.
Using bit.ly API to shorten URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bitly | |
attr_reader :client | |
def initialize(username, password) | |
@username, @password = username, password | |
@client = RestClient::Resource.new('http://api.bitly.com/v3') | |
end | |
def shorten(url) | |
params = default_params.merge(:longUrl => url) | |
JSON::parse(@client['shorten'].post(params))['data']['url'] | |
end | |
private | |
def default_params | |
{ | |
:format => 'json', | |
:login => @username, | |
:apiKey => @password | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment