Class: Transloadit
- Inherits:
-
Object
- Object
- Transloadit
- 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.0"
Instance Attribute Summary collapse
-
#duration ⇒ Integer
The duration in seconds that signed API requests generated from this instance remain valid.
-
#key ⇒ String
Your Transloadit auth key.
-
#max_size ⇒ Object
Returns the value of attribute max_size.
-
#secret ⇒ String
Your Transloadit auth secret, for signing requests.
Instance Method Summary collapse
-
#assembly(options = {}) ⇒ Object
Creates a Transloadit::Assembly ready to be sent to the REST API.
-
#bill(month = Date.today.month, year = Date.today.year) ⇒ Object
Gets user billing reports for specified month and year.
-
#initialize(options = {}) ⇒ Transloadit
constructor
Creates a new instance of the Transloadit API.
-
#inspect ⇒ String
A human-readable version of the Transloadit.
-
#signed_smart_cdn_url(workspace:, template:, input:, expire_at_ms: nil, url_params: {}) ⇒ String
Signed Smart CDN URL.
-
#step(name, robot, options = {}) ⇒ Step
Creates a Transloadit::Step describing a step in an upload assembly.
-
#template(options = {}) ⇒ Object
Creates a Transloadit::Template instance ready to interact with its corresponding REST API.
-
#to_hash ⇒ Hash
A Transloadit-compatible Hash of the instance’s contents.
-
#to_json ⇒ String
JSON-encoded String containing the object’s hash contents.
Constructor Details
#initialize(options = {}) ⇒ Transloadit
Creates a new instance of the Transloadit API.
46 47 48 49 50 51 52 53 |
# File 'lib/transloadit.rb', line 46 def initialize( = {}) self.key = [:key] self.secret = [:secret] self.duration = [:duration] || 5 * 60 self.max_size = [:max_size] _ensure_key_provided end |
Instance Attribute Details
#duration ⇒ Integer
Returns the duration in seconds that signed API requests generated from this instance remain valid.
31 32 33 |
# File 'lib/transloadit.rb', line 31 def duration @duration end |
#key ⇒ String
Returns your Transloadit auth key.
24 25 26 |
# File 'lib/transloadit.rb', line 24 def key @key end |
#max_size ⇒ Object
Returns the value of attribute max_size.
33 34 35 |
# File 'lib/transloadit.rb', line 33 def max_size @max_size end |
#secret ⇒ String
Returns your Transloadit auth secret, for signing requests.
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.
83 84 85 |
# File 'lib/transloadit.rb', line 83 def assembly( = {}) Transloadit::Assembly.new(self, ) 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 |
#inspect ⇒ String
Returns a human-readable version of the Transloadit.
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.
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, = {}) Transloadit::Step.new(name, robot, ) 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( = {}) Transloadit::Template.new(self, ) end |
#to_hash ⇒ Hash
Returns a Transloadit-compatible Hash of the instance’s contents.
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_json ⇒ String
Returns JSON-encoded String containing the object’s hash contents.
134 135 136 |
# File 'lib/transloadit.rb', line 134 def to_json MultiJson.dump(to_hash) end |