Sha256: 8079e5797b8074b7104d56ea9b9e6691f6008fc6b5ef864ad5370678c604bcc5

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

module Workarea
  module Storefront
    class VideoEmbedder
      class Youtube
        def initialize(embed_code, options = {})
          @embed_code = embed_code
          @options = options
        end

        def attrs
          {
            id: "youtube-#{youtube_id}",
            src: "#{url}?#{url_params}",
            data: {
              youtube_api: {
                mute: @options[:mute]
              }.to_json
            }
          }
        end

        def url_params
          parsed_query = Rack::Utils.parse_query(uri.query)
          parsed_query['enablejsapi'] = 1
          parsed_query['autoplay'] = autoplay
          parsed_query['loop'] = loop_playback
          parsed_query['videoId'] = youtube_id

          URI.encode_www_form(parsed_query)
        end

        private

          def youtube_id
            uri.path.split('/').last
          end

          def autoplay
            @options[:autoplay] ? 1 : 0
          end

          def loop_playback
            @options[:loop_playback] ? 1 : 0
          end

          def url
            @url ||= @embed_code.attribute('src').value
          end

          def uri
            URI.parse(url)
          end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
workarea-super_hero-2.3.0 app/services/workarea/storefront/video_embedder/youtube.rb
workarea-super_hero-2.2.2 app/services/workarea/storefront/video_embedder/youtube.rb
workarea-super_hero-2.2.1 app/services/workarea/storefront/video_embedder/youtube.rb