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.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Transloadit

Creates a new instance of the Transloadit API.

Parameters:

  • options (Hash) (defaults to: {})

    a hash of options, which can be any of:

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)



41
42
43
44
45
46
47
48
# File 'lib/transloadit.rb', line 41

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

Returns the duration in seconds that signed API requests generated from this instance remain valid.

Returns:

  • (Integer)

    the duration in seconds that signed API requests generated from this instance remain valid



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

def duration
  @duration
end

#keyString

Returns your Transloadit auth key.

Returns:

  • (String)

    your Transloadit auth key



19
20
21
# File 'lib/transloadit.rb', line 19

def key
  @key
end

#max_sizeObject

Returns the value of attribute max_size.



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

def max_size
  @max_size
end

#secretString

Returns your Transloadit auth secret, for signing requests.

Returns:

  • (String)

    your Transloadit auth secret, for signing requests



22
23
24
# File 'lib/transloadit.rb', line 22

def secret
  @secret
end

Instance Method Details

#assembly(options = {}) ⇒ Object

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

Parameters:

  • options (Hash) (defaults to: {})

    additional parameters to send with the assembly submission; for a full list of parameters, see the official documentation on templates.

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



78
79
80
# File 'lib/transloadit.rb', line 78

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.

Parameters:

  • month (Integer) (defaults to: Date.today.month)

    the month for which billing reports should be retrieved. defaults to current month if not specified.

  • year (Integer) (defaults to: Date.today.year)

    the year for which billing reports should be retrieved. defaults to current year if not specified.



101
102
103
104
105
106
107
# File 'lib/transloadit.rb', line 101

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

Returns a human-readable version of the Transloadit.

Returns:

  • (String)

    a human-readable version of the Transloadit.



112
113
114
# File 'lib/transloadit.rb', line 112

def inspect
  to_hash.inspect
end

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

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

Parameters:

  • name (String)

    the name to give the step

  • robot (String)

    the robot to use in this step (e.g., ‘/image/resize’)

  • options (Hash) (defaults to: {})

    a hash of options to customize the robot’s operation; see the online documentation for robot-specific options

Returns:

  • (Step)

    the created Step



60
61
62
# File 'lib/transloadit.rb', line 60

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 futher information on Templates and available endpoints.



88
89
90
# File 'lib/transloadit.rb', line 88

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

#to_hashHash

Returns a Transloadit-compatible Hash of the instance’s contents.

Returns:

  • (Hash)

    a Transloadit-compatible Hash of the instance’s contents



119
120
121
122
123
124
# File 'lib/transloadit.rb', line 119

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

Returns JSON-encoded String containing the object’s hash contents.

Returns:

  • (String)

    JSON-encoded String containing the object’s hash contents



129
130
131
# File 'lib/transloadit.rb', line 129

def to_json
  MultiJson.dump(to_hash)
end