Class: Spree::ExternalVideo

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model
Defined in:
app/models/spree/external_video.rb

Overview

Value object that reads a YouTube or Vimeo link and tells you which provider it belongs to, which video it points at, and how to embed it.

Spree does this once, at the write boundary, so the URL is validated before it is stored and every storefront reads the same derived values instead of re-implementing link parsing.

Constant Summary collapse

YOUTUBE_HOSTS =
%w[youtube.com www.youtube.com m.youtube.com youtu.be www.youtu.be].freeze
VIMEO_HOSTS =
%w[vimeo.com www.vimeo.com player.vimeo.com].freeze
YOUTUBE_ID_FORMAT =
/\A[\w-]{11}\z/
VIMEO_ID_FORMAT =
/\A\d+\z/
YOUTUBE_ID_PATHS =

Path shapes that actually carry a video id. Anything else on the domain — /c/name, /@handle, /playlist — is not a video, and treating its last segment as an id yields an embed URL that plays nothing.

%w[embed shorts live v].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(url) ⇒ Spree::ExternalVideo?

Reads a URL and returns the video it describes, or nil when the URL is blank, malformed, or points somewhere Spree doesn't support.

Parameters:

  • url (String, nil)

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/spree/external_video.rb', line 26

def self.parse(url)
  return nil if url.blank?

  uri = begin
    URI.parse(url.strip)
  rescue URI::InvalidURIError
    return nil
  end

  return nil unless uri.scheme.in?(%w[http https])

  host = uri.host&.downcase
  return nil if host.blank?

  if host.in?(YOUTUBE_HOSTS)
    youtube_from(uri, host)
  elsif host.in?(VIMEO_HOSTS)
    vimeo_from(uri)
  end
end

.supported?(url) ⇒ Boolean

Returns whether the URL is one Spree can embed.

Returns:

  • (Boolean) —

    whether the URL is one Spree can embed



48
49
50
# File 'app/models/spree/external_video.rb', line 48

def self.supported?(url)
  parse(url).present?
end

Instance Method Details

#embed_url ⇒ String

Returns URL for an iframe player.

Returns:

  • (String) —

    URL for an iframe player



89
90
91
92
93
94
# File 'app/models/spree/external_video.rb', line 89

def embed_url
  case provider
  when 'youtube' then "https://www.youtube.com/embed/#{video_id}"
  when 'vimeo' then "https://player.vimeo.com/video/#{video_id}"
  end
end

#thumbnail_url ⇒ String?

Provider-hosted still frame. YouTube serves one at a predictable URL; Vimeo requires an API call, so it has none here and the merchant uploads a poster instead.

hqdefault is the only size YouTube guarantees for every video — maxresdefault 404s on anything without an HD source, which would leave a broken image as the video's only still.

Returns:

  • (String, nil)


113
114
115
# File 'app/models/spree/external_video.rb', line 113

def thumbnail_url
  return "https://img.youtube.com/vi/#{video_id}/hqdefault.jpg" if provider == 'youtube'
end

#watch_url ⇒ String

Returns canonical watch URL on the provider's own site.

Returns:

  • (String) —

    canonical watch URL on the provider's own site



97
98
99
100
101
102
# File 'app/models/spree/external_video.rb', line 97

def watch_url
  case provider
  when 'youtube' then "https://www.youtube.com/watch?v=#{video_id}"
  when 'vimeo' then "https://vimeo.com/#{video_id}"
  end
end