25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/atig/bitly.rb', line 25
def shorten(url)
return url if url =~ /bit\.ly/
bitly = URI("http://api.bit.ly/v3/shorten")
if @login and @key
bitly.path = "/shorten"
bitly.query = {
:format => "json", :longUrl => url, :login => @login, :apiKey => @key,
}.to_query_str(";")
req = @http.req(:get, bitly, {})
res = @http.http(bitly, 5, 10).request(req)
res = JSON.parse(res.body)
if res['statusCode'] == "ERROR" then
@log.error res['errorMessage']
url
else
res["results"][url]['shortUrl']
end
else
url
end
rescue Errno::ETIMEDOUT, JSON::ParserError, IOError, Timeout::Error, Errno::ECONNRESET => e
@log.error e
url
end
|