-
Notifications
You must be signed in to change notification settings - Fork 20
/
transloadit.rb
153 lines (135 loc) · 4.7 KB
/
transloadit.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require "multi_json"
require "date"
#
# Implements the Transloadit REST API in Ruby. Check the {file:README.md README}
# for usage instructions.
#
class Transloadit
autoload :ApiModel, "transloadit/api_model"
autoload :Assembly, "transloadit/assembly"
autoload :Exception, "transloadit/exception"
autoload :Request, "transloadit/request"
autoload :Response, "transloadit/response"
autoload :Step, "transloadit/step"
autoload :Template, "transloadit/template"
autoload :VERSION, "transloadit/version"
# @return [String] your Transloadit auth key
attr_accessor :key
# @return [String] your Transloadit auth secret, for signing requests
attr_accessor :secret
# @return [Integer] the duration in seconds that signed API requests
# generated from this instance remain valid
attr_accessor :duration
attr_accessor :max_size
#
# Creates a new instance of the Transloadit API.
#
# @param [Hash] options a hash of options, which can be any of:
# @option options [String] :key your auth key from the
# {credentials}[https://transloadit.com/accounts/credentials] page
# (required)
# @option options [String] :secret your auth secret from the
# {credentials}[https://transloadit.com/accounts/credentials] page, for
# signing requests (optional)
#
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
#
# Creates a Transloadit::Step describing a step in an upload assembly.
#
# @param [String] name the name to give the step
# @param [String] robot the robot to use in this step (e.g., '/image/resize')
# @param [Hash] options a hash of options to customize the robot's
# operation; see the {online documentation}[https://transloadit.com/docs/building-assembly-instructions]
# for robot-specific options
# @return [Step] the created Step
#
def step(name, robot, options = {})
Transloadit::Step.new(name, robot, options)
end
#
# Creates a Transloadit::Assembly ready to be sent to the REST API.
#
# @param [Hash] options additional parameters to send with the assembly
# submission; for a full list of parameters, see the official
# documentation on {templates}[https://transloadit.com/docs/templates].
# @option options [Step, Array<Step>] :steps the steps to perform in this
# assembly
# @option options [String] :notify_url A URL to be POSTed when the assembly
# has finished processing
# @option options [String] :template_id the ID of a
# {template}[https://transloadit.com/templates] to use instead of
# specifying options here directly
#
def assembly(options = {})
Transloadit::Assembly.new(self, options)
end
#
# Creates a Transloadit::Template instance ready to interact with its corresponding REST API.
#
# See the Transloadit {documentation}[https://transloadit.com/docs/api-docs/#template-api]
# for further information on Templates and available endpoints.
#
def template(options = {})
Transloadit::Template.new(self, options)
end
#
# Gets user billing reports for specified month and year.
# Defaults to current month or year if corresponding param is not specified.
#
# @param [Integer] month the month for which billing reports should be retrieved.
# defaults to current month if not specified.
# @param [Integer] year the year for which billing reports should be retrieved.
# defaults to current year if not specified.
#
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
#
# @return [String] a human-readable version of the Transloadit.
#
def inspect
to_hash.inspect
end
#
# @return [Hash] a Transloadit-compatible Hash of the instance's contents
#
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
#
# @return [String] JSON-encoded String containing the object's hash contents
#
def to_json
MultiJson.dump(to_hash)
end
private
#
# Raises an ArgumentError if no {#key} has been assigned.
#
def _ensure_key_provided
unless key
raise ArgumentError, "an authentication key must be provided"
end
end
#
# Generates an API-compatible request expiration timestamp. Uses the
# current instance's duration.
#
# @return [String] an API-compatible timestamp
#
def _generate_expiry
(Time.now + duration).utc.strftime("%Y/%m/%d %H:%M:%S+00:00")
end
end