Class: VideoProcessing::CloudflareOperations

Inherits:
Object
  • Object
show all
Defined in:
app/services/video_processing/cloudflare_operations.rb

Overview

Service object for Cloudflare Stream API operations on a Video record.

Extracted from Video to move cross-boundary orchestration (API calls to
CloudflareStreamApi + persistence) out of the model. Pure data accessors
that read the cached cloudflare_data JSONB stay on Video; this service
holds the methods that actually call the Cloudflare API and write back.

All methods return a Result struct following the service-architecture
convention — never a bare boolean, hash, or nil.

Examples:

result = VideoProcessing::CloudflareOperations.new(video).refresh_data
result.success? # => true

See Also:

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(video) ⇒ CloudflareOperations

Initialize the operations service.

Parameters:

  • video (Video)

    the Video record this service will operate on



43
44
45
# File 'app/services/video_processing/cloudflare_operations.rb', line 43

def initialize(video)
  @video = video
end

Instance Method Details

#captions_statusResult

Get the English caption status from Cloudflare (live API call).

Returns:

  • (Result)

    with caption set to the English caption hash, or nil
    if not found / not a Cloudflare video.



131
132
133
134
135
136
# File 'app/services/video_processing/cloudflare_operations.rb', line 131

def captions_status
  return Result.new(success: false, error: 'Not a Cloudflare video') unless @video.is_cloudflare?

  caption = fetch_captions.find { |c| c['language'] == 'en' }
  Result.new(success: true, caption:)
end

#delete_download(download_type) ⇒ Result

Delete a specific MP4 download from Cloudflare Stream.

Parameters:

  • download_type (String)

    the download type to delete.

Returns:



107
108
109
110
111
112
113
114
# File 'app/services/video_processing/cloudflare_operations.rb', line 107

def delete_download(download_type)
  return Result.new(success: false, error: 'Not a Cloudflare video') unless @video.is_cloudflare?

  CloudflareStreamApi.instance.delete_mp4_download(@video.cloudflare_uid, download_type)
  Result.new(success: true)
rescue StandardError => e
  Result.new(success: false, error: e.message)
end

#delete_videoResult

Delete the video from Cloudflare Stream.

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/video_processing/cloudflare_operations.rb', line 74

def delete_video
  return Result.new(success: false, error: 'Not a Cloudflare video') unless @video.is_cloudflare?

  Rails.logger.info "Deleting video #{@video.id} from Cloudflare Stream (UID: #{@video.cloudflare_uid})"

  success = CloudflareStreamApi.instance.delete_video(@video.cloudflare_uid)

  if success
    Rails.logger.info "Successfully deleted video #{@video.id} from Cloudflare Stream"
    Result.new(success: true)
  else
    Rails.logger.error "Failed to delete video #{@video.id} from Cloudflare Stream"
    Result.new(success: false, error: 'Cloudflare API deletion failed')
  end
end

#enable_download(download_type) ⇒ Result

Enable a specific type of MP4 download (default or audio) on Cloudflare.

Parameters:

  • download_type (String)

    the download type to enable.

Returns:



94
95
96
97
98
99
100
101
# File 'app/services/video_processing/cloudflare_operations.rb', line 94

def enable_download(download_type)
  return Result.new(success: false, error: 'Not a Cloudflare video') unless @video.is_cloudflare?

  CloudflareStreamApi.instance.enable_mp4_download(@video.cloudflare_uid, download_type)
  Result.new(success: true)
rescue StandardError => e
  Result.new(success: false, error: e.message)
end

#ensure_downloads_enabledResult

Check if video downloads are enabled and enable the default if not.

Returns:



119
120
121
122
123
124
125
# File 'app/services/video_processing/cloudflare_operations.rb', line 119

def ensure_downloads_enabled
  return Result.new(success: false, error: 'Not a Cloudflare video') unless @video.is_cloudflare?
  return Result.new(success: true) if @video.has_default_download?

  Rails.logger.info "Enabling video downloads for video #{@video.cloudflare_uid}"
  enable_default_download
end

#refresh_dataResult

Refresh and cache Cloudflare video data including downloads and captions.
Fetches the video record, downloads metadata, and captions list from
Cloudflare Stream, merges them, and persists to cloudflare_data.

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/video_processing/cloudflare_operations.rb', line 52

def refresh_data
  return Result.new(success: false, error: 'Not a Cloudflare video') unless @video.is_cloudflare?

  video_data = CloudflareStreamApi.instance.get_video(@video.cloudflare_uid)

  if video_data
    downloads_data = CloudflareStreamApi.instance.get_downloads(@video.cloudflare_uid)
    video_data['downloads'] = downloads_data if downloads_data

    captions_data = fetch_captions
    video_data['captions'] = captions_data if captions_data.present?

    @video.update!(cloudflare_data: video_data)
    Result.new(success: true)
  else
    Result.new(success: false, error: 'Cloudflare API returned no data')
  end
end