Created
April 6, 2011 10:16
-
-
Save ryumu/905426 to your computer and use it in GitHub Desktop.
shoten url earthquake.gem plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output filter would be a nice addition