Skip to content

Instantly share code, notes, and snippets.

@jugyo
Forked from ryumu/bitly.rb
Created April 7, 2011 02:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jugyo/906913 to your computer and use it in GitHub Desktop.
Save jugyo/906913 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 text =~ /^:(update|reply|retweet|message)/ && text.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