Sha256: 585dff983abb1e5feea2d6bd47b2d35d343b5de640da10e9e5e169d4b1bff800

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

class VideoInfo
  module Providers
    class Youtube < Provider

      def self.usable?(url)
        url =~ /(youtube\.com\/(?!playlist|embed\/videoseries).*)|(youtu\.be)/
      end

      def provider
        'YouTube'
      end

      def title
        _video_entry['title']['$t']
      end

      %w[description keywords].each do |method|
        define_method(method) { _video_media_group["media$#{method}"]['$t'] }
      end

      %w[width height].each do |method|
        define_method(method) { nil }
      end

      def duration
        _video_media_group['yt$duration']['seconds'].to_i
      end

      def embed_url
        "www.youtube.com/embed/#{video_id}"
      end

      def date
        Time.parse(_video_entry['published']['$t'], Time.now.utc)
      end

      def thumbnail_small
        _video_thumbnail(0)
      end

      def thumbnail_medium
        _video_thumbnail(1)
      end

      def thumbnail_large
        _video_thumbnail(2)
      end

      def view_count
        _video_entry['yt$statistics'] ? _video_entry['yt$statistics']['viewCount'].to_i : 0
      end

      private

      def _url_regex
        /(?:youtube(?:-nocookie)?\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i
      end

      def _api_base
        "gdata.youtube.com"
      end

      def _api_path
        "/feeds/api/videos/#{video_id}?v=2&alt=json"
      end

      def _api_url
        "http://#{_api_base}#{_api_path}"
      end

      def _default_iframe_attributes
        { allowfullscreen: "allowfullscreen" }
      end

      def _default_url_attributes
        {}
      end

      def _video_entry
        data['entry']
      end

      def _video_media_group
        data['entry']['media$group']
      end

      def _video_thumbnail(id)
        _video_entry['media$group']['media$thumbnail'][id]['url']
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
video_info-2.3.0 lib/video_info/providers/youtube.rb
video_info-2.2.2 lib/video_info/providers/youtube.rb
video_info-2.2.1 lib/video_info/providers/youtube.rb