Class: HTML::Pipeline::FlickrFilter

Inherits:
TextFilter
  • Object
show all
Defined in:
lib/html/pipeline/flickr/flickr_filter.rb

Overview

HTML Filter for converting flickr’s link into linkable image

Context options:

:flickr_maxwidth, flickr_minheight - representing maxwidth and maxheight in oembed format's params.
:flickr_link_attr - HTML attributes for the link that will be generated

Instance Method Summary collapse

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/html/pipeline/flickr/flickr_filter.rb', line 15

def call
  regex = %r{(=")*(https?:\/\/(www\.)?flickr\.com\/photos\/[^\s<]*)}
  uri = URI("https://www.flickr.com/services/oembed")

  link_attr = context[:flickr_link_attr] || ""

  @text.gsub(regex) do |match|
    if $1.nil?
      params = {
        url: match,
        format: :json,
        maxwidth: max_value(:width),
        maxheight: max_value(:height)
      }

      uri.query = URI.encode_www_form(params)

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

      %{<a href="#{match}" #{link_attr}><img src="#{response["url"]}" alt="#{response["title"]}" title="#{response["title"]}" /></a>}
    else
      match
    end
  end
end