module Youframe class Builder def initialize(url, options = {}) @url = url @width = options[:width] || 560 @height = options[:height] || 315 @start_time = options[:start_time] @frameborder = options[:frameborder] || 0 @allowfullscreen = options.fetch(:allowfullscreen, true) @accelerometer = options.fetch(:accelerometer, true) @autoplay = options.fetch(:autoplay, true) @encrypted_media = options.fetch(:encrypted_media, true) @gyroscope = options.fetch(:gyroscope, true) @picture_in_picture = options.fetch(:picture_in_picture, true) end def url id = "" if @url.match(/youtu.be/) id = @url.split(/youtu.be\//)[-1].split("?t")[0] elsif @url.match(/www.youtube.com\/watch?/) id = @url.split(/watch\?v=/)[-1].split("&t=")[0] end @url = "https://www.youtube.com/embed/#{id}" @start_time.nil? ? @url : @url + "?start=#{@start_time}" end def allow_str allow_str = '' allow_str += 'accelerometer; ' if @accelerometer allow_str += 'autoplay; ' if @autoplay allow_str += 'encrypted-media; ' if @encrypted_media allow_str += 'gyroscope; ' if @gyroscope allow_str += 'picture-in-picture' if @picture_in_picture allow_str end def allow_full_screen @allowfullscreen ? "allowfullscreen" : nil end def generate tag = "" tag end end end