Skip to content

Instantly share code, notes, and snippets.

@ryumu
Created April 6, 2011 10:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ryumu/905426 to your computer and use it in GitHub Desktop.
Save ryumu/905426 to your computer and use it in GitHub Desktop.
shoten url earthquake.gem plugin
# earthquake.gem plugin
# shorten url using bit.ly
Earthquake.init do
_ = config[:bitly] ||= {}
_[:username] ||= ''
_[:api_key] ||= ''
_[:domain] ||= 'j.mp'
_[:only_long_tweet] ||= false
input_filter do |text|
if text =~ /^:(update|reply)/ && (!_[:only_long_tweet] || text.size > 140)
text.gsub(URI.regexp(['http','https'])) do |url|
query = "domain=#{_[:domain]}&longUrl=#{URI.encode(url)}&login=#{_[:username]}&apiKey=#{_[: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
@indeyets
Copy link

indeyets commented Mar 5, 2012

output filter would be a nice addition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment