Class: Bannerbear::V5::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bannerbear/v5/client.rb

Constant Summary collapse

TOOL_PARAMS =

Tools

Every tool is asynchronous: the POST returns a pending tool job. Poll get_tool_job until the status is "completed" or "failed", or subscribe to a webhook with the resource "tool_job".

{
  "remove_bg"              => [:image_url],
  "create_pdf"             => [:urls],
  "trim_video"             => [:video_url, :start, :end],
  "concat_videos"          => [:video_urls, :width, :height, :fps],
  "resize_video"           => [:video_url, :width, :height, :fit],
  "crop_video"             => [:video_url, :x, :y, :width, :height],
  # Place the overlay with either :position (+ optional :margin) or :x/:y,
  # not both.
  "overlay_video"          => [:base_video_url, :overlay_video_url, :x, :y, :position, :margin, :scale, :start],
  "overlay_image"          => [:video_url, :image_url, :x, :y, :position, :margin, :opacity],
  "subtitle_video"         => [:video_url, :language, :font, :font_size, :color, :bold, :italic,
                               :outline_color, :outline_width, :shadow_size, :shadow_color,
                               :background_style, :background_color, :alignment],
  "generate_voiceover"     => [:text, :voice],
  "add_audio"              => [:video_url, :audio_url, :mode, :volume, :loop, :ducking],
  "add_cover_art"          => [:video_url, :image_url],
  "create_video_slideshow" => [:image_urls, :slide_duration, :transition, :transition_duration, :width, :height],
  "apply_color_filter"     => [:video_url, :filter],
  "soften_video"           => [:video_url, :strength]
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Client

Returns a new instance of Client.



6
7
8
# File 'lib/bannerbear/v5/client.rb', line 6

def initialize(api_key = nil)
  @api_key = api_key || ENV["BANNERBEAR_API_KEY"]
end

Instance Method Details

#accountObject



10
11
12
# File 'lib/bannerbear/v5/client.rb', line 10

def 
  get_response "/account"
end

#build_instant_url(base_url, payload = {}) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/bannerbear/v5/client.rb', line 264

def build_instant_url(base_url, payload = {})
  modifications = payload[:modifications]
  mode          = (payload[:mode] || "encoded").to_s

  url = case mode
        when "encoded"
          data =
            if modifications.is_a?(Hash) && modifications.keys.map(&:to_s) == ["objects"]
              modifications[:objects] || modifications["objects"]
            else
              modifications
            end
          "#{base_url}?modifications=#{Base64.urlsafe_encode64(data.to_json, padding: false)}"
        when "named_params"
          template = modifications.is_a?(Hash) ? (modifications[:template] || modifications["template"]) : nil
          objects  = modifications.is_a?(Hash) ? (modifications[:objects]  || modifications["objects"])  : modifications
          parts = []
          (template || {}).each do |k, v|
            parts << "template:#{k}=#{URI.encode_www_form_component(v)}"
          end
          (objects || []).each do |obj|
            name = obj[:name] || obj["name"]
            obj.each do |k, v|
              key = k.to_s
              next if key == "name"
              parts << "#{name}:#{key}=#{URI.encode_www_form_component(v)}"
            end
          end
          "#{base_url}?#{parts.join('&')}"
        else
          raise ArgumentError, "unknown instant URL mode: #{mode.inspect}"
        end

  signing_key = payload[:signing_key]
  return url if signing_key.nil? || signing_key.empty?
  sig = OpenSSL::HMAC.hexdigest("SHA256", signing_key, url)
  "#{url}&s=#{sig}"
end

#check_assets(content_hashes) ⇒ Object

Returns a map of SHA-256 content hash => asset (or nil when the hash is not stored in this workspace). Max 100 hashes per call.



187
188
189
# File 'lib/bannerbear/v5/client.rb', line 187

def check_assets(content_hashes)
  post_response "/assets/check", { :content_hashes => content_hashes }
end

#create_animation(uid, payload = {}) ⇒ Object



86
87
88
# File 'lib/bannerbear/v5/client.rb', line 86

def create_animation(uid, payload = {})
  post_response "/animations", payload.slice(:modifications, :formats, :metadata).merge({:template => uid})
end

#create_animation_template(payload = {}) ⇒ Object



60
61
62
# File 'lib/bannerbear/v5/client.rb', line 60

def create_animation_template(payload = {})
  post_response "/animation_templates", payload.slice(:name, :description, :tags, :width, :height, :frame_rate)
end

#create_batch(payload = {}) ⇒ Object



216
217
218
# File 'lib/bannerbear/v5/client.rb', line 216

def create_batch(payload = {})
  post_response "/batches", payload.slice(:type, :items)
end

#create_image(uid, payload = {}) ⇒ Object



46
47
48
# File 'lib/bannerbear/v5/client.rb', line 46

def create_image(uid, payload = {})
  post_response "/images", payload.slice(:modifications, :formats, :scale, :dpi, :quality, :proxy, :metadata, :version).merge({:template => uid}), payload[:sync]
end

#create_image_template(payload = {}) ⇒ Object



24
25
26
# File 'lib/bannerbear/v5/client.rb', line 24

def create_image_template(payload = {})
  post_response "/image_templates", payload.slice(:name, :description, :tags, :width, :height, :config)
end

#create_instant_url(payload = {}) ⇒ Object



252
253
254
# File 'lib/bannerbear/v5/client.rb', line 252

def create_instant_url(payload = {})
  post_response "/instant_urls", payload.slice(:name, :template, :mode, :security, :status, :scale, :rate_limit, :template_version, :max_renders, :expires_at)
end

#create_tool_job(tool, payload = {}) ⇒ Object

Raises:

  • (ArgumentError)


146
147
148
149
150
# File 'lib/bannerbear/v5/client.rb', line 146

def create_tool_job(tool, payload = {})
  allowed = TOOL_PARAMS[tool.to_s]
  raise ArgumentError, "unknown tool: #{tool.inspect}" if allowed.nil?
  post_response "/tools/#{tool}", payload.slice(*allowed, :metadata)
end

#create_webhook(payload = {}) ⇒ Object



230
231
232
# File 'lib/bannerbear/v5/client.rb', line 230

def create_webhook(payload = {})
  post_response "/webhooks", payload.slice(:name, :url, :resource, :event, :status)
end

#create_workflow_run(uid, payload = {}) ⇒ Object



114
115
116
# File 'lib/bannerbear/v5/client.rb', line 114

def create_workflow_run(uid, payload = {})
  post_response "/workflow_runs", payload.slice(:inputs).merge({:workflow => uid})
end

#delete_animation_template(uid) ⇒ Object



68
69
70
# File 'lib/bannerbear/v5/client.rb', line 68

def delete_animation_template(uid)
  delete_response "/animation_templates/#{uid}"
end

#delete_image_template(uid) ⇒ Object



32
33
34
# File 'lib/bannerbear/v5/client.rb', line 32

def delete_image_template(uid)
  delete_response "/image_templates/#{uid}"
end

#delete_instant_url(uid) ⇒ Object



260
261
262
# File 'lib/bannerbear/v5/client.rb', line 260

def delete_instant_url(uid)
  delete_response "/instant_urls/#{uid}"
end

#delete_webhook(uid) ⇒ Object



238
239
240
# File 'lib/bannerbear/v5/client.rb', line 238

def delete_webhook(uid)
  delete_response "/webhooks/#{uid}"
end

#get_animation(uid) ⇒ Object



82
83
84
# File 'lib/bannerbear/v5/client.rb', line 82

def get_animation(uid)
  get_response "/animations/#{uid}"
end

#get_animation_template(uid) ⇒ Object



56
57
58
# File 'lib/bannerbear/v5/client.rb', line 56

def get_animation_template(uid)
  get_response "/animation_templates/#{uid}"
end

#get_asset(uid) ⇒ Object



173
174
175
# File 'lib/bannerbear/v5/client.rb', line 173

def get_asset(uid)
  get_response "/assets/#{uid}"
end

#get_batch(uid) ⇒ Object



212
213
214
# File 'lib/bannerbear/v5/client.rb', line 212

def get_batch(uid)
  get_response "/batches/#{uid}"
end

#get_image(uid) ⇒ Object



42
43
44
# File 'lib/bannerbear/v5/client.rb', line 42

def get_image(uid)
  get_response "/images/#{uid}"
end

#get_image_template(uid) ⇒ Object



20
21
22
# File 'lib/bannerbear/v5/client.rb', line 20

def get_image_template(uid)
  get_response "/image_templates/#{uid}"
end

#get_instant_url(uid) ⇒ Object



248
249
250
# File 'lib/bannerbear/v5/client.rb', line 248

def get_instant_url(uid)
  get_response "/instant_urls/#{uid}"
end

#get_publication(uid) ⇒ Object



197
198
199
# File 'lib/bannerbear/v5/client.rb', line 197

def get_publication(uid)
  get_response "/publications/#{uid}"
end

#get_tool_job(uid) ⇒ Object



163
164
165
# File 'lib/bannerbear/v5/client.rb', line 163

def get_tool_job(uid)
  get_response "/tool_jobs/#{uid}"
end

#get_webhook(uid) ⇒ Object



226
227
228
# File 'lib/bannerbear/v5/client.rb', line 226

def get_webhook(uid)
  get_response "/webhooks/#{uid}"
end

#get_workflow(uid) ⇒ Object



96
97
98
# File 'lib/bannerbear/v5/client.rb', line 96

def get_workflow(uid)
  get_response "/workflows/#{uid}"
end

#get_workflow_run(uid) ⇒ Object



110
111
112
# File 'lib/bannerbear/v5/client.rb', line 110

def get_workflow_run(uid)
  get_response "/workflow_runs/#{uid}"
end

#install_publication(uid) ⇒ Object

Clones a publication into the workspace as a new image template.



202
203
204
# File 'lib/bannerbear/v5/client.rb', line 202

def install_publication(uid)
  post_response "/publications/#{uid}/install", {}
end

#list_animation_templates(params = {}) ⇒ Object

Animation Templates



52
53
54
# File 'lib/bannerbear/v5/client.rb', line 52

def list_animation_templates(params = {})
  get_response "/animation_templates?#{URI.encode_www_form(params.slice(:page))}"
end

#list_animations(params = {}) ⇒ Object

Animations

Rendering is always asynchronous — there is no synchronous host for animations. Poll get_animation until the status is "completed" or "failed", or subscribe to a webhook with the resource "animation".



78
79
80
# File 'lib/bannerbear/v5/client.rb', line 78

def list_animations(params = {})
  get_response "/animations?#{URI.encode_www_form(params.slice(:page))}"
end

#list_assets(params = {}) ⇒ Object

Assets



169
170
171
# File 'lib/bannerbear/v5/client.rb', line 169

def list_assets(params = {})
  get_response "/assets?#{URI.encode_www_form(params.slice(:page))}"
end

#list_batches(params = {}) ⇒ Object

Batches



208
209
210
# File 'lib/bannerbear/v5/client.rb', line 208

def list_batches(params = {})
  get_response "/batches?#{URI.encode_www_form(params.slice(:page))}"
end

#list_image_templates(params = {}) ⇒ Object

Image Templates



16
17
18
# File 'lib/bannerbear/v5/client.rb', line 16

def list_image_templates(params = {})
  get_response "/image_templates?#{URI.encode_www_form(params.slice(:page))}"
end

#list_images(params = {}) ⇒ Object

Images



38
39
40
# File 'lib/bannerbear/v5/client.rb', line 38

def list_images(params = {})
  get_response "/images?#{URI.encode_www_form(params.slice(:page))}"
end

#list_instant_urls(params = {}) ⇒ Object

Instant URLs



244
245
246
# File 'lib/bannerbear/v5/client.rb', line 244

def list_instant_urls(params = {})
  get_response "/instant_urls?#{URI.encode_www_form(params.slice(:page))}"
end

#list_publications(params = {}) ⇒ Object

Publications



193
194
195
# File 'lib/bannerbear/v5/client.rb', line 193

def list_publications(params = {})
  get_response "/publications?#{URI.encode_www_form(params.slice(:page))}"
end

#list_tool_jobs(params = {}) ⇒ Object

Tool Jobs



159
160
161
# File 'lib/bannerbear/v5/client.rb', line 159

def list_tool_jobs(params = {})
  get_response "/tool_jobs?#{URI.encode_www_form(params.slice(:page))}"
end

#list_webhooks(params = {}) ⇒ Object

Webhooks



222
223
224
# File 'lib/bannerbear/v5/client.rb', line 222

def list_webhooks(params = {})
  get_response "/webhooks?#{URI.encode_www_form(params.slice(:page))}"
end

#list_workflow_runs(params = {}) ⇒ Object

Workflow Runs

A run is asynchronous. Poll get_workflow_run until the status is "completed" or "failed", or subscribe to a webhook with the resource "workflow_run".



106
107
108
# File 'lib/bannerbear/v5/client.rb', line 106

def list_workflow_runs(params = {})
  get_response "/workflow_runs?#{URI.encode_www_form(params.slice(:page))}"
end

#list_workflows(params = {}) ⇒ Object

Workflows



92
93
94
# File 'lib/bannerbear/v5/client.rb', line 92

def list_workflows(params = {})
  get_response "/workflows?#{URI.encode_www_form(params.slice(:page))}"
end

#update_animation_template(uid, payload = {}) ⇒ Object



64
65
66
# File 'lib/bannerbear/v5/client.rb', line 64

def update_animation_template(uid, payload = {})
  patch_response "/animation_templates/#{uid}", payload.slice(:name, :description, :tags, :width, :height, :frame_rate)
end

#update_image_template(uid, payload = {}) ⇒ Object



28
29
30
# File 'lib/bannerbear/v5/client.rb', line 28

def update_image_template(uid, payload = {})
  patch_response "/image_templates/#{uid}", payload.slice(:name, :description, :tags, :width, :height, :config)
end

#update_instant_url(uid, payload = {}) ⇒ Object



256
257
258
# File 'lib/bannerbear/v5/client.rb', line 256

def update_instant_url(uid, payload = {})
  patch_response "/instant_urls/#{uid}", payload.slice(:name, :template, :mode, :security, :status, :scale, :rate_limit, :template_version, :max_renders, :expires_at)
end

#update_webhook(uid, payload = {}) ⇒ Object



234
235
236
# File 'lib/bannerbear/v5/client.rb', line 234

def update_webhook(uid, payload = {})
  patch_response "/webhooks/#{uid}", payload.slice(:name, :url, :resource, :event, :status)
end

#upload_asset(data, content_type) ⇒ Object

Uploads raw file bytes (max 5MB). content_type must be the mime type of the data, e.g. "image/png" or "video/mp4". Uploads are deduplicated per workspace by SHA-256, so re-uploading the same bytes returns the existing asset instead of creating a duplicate.



181
182
183
# File 'lib/bannerbear/v5/client.rb', line 181

def upload_asset(data, content_type)
  upload_response "/assets", data, content_type
end