class Jekyll::GiphyEmbedding

convert Giphy url to embedding html

Public Instance Methods

convert(line) click to toggle source
# File lib/jekyll/giphy/embed.rb, line 18
def convert(line)
  r = /^https?:\/\/giphy\.com\/gifs\/([A-Za-z0-9\-\_]+)\/?$/
  r =~ line ? get_html($~[1]) : line
end
embed(content) click to toggle source
# File lib/jekyll/giphy/embed.rb, line 23
def embed(content)
  content.lines.collect {|line| convert(line) }.join
end
get_html(id) click to toggle source
# File lib/jekyll/giphy/embed.rb, line 10
def get_html(id)
    url          = "https://giphy.com/services/oembed/?url=https://giphy.com/gifs/#{id}"
    uri          = URI(url)
    response     = Net::HTTP.get(uri)
    theHash      = JSON.parse(response)
    return "<img src='#{theHash['url']}' alt='#{theHash['title']}' width='#{theHash['width']}' height='#{theHash['height']}'>"
end