Class: YouTube::ThumbnailService

Inherits:
Object
  • Object
show all
Defined in:
app/services/youtube/thumbnail_service.rb

Overview

Pushes the video poster/thumbnail to YouTube.

Resolution priority:

  1. youtube_thumbnail_image (Image library — CRM “YouTube thumbnail” override)
  2. poster_image (Image record with ImageKit hosting)
  3. Dragonfly poster attachment
  4. Cloudflare Stream thumbnail

YouTube requirements: JPG/PNG/GIF/BMP, max 2MB, 1280x720 recommended.

Constant Summary collapse

MAX_FILE_SIZE =
2.megabytes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account: nil, client: nil) ⇒ ThumbnailService

Returns a new instance of ThumbnailService.



22
23
24
25
# File 'app/services/youtube/thumbnail_service.rb', line 22

def initialize(account: nil, client: nil)
  @client = client || YouTube::ApiClient.new(account: )
  @logger = Rails.logger
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



20
21
22
# File 'app/services/youtube/thumbnail_service.rb', line 20

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'app/services/youtube/thumbnail_service.rb', line 20

def logger
  @logger
end

Class Method Details

.source_url_for_youtube_push(video) ⇒ Object

Public URL of the image that #push_thumbnail would upload (poster image, Dragonfly, or Cloudflare still).



28
29
30
# File 'app/services/youtube/thumbnail_service.rb', line 28

def self.source_url_for_youtube_push(video)
  new.source_url_for_youtube_push(video)
end

Instance Method Details

#push_thumbnail(video) ⇒ Boolean

Push the video's poster image to YouTube as the video thumbnail.

Parameters:

  • video (Video)

    must have youtube_id and a poster source

Returns:

  • (Boolean)

    true on success

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
# File 'app/services/youtube/thumbnail_service.rb', line 39

def push_thumbnail(video)
  raise ArgumentError, 'Video has no YouTube ID' if video.youtube_id.blank?

  url = thumbnail_source_url(video)
  raise ArgumentError, 'Video has no poster/thumbnail to push' if url.blank?

  download_and_push(video.youtube_id, url)
end

#source_url_for_youtube_push(video) ⇒ Object



32
33
34
# File 'app/services/youtube/thumbnail_service.rb', line 32

def source_url_for_youtube_push(video)
  thumbnail_source_url(video)
end