Class: Bannerbear::V5::Client

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

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/bannerbear/v5/client.rb', line 100

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

#create_batch(payload = {}) ⇒ Object



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

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

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



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

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_instant_url(payload = {}) ⇒ Object



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

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_webhook(payload = {}) ⇒ Object



66
67
68
# File 'lib/bannerbear/v5/client.rb', line 66

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

#delete_instant_url(uid) ⇒ Object



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

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

#delete_webhook(uid) ⇒ Object



74
75
76
# File 'lib/bannerbear/v5/client.rb', line 74

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

#get_batch(uid) ⇒ Object



48
49
50
# File 'lib/bannerbear/v5/client.rb', line 48

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

#get_image(uid) ⇒ Object



34
35
36
# File 'lib/bannerbear/v5/client.rb', line 34

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



84
85
86
# File 'lib/bannerbear/v5/client.rb', line 84

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

#get_webhook(uid) ⇒ Object



62
63
64
# File 'lib/bannerbear/v5/client.rb', line 62

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

#list_batches(params = {}) ⇒ Object

Batches



44
45
46
# File 'lib/bannerbear/v5/client.rb', line 44

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



30
31
32
# File 'lib/bannerbear/v5/client.rb', line 30

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

#list_instant_urls(params = {}) ⇒ Object

Instant URLs



80
81
82
# File 'lib/bannerbear/v5/client.rb', line 80

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

#list_webhooks(params = {}) ⇒ Object

Webhooks



58
59
60
# File 'lib/bannerbear/v5/client.rb', line 58

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

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



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

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

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



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

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



70
71
72
# File 'lib/bannerbear/v5/client.rb', line 70

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