Sha256: 10ffe1c0e884271ea48596c4d2581139d636eb0b9006d0e332c0dab2cfd01855

Contents?: true

Size: 700 Bytes

Versions: 1

Compression:

Stored size: 700 Bytes

Contents

require "youtube_images/version"

module YouTubeImages

  class IncorrectYouTubeID < Exception; end

  def self.For(id_or_url, secure=false)
    id = id_or_url[/[\w\-\_]+$/]

    raise IncorrectYouTubeID if id.nil? || (id.length < 11)

    url_prefix = url_constructor(secure, id)

    {
      default: "#{url_prefix}/default.jpg",
      medium:  "#{url_prefix}/hqdefault.jpg",
      high:    "#{url_prefix}/mqdefault.jpg",
      maximum: "#{url_prefix}/maxresdefault.jpg"
    }
  end

  private

  def self.uri
    "img.youtube.com/vi"
  end

  def self.protocol(secure)
    secure ? "https" : "http"
  end

  def self.url_constructor(secure, id)
    "#{protocol(secure)}://#{uri}/#{id}"
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
youtube_images-1.0.1 lib/youtube_images.rb