module Bitly

Constants

TIME_TO_LIVE

Public Class Methods

shorten(url) click to toggle source
# 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_base() click to toggle source
# 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