Class: Transloadit::Step

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

Overview

Implements the concept of a step in the Transloadit API. Each Step has a robot (e.g., ‘/image/resize’ or ‘/video/thumbnail’) and a hash of options specific to the chosen robot.

See the Transloadit documentation for futher information on robot types and their parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new Step with the given robot.

Parameters:

  • name (String)

    the explicit name to give the step

  • robot (String)

    the robot to use

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

    the configuration options for the robot; see Transloadit#step for possible values



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

def initialize(name, robot, options = {})
  self.name = name
  self.robot = robot
  self.options = options
end

Instance Attribute Details

#nameString

Returns the name to give the step.

Returns:

  • (String)

    the name to give the step



13
14
15
# File 'lib/transloadit/step.rb', line 13

def name
  @name
end

#optionsHash

Returns the robot’s options.

Returns:

  • (Hash)

    the robot’s options



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

def options
  @options
end

#robotString

Returns the robot to use.

Returns:

  • (String)

    the robot to use



16
17
18
# File 'lib/transloadit/step.rb', line 16

def robot
  @robot
end

Instance Method Details

#inspectString

Returns a human-readable version of the Step.

Returns:

  • (String)

    a human-readable version of the Step



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

def inspect
  to_hash[name].inspect
end

#to_hashHash

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

Returns:

  • (Hash)

    a Transloadit-compatible Hash of the Step’s contents



71
72
73
# File 'lib/transloadit/step.rb', line 71

def to_hash
  {name => options.merge(robot: robot)}
end

#to_jsonString

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

Returns:

  • (String)

    JSON-encoded String containing the Step’s hash contents



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

def to_json
  MultiJson.dump(to_hash)
end

#use(input) ⇒ String, ...

Specifies that this Step should process the provided input instead of the output of the Step before it.

Parameters:

  • input (Step, Array<Step>, Symbol, nil)

    The input step to use. Follows the conventions outlined in the online documentation. The symbol :original specifies that the original file should be sent to the robot. A Step indicates that this Step’s output should be used as the input to this one. Likewise, an array of Steps tells Transloadit to use pass each of their outputs to this Step. And lastly, an explicit nil clears the setting and restores it to its default input.

Returns:

  • (String, Array<String>, nil)

    The value for the :use parameter that will actually be sent to the REST API.



51
52
53
54
55
56
57
58
59
# File 'lib/transloadit/step.rb', line 51

def use(input)
  options.delete(:use) && return if input.nil?

  options[:use] = case input
  when Symbol then input.inspect
  when Array then input.map { |i| i.name }
  else [input.name]
  end
end