Sha256: 3466b55b123db0fbf70143588cb003139fb4199bcc09ea3da60528236918b75c

Contents?: true

Size: 886 Bytes

Versions: 4

Compression:

Stored size: 886 Bytes

Contents

AutoHtml.add_filter(:flickr).with(:maxwidth => nil, :maxheight => nil, :link_options => {}) do |text, options|
  require 'uri'
  require 'net/http'
  require 'rexml/document'

  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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
auto_html-whistlerbrk-2.0.0.pre lib/auto_html/filters/flickr.rb
auto_html-1.6.4 lib/auto_html/filters/flickr.rb
auto_html-1.6.3 lib/auto_html/filters/flickr.rb
auto_html-1.6.2 lib/auto_html/filters/flickr.rb