Skip to content

Instantly share code, notes, and snippets.

@no6v
Forked from jugyo/bitly.rb
Created February 20, 2012 21:56
Show Gist options
  • Save no6v/1871755 to your computer and use it in GitHub Desktop.
Save no6v/1871755 to your computer and use it in GitHub Desktop.
# earthquake.gem plugin
# shorten url using bit.ly if text is over 140
Earthquake.init do
config[:bitly] ||= {}
config[:bitly][:username] ||= 'earthquakegem'
config[:bitly][:api_key] ||= 'R_22e702353baf49751d053660e4c71a30'
config[:bitly][:domain] ||= 'j.mp'
input_filter do |text|
if /^:(update|reply|retweet|message)\s+/ === text and text.size - $&.size > 140
puts "shortening urls...".c(:info)
text.gsub(URI.regexp(['http','https'])) do |url|
query = "domain=#{config[:bitly][:domain]}&longUrl=#{URI.encode(url)}&login=#{config[:bitly][:username]}&apiKey=#{config[:bitly][:api_key]}"
result = JSON.parse(Net::HTTP.get("api.bit.ly", "/v3/shorten?#{query}"))
if result['status_code'] == 200
result['data']['url']
else
url
end
end
else
text
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment