Class: VideoProcessing::CloudflareOperations
- Inherits:
-
Object
- Object
- VideoProcessing::CloudflareOperations
- 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.
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
-
#captions_status ⇒ Result
Get the English caption status from Cloudflare (live API call).
-
#delete_download(download_type) ⇒ Result
Delete a specific MP4 download from Cloudflare Stream.
-
#delete_video ⇒ Result
Delete the video from Cloudflare Stream.
-
#enable_download(download_type) ⇒ Result
Enable a specific type of MP4 download (default or audio) on Cloudflare.
-
#ensure_downloads_enabled ⇒ Result
Check if video downloads are enabled and enable the default if not.
-
#initialize(video) ⇒ CloudflareOperations
constructor
Initialize the operations service.
-
#refresh_data ⇒ Result
Refresh and cache Cloudflare video data including downloads and captions.
Constructor Details
#initialize(video) ⇒ CloudflareOperations
Initialize the operations service.
43 44 45 |
# File 'app/services/video_processing/cloudflare_operations.rb', line 43 def initialize(video) @video = video end |
Instance Method Details
#captions_status ⇒ Result
Get the English caption status from Cloudflare (live API call).
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.
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.) end |
#delete_video ⇒ Result
Delete the video from Cloudflare Stream.
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.
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.) end |
#ensure_downloads_enabled ⇒ Result
Check if video downloads are enabled and enable the default if not.
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_data ⇒ Result
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.
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 |