Skip to content

Instantly share code, notes, and snippets.

@andreimoment
Last active February 21, 2018 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreimoment/7eeea30785c6bd85743975dce8ed4a0d to your computer and use it in GitHub Desktop.
Save andreimoment/7eeea30785c6bd85743975dce8ed4a0d to your computer and use it in GitHub Desktop.
Adds a vimeo_thumbnail helper to Middleman
# 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