Sha256: 0fe48e4142a35da24d9950e773c71f78484d22bde17a0fcac12a5b089b9d828e

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

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 = "<iframe width='#{@width}' height='#{@height}' src='#{url}' frameborder='#{@frameborder}' allow='#{allow_str}'#{allow_full_screen}></iframe>"
			tag
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
youframe-1.0.0 lib/youframe/builder.rb