Module: Bitly

Defined in:
lib/extensions/bitly.rb

Constant Summary collapse

TIME_TO_LIVE =
365 * 24 * 3600

Class Method Summary collapse

Class Method Details

.shorten(url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/extensions/bitly.rb', line 19

def self.shorten(url)
  return url if !self.url_base || !url || url.index("/bit.ly/")

  bitly_url = "#{self.url_base}&longUrl=#{CGI.escape(url)}"
  parsed = JSON.parse Http.get(bitly_url, TIME_TO_LIVE) 

  if parsed["status_code"] == 200
    bitlified = parsed["data"]["url"] 
    App.logger.debug "bitlify: #{url} => #{bitlified}"
    bitlified
  else
    App.logger.error "#{url}: bitly error: #{parsed["status_txt"] }"
    url
  end
end

.url_baseObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/extensions/bitly.rb', line 6

def self.url_base
  return @url_base unless @url_base.nil?

  user, key = (App.config[:bitly] || {}).values_at "user", "key"

  @url_base = unless user && key
    App.logger.warn "Missing or invalid bitly configuration: #{App.config[:bitly]}"
    false
  else
    "https://api-ssl.bitly.com/v3/shorten?login=#{user}&apiKey=#{key}"
  end
end