Sha256: 5aba60e305576f4bad568419d37aedbacf8a999adaa94507c52b27e2bc30d849

Contents?: true

Size: 899 Bytes

Versions: 7

Compression:

Stored size: 899 Bytes

Contents

require "cgi"

module Bitly
  TIME_TO_LIVE = 365 * 24 * 3600
  
  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
  
  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
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
radiospieler-0.3.1 lib/extensions/bitly.rb
radiospieler-0.3.0 lib/extensions/bitly.rb
radiospiel-app-0.2.10 lib/extensions/bitly.rb
radiospiel-app-0.2.9 lib/extensions/bitly.rb
radiospiel-app-0.2.7 lib/extensions/bitly.rb
radiospiel-app-0.2.6 lib/extensions/bitly.rb
radiospiel-app-0.2.5 lib/extensions/bitly.rb