Skip to content

Commit

Permalink
Add Smart CDN Signature Auth helper (#71)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
kvz and rmehner authored Nov 28, 2024
1 parent ca7df0f commit a3d8049
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 50 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: CI
on:
push:
branches: [ main ]
branches:
- main
pull_request:
branches: [ main ]
types:
- opened
- synchronize
jobs:
ci:
runs-on: ubuntu-latest
Expand All @@ -13,15 +16,19 @@ jobs:
- 3.0
- 3.1
- 3.2
- 3.3
- jruby
# - jruby-head
- truffleruby
# - truffleruby-head
fail-fast: false
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install -g tsx
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle exec standardrb
- run: COVERAGE=false bundle exec rake test
- run: COVERAGE=0 TEST_NODE_PARITY=1 bundle exec rake test
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ transloadit-*.gem

.DS_Store
env.sh

# Bundle directory
.env
vendor/bundle/
13 changes: 13 additions & 0 deletions .vscode/ruby-sdk.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"folders": [
{
"path": ".."
}
],
"settings": {
"workbench.colorCustomizations": {
"titleBar.activeForeground": "#ffffff",
"titleBar.activeBackground": "#cc0000"
},
}
}
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.PHONY: all test fix

all: fix test

# Run tests
test:
bundle exec rake test

# Fix code formatting
fix:
bundle exec standardrb --fix

# Install dependencies
install:
bundle install

# Run both fix and test
check: fix test
144 changes: 103 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A **Ruby** Integration for [Transloadit](https://transloadit.com)'s file uploadi

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

*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).*
_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)._

## Install

Expand All @@ -29,14 +29,14 @@ $ irb -rubygems
=> true
```

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

```ruby
transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)
```

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

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

store = transloadit.step 'store', '/s3/store',
:key => 'YOUR_AWS_KEY',
:secret => 'YOUR_AWS_SECRET',
:bucket => 'YOUR_S3_BUCKET'
:key => 'MY_AWS_KEY',
:secret => 'MY_AWS_SECRET',
:bucket => 'MY_S3_BUCKET'

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

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

assembly = transloadit.assembly(steps: store)
Expand All @@ -160,7 +160,7 @@ response = assembly.create!(
)
```

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

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

transloadit.assembly(
:template_id => 'YOUR_TEMPLATE_ID'
:template_id => 'MY_TEMPLATE_ID'
).create! open('/PATH/TO/FILE.mpg')
```

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

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

assembly = transloadit.assembly
Expand All @@ -281,18 +281,18 @@ assembly = transloadit.assembly
assembly.list

# returns a specific assembly
assembly.get 'YOUR_ASSEMBLY_ID'
assembly.get 'MY_ASSEMBLY_ID'

# replays a specific assembly
response = assembly.replay 'YOUR_ASSEMBLY_ID'
response = assembly.replay 'MY_ASSEMBLY_ID'
# should return true if assembly is replaying and false otherwise.
response.replaying?

# returns all assembly notifications
assembly.get_notifications

# replays an assembly notification
assembly.replay_notification 'YOUR_ASSEMBLY_ID'
assembly.replay_notification 'MY_ASSEMBLY_ID'
```

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

template = transloadit.template
Expand All @@ -341,11 +341,11 @@ template = transloadit.template
template.list

# returns a specific template.
template.get 'YOUR_TEMPLATE_ID'
template.get 'MY_TEMPLATE_ID'

# updates the template whose id is specified.
template.update(
'YOUR_TEMPLATE_ID',
'MY_TEMPLATE_ID',
:name => 'CHANGED_TEMPLATE_NAME',
:template => {
:steps => {
Expand All @@ -358,7 +358,7 @@ template.update(
)

# deletes a specific template
template.delete 'YOUR_TEMPLATE_ID'
template.delete 'MY_TEMPLATE_ID'
```

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

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

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

### 10. Rate limits
### 10. Signing Smart CDN URLs

You can generate signed [Smart CDN](https://transloadit.com/services/content-delivery/) URLs using your Transloadit instance:

```ruby
require 'transloadit'

transloadit = Transloadit.new(
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

# Generate a signed URL using instance credentials
url = transloadit.signed_smart_cdn_url(
workspace: "MY_WORKSPACE",
template: "MY_TEMPLATE",
input: "avatars/jane.jpg"
)

# Add URL parameters
url = transloadit.signed_smart_cdn_url(
workspace: "MY_WORKSPACE",
template: "MY_TEMPLATE",
input: "avatars/jane.jpg",
url_params: {
width: 100,
height: 200
}
)

# Set expiration time
url = transloadit.signed_smart_cdn_url(
workspace: "MY_WORKSPACE",
template: "MY_TEMPLATE",
input: "avatars/jane.jpg",
expire_at_ms: 1732550672867 # Specific timestamp
)
```

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.

### 11. Rate limits

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

transloadit = Transloadit.new(
:key => 'YOUR_TRANSLOADIT_KEY',
:secret => 'YOUR_TRANSLOADIT_SECRET'
:key => 'MY_TRANSLOADIT_KEY',
:secret => 'MY_TRANSLOADIT_SECRET'
)

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

If you still need support for Ruby 2.x, 2.0.1 is the last version that supports it.

## Contributing

### Running tests

```bash
bundle install
bundle exec rake test
```

To also test parity against the Node.js reference implementation, run:

```bash
TEST_NODE_PARITY=1 bundle exec rake test
```

To disable coverage reporting, run:

```bash
COVERAGE=0 bundle exec rake test
```
Loading

0 comments on commit a3d8049

Please sign in to comment.