forked from transloadit/ruby-sdk
/
step.rb
85 lines (74 loc) · 2.45 KB
/
step.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require "transloadit"
#
# 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}[https://transloadit.com/docs/building-assembly-instructions]
# for further information on robot types and their parameters.
#
class Transloadit::Step
# @return [String] the name to give the step
attr_accessor :name
# @return [String] the robot to use
attr_reader :robot
# @return [Hash] the robot's options
attr_accessor :options
#
# Creates a new Step with the given +robot+.
#
# @param [String] name the explicit name to give the step
# @param [String] robot the robot to use
# @param [Hash] options the configuration options for the robot; see
# {Transloadit#step} for possible values
#
def initialize(name, robot, options = {})
self.name = name
self.robot = robot
self.options = options
end
#
# Specifies that this Step should process the provided +input+ instead of
# the output of the Step before it.
#
# @param [Step, Array<Step>, Symbol, nil] input The input
# step to use. Follows the conventions outlined in the
# online {documentation}[https://transloadit.com/docs/building-assembly-instructions#special-parameters].
# 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.
#
# @return [String, Array<String>, nil> The value for the +:use+ parameter
# that will actually be sent to the REST API.
#
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
#
# @return [String] a human-readable version of the Step
#
def inspect
to_hash[name].inspect
end
#
# @return [Hash] a Transloadit-compatible Hash of the Step's contents
#
def to_hash
{name => options.merge(robot: robot)}
end
#
# @return [String] JSON-encoded String containing the Step's hash contents
#
def to_json
MultiJson.dump(to_hash)
end
protected
attr_writer :robot
end