Class: Transloadit

Inherits:
Object
  • Object
show all
Defined in:
lib/transloadit.rb,
lib/transloadit/version.rb

Overview

Implements the Transloadit REST API in Ruby. Check the README for usage instructions.

Defined Under Namespace

Modules: Exception Classes: ApiModel, Assembly, Request, Response, Step, Template

Constant Summary collapse

VERSION =
"3.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Transloadit

Creates a new instance of the Transloadit API.

Options Hash (options):

  • :key (String)

    your auth key from the credentials page (required)

  • :secret (String)

    your auth secret from the credentials page, for signing requests (optional)



46
47
48
49
50
51
52
53
# File 'lib/transloadit.rb', line 46

def initialize(options = {})
  self.key = options[:key]
  self.secret = options[:secret]
  self.duration = options[:duration] || 5 * 60
  self.max_size = options[:max_size]

  _ensure_key_provided
end

Instance Attribute Details

#durationInteger



31
32
33
# File 'lib/transloadit.rb', line 31

def duration
  @duration
end

#keyString



24
25
26
# File 'lib/transloadit.rb', line 24

def key
  @key
end

#max_sizeObject

Returns the value of attribute max_size.



33
34
35
# File 'lib/transloadit.rb', line 33

def max_size
  @max_size
end

#secretString



27
28
29
# File 'lib/transloadit.rb', line 27

def secret
  @secret
end

Instance Method Details

#assembly(options = {}) ⇒ Object

Creates a Transloadit::Assembly ready to be sent to the REST API.

Options Hash (options):

  • :steps (Step, Array<Step>)

    the steps to perform in this assembly

  • :notify_url (String)

    A URL to be POSTed when the assembly has finished processing

  • :template_id (String)

    the ID of a template to use instead of specifying options here directly



83
84
85
# File 'lib/transloadit.rb', line 83

def assembly(options = {})
  Transloadit::Assembly.new(self, options)
end

#bill(month = Date.today.month, year = Date.today.year) ⇒ Object

Gets user billing reports for specified month and year. Defaults to current month or year if corresponding param is not specified.



106
107
108
109
110
111
112
# File 'lib/transloadit.rb', line 106

def bill(month = Date.today.month, year = Date.today.year)
  # convert month to 2 digit format
  month = format "%02d", month
  path = "bill/#{year}-#{month}"

  Transloadit::Request.new(path, secret).get({auth: to_hash})
end

#inspectString



117
118
119
# File 'lib/transloadit.rb', line 117

def inspect
  to_hash.inspect
end

#signed_smart_cdn_url(workspace:, template:, input:, expire_at_ms: nil, url_params: {}) ⇒ String

Returns Signed Smart CDN URL.

Raises:

  • (ArgumentError)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/transloadit.rb', line 144

def signed_smart_cdn_url(
  workspace:,
  template:,
  input:,
  expire_at_ms: nil,
  url_params: {}
)
  raise ArgumentError, "workspace is required" if workspace.nil? || workspace.empty?
  raise ArgumentError, "template is required" if template.nil? || template.empty?
  raise ArgumentError, "input is required" if input.nil?

  workspace_slug = CGI.escape(workspace)
  template_slug = CGI.escape(template)
  input_field = CGI.escape(input)

  expire_at = expire_at_ms || (Time.now.to_i * 1000 + 60 * 60 * 1000) # 1 hour default

  query_params = {}
  url_params.each do |key, value|
    next if value.nil?
    Array(value).each do |val|
      next if val.nil?
      (query_params[key.to_s] ||= []) << val.to_s
    end
  end

  query_params["auth_key"] = [key]
  query_params["exp"] = [expire_at.to_s]

  # Sort parameters to ensure consistent ordering
  sorted_params = query_params.sort.flat_map do |key, values|
    values.map { |v| "#{CGI.escape(key)}=#{CGI.escape(v)}" }
  end.join("&")

  string_to_sign = "#{workspace_slug}/#{template_slug}/#{input_field}?#{sorted_params}"

  signature = OpenSSL::HMAC.hexdigest("sha256", secret, string_to_sign)

  final_params = "#{sorted_params}&sig=#{CGI.escape("sha256:#{signature}")}"
  "https://#{workspace_slug}.tlcdn.com/#{template_slug}/#{input_field}?#{final_params}"
end

#step(name, robot, options = {}) ⇒ Step

Creates a Transloadit::Step describing a step in an upload assembly.



65
66
67
# File 'lib/transloadit.rb', line 65

def step(name, robot, options = {})
  Transloadit::Step.new(name, robot, options)
end

#template(options = {}) ⇒ Object

Creates a Transloadit::Template instance ready to interact with its corresponding REST API.

See the Transloadit documentation for further information on Templates and available endpoints.



93
94
95
# File 'lib/transloadit.rb', line 93

def template(options = {})
  Transloadit::Template.new(self, options)
end

#to_hashHash



124
125
126
127
128
129
# File 'lib/transloadit.rb', line 124

def to_hash
  result = {key: key}
  result.update(max_size: max_size) unless max_size.nil?
  result.update(expires: _generate_expiry) unless secret.nil?
  result
end

#to_jsonString



134
135
136
# File 'lib/transloadit.rb', line 134

def to_json
  MultiJson.dump(to_hash)
end