Class: Atig::Bitly
- Inherits:
-
Object
- Object
- Atig::Bitly
- Defined in:
- lib/atig/bitly.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(logger, login, key) ⇒ Bitly
constructor
A new instance of Bitly.
- #shorten(url) ⇒ Object
Constructor Details
#initialize(logger, login, key) ⇒ Bitly
Returns a new instance of Bitly.
18 19 20 21 22 23 |
# File 'lib/atig/bitly.rb', line 18 def initialize(logger, login, key) @log = logger @login = login @key = key @http = Http.new logger end |
Class Method Details
.login(logger, login, key) ⇒ Object
13 14 15 |
# File 'lib/atig/bitly.rb', line 13 def login(logger, login, key) self.new logger, login, key end |
.no_login(logger) ⇒ Object
9 10 11 |
# File 'lib/atig/bitly.rb', line 9 def no_login(logger) self.new logger, nil, nil end |
Instance Method Details
#shorten(url) ⇒ Object
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 |