Class: Atig::Bitly

Inherits:
Object
  • Object
show all
Defined in:
lib/atig/bitly.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, login, key) ⇒ Bitly

Returns a new instance of Bitly.



17
18
19
20
21
22
# File 'lib/atig/bitly.rb', line 17

def initialize(logger, , key)
  @log   = logger
  @login = 
  @key   = key
  @http  = Http.new logger
end

Class Method Details

.login(logger, login, key) ⇒ Object



12
13
14
# File 'lib/atig/bitly.rb', line 12

def (logger, , key)
  self.new logger, , key
end

.no_login(logger) ⇒ Object



8
9
10
# File 'lib/atig/bitly.rb', line 8

def (logger)
  self.new logger, nil, nil
end

Instance Method Details

#shorten(url) ⇒ Object



24
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
# File 'lib/atig/bitly.rb', line 24

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