Sha256: 57b36101e40b14bcff58b74729976472b4e25a0736f2c564f47abc93e2f9b566

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

require 'social_parser/provider/base'

module SocialParser
  module Provider
    class Youtube < Base
      URL_FORMATS = {
        embed: /\A((https?)?:\/\/)?(www\.)?youtube\.com\/embed\/(?<id>[\w\-\.]+)\/?/i,
        video: /\A((https?)?:\/\/)?(www\.)?youtube\.com\/watch\?v=(?<id>[\w\-\.]+)\/?/i,
        full: /\A((https?)?:\/\/)?(www\.)?youtube\.com\/(?<type>(user|channel|playlist))\/(?<id>[\w\-\.]+)\/?/i,
        shortend: /\A((https?)?:\/\/)?(www\.)?youtube\.com\/(?<id>[\w\-\.]+)\/?/i,
      }

      def provider
        :youtube
      end

      def type
        @type || 'user'
      end

      def url
        if video?
          "https://www.youtube.#{domain}/watch?v=#{username}"
        else
          "https://www.youtube.#{domain}/#{type}/#{username}"
        end
      end

      def embed_url
        return super unless video?
        "https://www.youtube.#{domain}/embed/#{username}"
      end

      private

      def video?
        type == 'video'
      end

      def parse_from_url
        URL_FORMATS.each do |key, format|
          m = format.match(url_from_attributes)
          next unless m

          if key == :full
            @type = m[:type]
            return nil if @type != 'user'
          elsif key == :video || key == :embed
            @type = 'video'
          end
          return m[:id]
        end
        nil
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
social_parser-1.1.0 lib/social_parser/provider/youtube.rb
social_parser-1.0.8 lib/social_parser/provider/youtube.rb
social_parser-1.0.7 lib/social_parser/provider/youtube.rb
social_parser-1.0.6 lib/social_parser/provider/youtube.rb
social_parser-1.0.5 lib/social_parser/provider/youtube.rb
social_parser-1.0.4 lib/social_parser/provider/youtube.rb
social_parser-1.0.3 lib/social_parser/provider/youtube.rb
social_parser-1.0.2 lib/social_parser/provider/youtube.rb