Skip to content

Instantly share code, notes, and snippets.

@a-barbieri
Last active July 6, 2023 08:21
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save a-barbieri/a93281cd5c01025664e5cbd781cd917c to your computer and use it in GitHub Desktop.
Save a-barbieri/a93281cd5c01025664e5cbd781cd917c to your computer and use it in GitHub Desktop.
Rails helper to embed YouTube and Vimeo iframe in view
# Helpers for better embedding and manipulation of videos
# Place this code in app/helpers/videos_helper.rb
# Then from any view you can add:
#
# <%= get_video_iframe('http://the.video.url') %>
#
# Optionally you can add width and height.
#
# <%= get_video_iframe('http://the.video.url', '1600px', '900px') %>
#
# Thanks to https://stackoverflow.com/a/27878890/1498118
module VideosHelper
require 'net/http'
# Regex to find YouTube's and Vimeo's video ID
YOUTUBE_REGEX = %r(^(http[s]*:\/\/)?(www.)?(youtube.com|youtu.be)\/(watch\?v=){0,1}([a-zA-Z0-9_-]{11}))
VIMEO_REGEX = %r(^https?:\/\/(?:.*?)\.?(vimeo)\.com\/(\d+).*$)
# Finds YouTube's video ID from given URL or [nil] if URL is invalid
# The video ID matches the RegEx \[a-zA-Z0-9_-]{11}\
def find_youtube_id url
url = sanitize url
matches = YOUTUBE_REGEX.match url.to_str
if matches
matches[6] || matches[5]
end
end
# Get YouTube video iframe from given URL
def get_youtube_iframe url, width, height
youtube_id = find_youtube_id url
result = %(<iframe title="YouTube video player" width="#{width}"
height="#{height}" src="//www.youtube.com/embed/#{ youtube_id }"
frameborder="0" allowfullscreen></iframe>)
result.html_safe
end
# Finds Vimeo's video ID from given URL or [nil] if URL is invalid
def find_vimeo_id url
url = sanitize url
matches = VIMEO_REGEX.match url.to_str
matches[2] if matches
end
# Get Vimeo video iframe from given URL
def get_vimeo_iframe url, width, height
vimeo_id = find_vimeo_id url
uri = "https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/#{ vimeo_id }&width=#{ width }&height=#{ height }"
# see -> https://stackoverflow.com/a/4581095/1498118
response = Net::HTTP.get( URI.parse( uri ))
json = JSON.parse response
json['html'].html_safe
end
# Main function
# Return a video iframe
# If the url provided is not a valid YouTube or Vimeo url it returns [nil]
def get_video_iframe(url, width = "560px", height = "315px")
if find_vimeo_id(url)
get_vimeo_iframe(url, width, height)
elsif find_youtube_id(url)
get_youtube_iframe(url, width, height)
end
end
end
@a-barbieri
Copy link
Author

It'd be great to add some validation like in this piece of code

@mulvster
Copy link

This is great! Thank you for sharing

@Bottlecaps4
Copy link

Dude, been looking everywhere online for something like this. Great job. Works like a charm!

@vitkoz
Copy link

vitkoz commented May 24, 2018

👍

@devanmoylan
Copy link

Nice work! Thanks for putting this together!

@MunechikaHayashi
Copy link

Thank you!👍

@nirsh5
Copy link

nirsh5 commented Oct 23, 2020

Thank you!

@luthfirrach
Copy link

Sankyu Mastah .... :3

@dareosK
Copy link

dareosK commented Sep 29, 2022

OMG, you're the beeest <3 Thank you Thank you <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment