Skip to content

Commit a3d8049

Browse files
kvzrmehner
andauthored
Add Smart CDN Signature Auth helper (#71)
* Create ruby-sdk.code-workspace * Update README.md * Update test_assembly.rb * Create smartcdn-sig.ts * wip * Wip * Create Makefile * Update smartcdn-sig.ts * Cleanup * Move to tl instance * Wip * Inline smart cdn method * Readme * More comprehensive node comparison * Make no tsx fatal for TEST_NODE_PARITY * Update ci.yml * Document test flags * Support empty input * Update ruby-sdk.code-workspace * Assert empty string param behavior * Make empty string param show up in url * Update test_smart_cdn_node_parity.rb * Harcode urls * Merge parity testing into regular suite * Retire expireIn * Remove ability to override auth keys * Fix: redundant self * Fix linting * Fixes * Validate both implementations against hardcoded URL * Update lib/transloadit.rb Co-authored-by: Robin Mehner <[email protected]> * Add flat_map. Thx @rmehner! Co-Authored-By: Robin Mehner <[email protected]> * Update transloadit.rb Co-Authored-By: Robin Mehner <[email protected]> * Ensure workspace and template can't be empty strings --------- Co-authored-by: Robin Mehner <[email protected]>
1 parent ca7df0f commit a3d8049

File tree

9 files changed

+460
-50
lines changed

9 files changed

+460
-50
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
name: CI
22
on:
33
push:
4-
branches: [ main ]
4+
branches:
5+
- main
56
pull_request:
6-
branches: [ main ]
7+
types:
8+
- opened
9+
- synchronize
710
jobs:
811
ci:
912
runs-on: ubuntu-latest
@@ -13,15 +16,19 @@ jobs:
1316
- 3.0
1417
- 3.1
1518
- 3.2
19+
- 3.3
1620
- jruby
17-
# - jruby-head
1821
- truffleruby
19-
# - truffleruby-head
22+
fail-fast: false
2023
steps:
21-
- uses: actions/checkout@v2
24+
- uses: actions/checkout@v3
25+
- uses: actions/setup-node@v4
26+
with:
27+
node-version: 20
28+
- run: npm install -g tsx
2229
- uses: ruby/setup-ruby@v1
2330
with:
2431
ruby-version: ${{ matrix.ruby }}
2532
bundler-cache: true
2633
- run: bundle exec standardrb
27-
- run: COVERAGE=false bundle exec rake test
34+
- run: COVERAGE=0 TEST_NODE_PARITY=1 bundle exec rake test

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ transloadit-*.gem
1313

1414
.DS_Store
1515
env.sh
16-
17-
# Bundle directory
16+
.env
1817
vendor/bundle/

.vscode/ruby-sdk.code-workspace

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"folders": [
3+
{
4+
"path": ".."
5+
}
6+
],
7+
"settings": {
8+
"workbench.colorCustomizations": {
9+
"titleBar.activeForeground": "#ffffff",
10+
"titleBar.activeBackground": "#cc0000"
11+
},
12+
}
13+
}

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.PHONY: all test fix
2+
3+
all: fix test
4+
5+
# Run tests
6+
test:
7+
bundle exec rake test
8+
9+
# Fix code formatting
10+
fix:
11+
bundle exec standardrb --fix
12+
13+
# Install dependencies
14+
install:
15+
bundle install
16+
17+
# Run both fix and test
18+
check: fix test

README.md

Lines changed: 103 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A **Ruby** Integration for [Transloadit](https://transloadit.com)'s file uploadi
1111

1212
This is a **Ruby** SDK to make it easy to talk to the [Transloadit](https://transloadit.com) REST API.
1313

14-
*If you run Ruby on Rails and are looking to integrate with the browser for file uploads, checkout the [rails-sdk](https://github.com/transloadit/rails-sdk).*
14+
_If you run Ruby on Rails and are looking to integrate with the browser for file uploads, checkout the [rails-sdk](https://github.com/transloadit/rails-sdk)._
1515

1616
## Install
1717

@@ -29,14 +29,14 @@ $ irb -rubygems
2929
=> true
3030
```
3131

32-
Then create a Transloadit instance, which will maintain your
33-
[authentication credentials](https://transloadit.com/accounts/credentials)
32+
Then create a Transloadit instance, which will maintain your
33+
[authentication credentials](https://transloadit.com/accounts/credentials)
3434
and allow us to make requests to [the API](https://transloadit.com/docs/api/).
3535

3636
```ruby
3737
transloadit = Transloadit.new(
38-
:key => 'YOUR_TRANSLOADIT_KEY',
39-
:secret => 'YOUR_TRANSLOADIT_SECRET'
38+
:key => 'MY_TRANSLOADIT_KEY',
39+
:secret => 'MY_TRANSLOADIT_SECRET'
4040
)
4141
```
4242

@@ -49,8 +49,8 @@ and store the result on [Amazon S3](https://aws.amazon.com/s3/).
4949
require 'transloadit'
5050

5151
transloadit = Transloadit.new(
52-
:key => 'YOUR_TRANSLOADIT_KEY',
53-
:secret => 'YOUR_TRANSLOADIT_SECRET'
52+
:key => 'MY_TRANSLOADIT_KEY',
53+
:secret => 'MY_TRANSLOADIT_SECRET'
5454
)
5555

5656
# First, we create two steps: one to resize the image to 320x240, and another to
@@ -60,9 +60,9 @@ resize = transloadit.step 'resize', '/image/resize',
6060
:height => 240
6161

6262
store = transloadit.step 'store', '/s3/store',
63-
:key => 'YOUR_AWS_KEY',
64-
:secret => 'YOUR_AWS_SECRET',
65-
:bucket => 'YOUR_S3_BUCKET'
63+
:key => 'MY_AWS_KEY',
64+
:secret => 'MY_AWS_SECRET',
65+
:bucket => 'MY_S3_BUCKET'
6666

6767
# Now that we have the steps, we create an assembly (which is just a request to
6868
# process a file or set of files) and let Transloadit do the rest.
@@ -125,7 +125,7 @@ API at the time the <dfn>Assembly</dfn> was created. You have to explicitly ask
125125
# reloads the response's contents from the REST API
126126
response.reload!
127127

128-
# reloads once per second until all processing is finished, up to number of
128+
# reloads once per second until all processing is finished, up to number of
129129
# times specified in :tries option, otherwise will raise ReloadLimitReached
130130
response.reload_until_finished! tries: 300 # default is 600
131131
```
@@ -147,8 +147,8 @@ than one file in the same request. You can also pass a single <dfn>Step</dfn> fo
147147
require 'transloadit'
148148

149149
transloadit = Transloadit.new(
150-
:key => 'YOUR_TRANSLOADIT_KEY',
151-
:secret => 'YOUR_TRANSLOADIT_SECRET'
150+
:key => 'MY_TRANSLOADIT_KEY',
151+
:secret => 'MY_TRANSLOADIT_SECRET'
152152
)
153153

154154
assembly = transloadit.assembly(steps: store)
@@ -160,7 +160,7 @@ response = assembly.create!(
160160
)
161161
```
162162

163-
You can also pass an array of files to the `create!` method.
163+
You can also pass an array of files to the `create!` method.
164164
Just unpack the array using the splat `*` operator.
165165

166166
```ruby
@@ -178,8 +178,8 @@ simply need to `use` other <dfn>Steps</dfn>. Following
178178
require 'transloadit'
179179

180180
transloadit = Transloadit.new(
181-
:key => 'YOUR_TRANSLOADIT_KEY',
182-
:secret => 'YOUR_TRANSLOADIT_SECRET'
181+
:key => 'MY_TRANSLOADIT_KEY',
182+
:secret => 'MY_TRANSLOADIT_SECRET'
183183
)
184184

185185
encode = transloadit.step 'encode', '/video/encode', { ... }
@@ -208,17 +208,17 @@ for recurring encoding tasks. In order to use these do the following:
208208
require 'transloadit'
209209

210210
transloadit = Transloadit.new(
211-
:key => 'YOUR_TRANSLOADIT_KEY',
212-
:secret => 'YOUR_TRANSLOADIT_SECRET'
211+
:key => 'MY_TRANSLOADIT_KEY',
212+
:secret => 'MY_TRANSLOADIT_SECRET'
213213
)
214214

215215
transloadit.assembly(
216-
:template_id => 'YOUR_TEMPLATE_ID'
216+
:template_id => 'MY_TEMPLATE_ID'
217217
).create! open('/PATH/TO/FILE.mpg')
218218
```
219219

220220
You can use your steps together with this template and even use variables.
221-
The [Transloadit documentation](https://transloadit.com/docs/#passing-variables-into-a-template)
221+
The [Transloadit documentation](https://transloadit.com/docs/#passing-variables-into-a-template)
222222
has some nice examples for that.
223223

224224
### 5. Using fields
@@ -231,8 +231,8 @@ to the upload itself. You can use fields like the following:
231231
require 'transloadit'
232232

233233
transloadit = Transloadit.new(
234-
:key => 'YOUR_TRANSLOADIT_KEY',
235-
:secret => 'YOUR_TRANSLOADIT_SECRET'
234+
:key => 'MY_TRANSLOADIT_KEY',
235+
:secret => 'MY_TRANSLOADIT_SECRET'
236236
)
237237

238238
transloadit.assembly(
@@ -252,8 +252,8 @@ a Notify URL for the <dfn>Assembly</dfn>.
252252
require 'transloadit'
253253

254254
transloadit = Transloadit.new(
255-
:key => 'YOUR_TRANSLOADIT_KEY',
256-
:secret => 'YOUR_TRANSLOADIT_SECRET'
255+
:key => 'MY_TRANSLOADIT_KEY',
256+
:secret => 'MY_TRANSLOADIT_SECRET'
257257
)
258258

259259
transloadit.assembly(
@@ -271,8 +271,8 @@ Transloadit also provides methods to retrieve/replay <dfn>Assemblies</dfn> and t
271271
require 'transloadit'
272272

273273
transloadit = Transloadit.new(
274-
:key => 'YOUR_TRANSLOADIT_KEY',
275-
:secret => 'YOUR_TRANSLOADIT_SECRET'
274+
:key => 'MY_TRANSLOADIT_KEY',
275+
:secret => 'MY_TRANSLOADIT_SECRET'
276276
)
277277

278278
assembly = transloadit.assembly
@@ -281,18 +281,18 @@ assembly = transloadit.assembly
281281
assembly.list
282282

283283
# returns a specific assembly
284-
assembly.get 'YOUR_ASSEMBLY_ID'
284+
assembly.get 'MY_ASSEMBLY_ID'
285285

286286
# replays a specific assembly
287-
response = assembly.replay 'YOUR_ASSEMBLY_ID'
287+
response = assembly.replay 'MY_ASSEMBLY_ID'
288288
# should return true if assembly is replaying and false otherwise.
289289
response.replaying?
290290

291291
# returns all assembly notifications
292292
assembly.get_notifications
293293

294294
# replays an assembly notification
295-
assembly.replay_notification 'YOUR_ASSEMBLY_ID'
295+
assembly.replay_notification 'MY_ASSEMBLY_ID'
296296
```
297297

298298
### 8. Templates
@@ -304,8 +304,8 @@ for recurring encoding tasks. Here's how you would create a <dfn>Template</dfn>:
304304
require 'transloadit'
305305

306306
transloadit = Transloadit.new(
307-
:key => 'YOUR_TRANSLOADIT_KEY',
308-
:secret => 'YOUR_TRANSLOADIT_SECRET'
307+
:key => 'MY_TRANSLOADIT_KEY',
308+
:secret => 'MY_TRANSLOADIT_SECRET'
309309
)
310310

311311
template = transloadit.template
@@ -331,8 +331,8 @@ There are also some other methods to retrieve, update and delete a <dfn>Template
331331
require 'transloadit'
332332

333333
transloadit = Transloadit.new(
334-
:key => 'YOUR_TRANSLOADIT_KEY',
335-
:secret => 'YOUR_TRANSLOADIT_SECRET'
334+
:key => 'MY_TRANSLOADIT_KEY',
335+
:secret => 'MY_TRANSLOADIT_SECRET'
336336
)
337337

338338
template = transloadit.template
@@ -341,11 +341,11 @@ template = transloadit.template
341341
template.list
342342

343343
# returns a specific template.
344-
template.get 'YOUR_TEMPLATE_ID'
344+
template.get 'MY_TEMPLATE_ID'
345345

346346
# updates the template whose id is specified.
347347
template.update(
348-
'YOUR_TEMPLATE_ID',
348+
'MY_TEMPLATE_ID',
349349
:name => 'CHANGED_TEMPLATE_NAME',
350350
:template => {
351351
:steps => {
@@ -358,7 +358,7 @@ template.update(
358358
)
359359

360360
# deletes a specific template
361-
template.delete 'YOUR_TEMPLATE_ID'
361+
template.delete 'MY_TEMPLATE_ID'
362362
```
363363

364364
### 9. Getting Bill reports
@@ -370,8 +370,8 @@ you can use the `bill` method passing the required month and year like the follo
370370
require 'transloadit'
371371

372372
transloadit = Transloadit.new(
373-
:key => 'YOUR_TRANSLOADIT_KEY',
374-
:secret => 'YOUR_TRANSLOADIT_SECRET'
373+
:key => 'MY_TRANSLOADIT_KEY',
374+
:secret => 'MY_TRANSLOADIT_SECRET'
375375
)
376376

377377
# returns bill report for February, 2016.
@@ -380,7 +380,48 @@ transloadit.bill(2, 2016)
380380

381381
Not specifying the `month` or `year` would default to the current month or year.
382382

383-
### 10. Rate limits
383+
### 10. Signing Smart CDN URLs
384+
385+
You can generate signed [Smart CDN](https://transloadit.com/services/content-delivery/) URLs using your Transloadit instance:
386+
387+
```ruby
388+
require 'transloadit'
389+
390+
transloadit = Transloadit.new(
391+
:key => 'MY_TRANSLOADIT_KEY',
392+
:secret => 'MY_TRANSLOADIT_SECRET'
393+
)
394+
395+
# Generate a signed URL using instance credentials
396+
url = transloadit.signed_smart_cdn_url(
397+
workspace: "MY_WORKSPACE",
398+
template: "MY_TEMPLATE",
399+
input: "avatars/jane.jpg"
400+
)
401+
402+
# Add URL parameters
403+
url = transloadit.signed_smart_cdn_url(
404+
workspace: "MY_WORKSPACE",
405+
template: "MY_TEMPLATE",
406+
input: "avatars/jane.jpg",
407+
url_params: {
408+
width: 100,
409+
height: 200
410+
}
411+
)
412+
413+
# Set expiration time
414+
url = transloadit.signed_smart_cdn_url(
415+
workspace: "MY_WORKSPACE",
416+
template: "MY_TEMPLATE",
417+
input: "avatars/jane.jpg",
418+
expire_at_ms: 1732550672867 # Specific timestamp
419+
)
420+
```
421+
422+
The generated URL will be signed using your Transloadit credentials and can be used to access files through the Smart CDN in a secure manner.
423+
424+
### 11. Rate limits
384425

385426
Transloadit enforces rate limits to guarantee that no customers are adversely affected by the usage
386427
of any given customer. See [Rate Limiting](https://transloadit.com/docs/api/#rate-limiting).
@@ -393,8 +434,8 @@ To change the number of attempts that will be made when creating an <dfn>Assembl
393434
require 'transloadit'
394435

395436
transloadit = Transloadit.new(
396-
:key => 'YOUR_TRANSLOADIT_KEY',
397-
:secret => 'YOUR_TRANSLOADIT_SECRET'
437+
:key => 'MY_TRANSLOADIT_KEY',
438+
:secret => 'MY_TRANSLOADIT_SECRET'
398439
)
399440

400441
# would make one extra attempt after a failed attempt.
@@ -423,3 +464,24 @@ Please see [ci.yml](https://github.com/transloadit/ruby-sdk/tree/main/.github/wo
423464
### Ruby 2.x
424465

425466
If you still need support for Ruby 2.x, 2.0.1 is the last version that supports it.
467+
468+
## Contributing
469+
470+
### Running tests
471+
472+
```bash
473+
bundle install
474+
bundle exec rake test
475+
```
476+
477+
To also test parity against the Node.js reference implementation, run:
478+
479+
```bash
480+
TEST_NODE_PARITY=1 bundle exec rake test
481+
```
482+
483+
To disable coverage reporting, run:
484+
485+
```bash
486+
COVERAGE=0 bundle exec rake test
487+
```

0 commit comments

Comments
 (0)