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 robot types and their parameters.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Step) initialize(robot, options = {})

Creates a new Step with the given robot.

Parameters:

  • the (String)

    robot to use

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

    the step's configuration options



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

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

Instance Attribute Details

- (Hash) options

The robot's options

Returns:

  • (Hash)

    the robot's options



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

def options
  @options
end

- (String) robot

The robot to use

Returns:

  • (String)

    the robot to use



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

def robot
  @robot
end

Instance Method Details

- (String) inspect

A human-readable version of the Step

Returns:

  • (String)

    a human-readable version of the Step



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

def inspect
  self.to_h[self.name].inspect
end

- (String) name

Automatically generates a unique, 32-character hex name for the step that uses this robot.

Returns:

  • (String)

    a randomly generated name



36
37
38
39
40
# File 'lib/transloadit/step.rb', line 36

def name
  # rand() is "good enough" for this; we generate 128 random bits (same
  # length as a UUID for future compatibility) and convert it to hex
  @name ||= rand(2 ** 128).to_s(16).rjust(32, '0')
end

- (Hash) to_h

A Transloadit-compatible Hash of the Step's contents

Returns:

  • (Hash)

    a Transloadit-compatible Hash of the Step's contents



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

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

- (String) to_json

JSON-encoded String containing the Step's hash contents

Returns:

  • (String)

    JSON-encoded String containing the Step's hash contents



85
86
87
# File 'lib/transloadit/step.rb', line 85

def to_json
  self.to_h.to_json
end

- (String, ...) use(input)

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.



58
59
60
61
62
63
64
65
66
# File 'lib/transloadit/step.rb', line 58

def use(input)
  self.options.delete(:use) and return if input.nil?
  
  self.options[:use] = case input
    when Symbol then input.inspect
    when Array  then input.map {|i| i.name }
    else             [ input.name ]
  end
end