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],
  "resize_video"           => [:video_url, :width, :height, :fit],
  "crop_video"             => [:video_url, :x, :y, :width, :height],
  "overlay_video"          => [:base_video_url, :overlay_video_url, :x, :y, :scale, :start],
  "overlay_image"          => [:video_url, :image_url, :x, :y, :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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/bannerbear/v5/client.rb', line 194

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.



117
118
119
# File 'lib/bannerbear/v5/client.rb', line 117

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

#create_batch(payload = {}) ⇒ Object



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

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



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

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)


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

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



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

def create_webhook(payload = {})
  post_response "/webhooks", payload.slice(:name, :url, :resource, :event, :status, :scope, :templates)
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



190
191
192
# File 'lib/bannerbear/v5/client.rb', line 190

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

#delete_webhook(uid) ⇒ Object



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

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

#get_asset(uid) ⇒ Object



103
104
105
# File 'lib/bannerbear/v5/client.rb', line 103

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

#get_batch(uid) ⇒ Object



142
143
144
# File 'lib/bannerbear/v5/client.rb', line 142

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



178
179
180
# File 'lib/bannerbear/v5/client.rb', line 178

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

#get_publication(uid) ⇒ Object



127
128
129
# File 'lib/bannerbear/v5/client.rb', line 127

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

#get_tool_job(uid) ⇒ Object



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

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

#get_webhook(uid) ⇒ Object



156
157
158
# File 'lib/bannerbear/v5/client.rb', line 156

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

#install_publication(uid) ⇒ Object

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



132
133
134
# File 'lib/bannerbear/v5/client.rb', line 132

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

#list_assets(params = {}) ⇒ Object

Assets



99
100
101
# File 'lib/bannerbear/v5/client.rb', line 99

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

#list_batches(params = {}) ⇒ Object

Batches



138
139
140
# File 'lib/bannerbear/v5/client.rb', line 138

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



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

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

#list_publications(params = {}) ⇒ Object

Publications



123
124
125
# File 'lib/bannerbear/v5/client.rb', line 123

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

#list_tool_jobs(params = {}) ⇒ Object

Tool Jobs



89
90
91
# File 'lib/bannerbear/v5/client.rb', line 89

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

#list_webhooks(params = {}) ⇒ Object

Webhooks



152
153
154
# File 'lib/bannerbear/v5/client.rb', line 152

def list_webhooks(params = {})
  get_response "/webhooks?#{URI.encode_www_form(params.slice(:page))}"
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



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

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



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

def update_webhook(uid, payload = {})
  patch_response "/webhooks/#{uid}", payload.slice(:name, :url, :resource, :event, :status, :scope, :templates)
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.



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

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