Class: ProductTours::VideoMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/product_tours/video_metadata.rb

Constant Summary collapse

ENDPOINTS =
{
  'youtube' => 'https://www.youtube.com/oembed',
  'vimeo' => 'https://vimeo.com/api/oembed.json',
  'loom' => 'https://www.loom.com/v1/oembed'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ VideoMetadata

Returns a new instance of VideoMetadata.



19
20
21
# File 'lib/product_tours/video_metadata.rb', line 19

def initialize(url)
  @url = url.to_s
end

Class Method Details

.fetch(url) ⇒ Object



15
16
17
# File 'lib/product_tours/video_metadata.rb', line 15

def self.fetch(url)
  new(url).fetch
end

Instance Method Details

#fetchObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/product_tours/video_metadata.rb', line 23

def fetch
  resolved = VideoResolver.resolve(@url)
  return {} unless resolved

   = resolved.
  endpoint = ENDPOINTS[resolved.provider]
  return  unless endpoint

  response = request(endpoint)
  return  unless response.is_a?(Net::HTTPSuccess)

  body = JSON.parse(response.body)
  .merge(
    'provider_title' => body['title'].presence,
    'thumbnail_url' => safe_https_url(body['thumbnail_url'])
  ).compact
rescue JSON::ParserError, IOError, SocketError, SystemCallError, Timeout::Error, OpenSSL::SSL::SSLError
   || {}
end