Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flickr and Twitter filters using oEmbed and JSON #33

Merged
merged 5 commits into from
Jul 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/auto_html/filters/flickr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'uri'
require 'net/http'
require 'rexml/document'

AutoHtml.add_filter(:flickr).with(:maxwidth => nil, :maxheight => nil, :link_options => {}) do |text, options|
regex = %r{http://(www\.)?flickr\.com/photos/[^\s<]*}

text.gsub(regex) do |match|
params = { :url => match, :format => "json" }
[:maxwidth, :maxheight].each { |p| params[p] = options[p] unless options[p].nil? or not options[p] > 0 }

uri = URI("http://www.flickr.com/services/oembed")
uri.query = URI.encode_www_form(params)

response = JSON.parse(Net::HTTP.get(uri))

link_options = Array(options[:link_options]).reject { |k,v| v.nil? }.map { |k, v| %{#{k}="#{REXML::Text::normalize(v)}"} }.join(' ')
%{<a href="#{match}"#{ ' ' + link_options unless link_options.empty? }><img src="#{response["url"]}" alt="#{response["title"]}" title="#{response["title"]}" /></a>}
end
end
16 changes: 16 additions & 0 deletions lib/auto_html/filters/twitter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'uri'
require 'net/http'

AutoHtml.add_filter(:twitter).with({}) do |text, options|
regex = %r{https://twitter\.com(/#!)?/[A-Za-z0-9_]{1,15}/status(es)?/\d+}

text.gsub(regex) do |match|
params = { :url => match }.merge(options)

uri = URI("http://api.twitter.com/1/statuses/oembed.json")
uri.query = URI.encode_www_form(params)

response = JSON.parse(Net::HTTP.get(uri))
response["html"]
end
end