Class: Atig::Unu

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

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Unu

Returns a new instance of Unu.



4
5
6
7
# File 'lib/atig/unu.rb', line 4

def initialize(logger)
  @log = logger
  @http = Atig::Http.new logger
end

Instance Method Details

#shorten(url) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/atig/unu.rb', line 9

def shorten(url)
  unu_url = "http://u.nu/"
  unu     = URI("#{unu_url}unu-api-simple")
  url = URI.rstrip url
  unu.query = { :url => url }.to_query_str
  res = @http.http(unu, 5, 5).request(@http.req(:get, unu)).body

  if res[0, 12] == unu_url
    res
  else
    @log.error res
    url
  end
rescue Errno::ETIMEDOUT, JSON::ParserError, IOError, Timeout::Error, Errno::ECONNRESET => e
  @log.error e
  url
end