Last active
February 21, 2018 21:04
-
-
Save andreimoment/7eeea30785c6bd85743975dce8ed4a0d to your computer and use it in GitHub Desktop.
Adds a vimeo_thumbnail helper to Middleman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adds a vimeo_thumbnail(vimeo_id, size) helper to Middleman | |
# | |
# by Andrei Andreev / github.com/andreimoment, 2018 | |
# | |
# to install: | |
# - save the content of this gist to lib/custom_helpers.rb | |
# (you may need to create the lib folder and the custom_helpers.rb file) | |
# | |
# - add these two lines to config.rb: | |
# require 'lib/custom_helpers' | |
# helpers CustomHelpers | |
# | |
# - to use: | |
# img src="vimeo_thumbnail(241359441)" | |
# or | |
# img src="vimeo_thumbnail(241359441, :small)" | |
# - size can be :large (default), :medium, :small | |
# - the vimeo id may be a number or a string | |
# <img src="<%=vimeo_thumbnail('241359441')%>" /> | |
# | |
# Enjoy! | |
require 'open-uri' | |
require 'json' | |
module CustomHelpers | |
def vimeo_thumbnail_url(vimeo_id, size=:large) | |
raise ArgumentError.new("Vimeo Thumbnail Error: Vimeo ids may only contain digits! You provided: #{vimeo_id}") unless /\A(\d+)\z/.match vimeo_id.to_s | |
desired_size = size if [:small, :medium, :large].include? size | |
# this will raise an error if the id is missing | |
json = open("https://vimeo.com/api/v2/video/#{vimeo_id.to_s}.json").read | |
video_data = JSON.parse json | |
thumbnail_url = video_data.first["thumbnail_#{desired_size.to_s}"] | |
end | |
alias_method :vimeo_thumbnail, :vimeo_thumbnail_url | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment