Class: ILoveVideoEditor::Client
- Inherits:
-
Object
- Object
- ILoveVideoEditor::Client
- Defined in:
- lib/ilovevideoeditor/client.rb
Overview
High-level iLoveVideoEditor client with polling and friendly method names.
Usage:
client = ILoveVideoEditor::Client.new(api_key: 'vf_live_xxx')
result = client.render({ name: 'Hello', layers: [...] })
puts result.download_url
Defined Under Namespace
Classes: RenderResult
Class Method Summary collapse
-
.progress_percent(progress) ⇒ Object
Normalize the API progress payload (total, percent) to a percent number.
Instance Method Summary collapse
- #get_render(job_id) ⇒ Object
- #get_template(id) ⇒ Object
-
#initialize(api_key:, base_url: 'https://api.ilovevideoeditor.com') ⇒ Client
constructor
A new instance of Client.
- #list_templates ⇒ Object
- #refresh_url(job_id) ⇒ Object
-
#render(video_json, poll_interval: 2, max_wait: 300, on_progress: nil) ⇒ Object
Submit a VideoJSON payload and block until the render finishes.
Constructor Details
#initialize(api_key:, base_url: 'https://api.ilovevideoeditor.com') ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ilovevideoeditor/client.rb', line 17 def initialize(api_key:, base_url: 'https://api.ilovevideoeditor.com') raise ArgumentError, 'api_key is required' if api_key.nil? || api_key.empty? ILoveVideoEditor.configure do |config| config.host = base_url config.api_key['x-api-key'] = api_key end @render_api = ILoveVideoEditor::RenderApi.new @templates_api = ILoveVideoEditor::TemplatesApi.new end |
Class Method Details
.progress_percent(progress) ⇒ Object
Normalize the API progress payload (total, percent) to a percent number.
30 31 32 33 34 35 |
# File 'lib/ilovevideoeditor/client.rb', line 30 def self.progress_percent(progress) return 0.0 if progress.nil? return progress.to_f if progress.is_a?(Numeric) progress.respond_to?(:percent) && progress.percent ? progress.percent.to_f : 0.0 end |
Instance Method Details
#get_render(job_id) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ilovevideoeditor/client.rb', line 80 def get_render(job_id) status = @render_api.get_render_status(job_id) RenderResult.new( job_id: status.job_id, status: status.status, progress: self.class.progress_percent(status.progress), url: status.url, error: status.error, created_at: status.created_at, completed_at: status.completed_at ) end |
#get_template(id) ⇒ Object
102 103 104 |
# File 'lib/ilovevideoeditor/client.rb', line 102 def get_template(id) @templates_api.get_template(id).template end |
#list_templates ⇒ Object
98 99 100 |
# File 'lib/ilovevideoeditor/client.rb', line 98 def list_templates @templates_api.list_templates.templates end |
#refresh_url(job_id) ⇒ Object
93 94 95 96 |
# File 'lib/ilovevideoeditor/client.rb', line 93 def refresh_url(job_id) result = @render_api.refresh_render_url(job_id) result.download_url end |
#render(video_json, poll_interval: 2, max_wait: 300, on_progress: nil) ⇒ Object
Submit a VideoJSON payload and block until the render finishes.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ilovevideoeditor/client.rb', line 38 def render(video_json, poll_interval: 2, max_wait: 300, on_progress: nil) body = ILoveVideoEditor::QueueRenderRequest.new(video_json: video_json) queued = @render_api.queue_render(body) job_id = queued.job_id deadline = Time.now + max_wait while Time.now < deadline status = @render_api.get_render_status(job_id) progress = self.class.progress_percent(status.progress) on_progress&.call(status.status, progress) if status.status == 'completed' refresh = @render_api.refresh_render_url(job_id) return RenderResult.new( job_id: job_id, status: status.status, progress: progress, url: status.url, download_url: refresh.download_url, error: status.error, created_at: status.created_at, completed_at: status.completed_at ) end if status.status == 'failed' return RenderResult.new( job_id: job_id, status: status.status, progress: progress, error: status.error, created_at: status.created_at, completed_at: status.completed_at ) end sleep(poll_interval) end raise Timeout::Error, "Render #{job_id} did not complete within #{max_wait}s" end |