Skip to content

Instantly share code, notes, and snippets.

@fantattitude
Created September 3, 2012 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fantattitude/3614500 to your computer and use it in GitHub Desktop.
Save fantattitude/3614500 to your computer and use it in GitHub Desktop.
Go from simple http link to twitter embed automagically using RedCarpet !
def autolink(link, link_type)
statusReg = /https?:\/\/twitter\.com\/[a-zA-Z]+\/status\/([0-9]+)/
if link_type == :url
if link =~ statusReg
tweet = statusReg.match(link)
urlTweet = tweet[0]
idTweet = tweet[1]
uri = URI.parse("https://api.twitter.com/1/statuses/oembed.json?id=#{idTweet}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
jsonTweet = ActiveSupport::JSON.decode(response.body)
jsonTweet["html"]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment