net/http and json from the standard library.
This page puts that behind a service object, a rake task and one controller
action.
Add SudoMock rendering to a Ruby on Rails app.
Prerequisites
- An API key. Create one and keep the
sm_value. It travels in thex-api-keyheader. - A Rails app with encrypted credentials in use, or
SUDOMOCK_API_KEYset in the environment. - A PSD holding at least one visible smart object, reachable at a public URL. Preparing a PSD covers what the file needs.
- Artwork served over HTTPS, or held in memory as base64.
Guide
1
Store the key
Credentials keep the key out of the repository and out of the log.Confirm the key is live before you write a line of client code:
2
Add a client
One small service object carries the header, the timeout and the failure.
Everything below calls it.A failed call still returns a JSON body, and
app/services/sudomock.rb
error_code is what you
branch on. Errors lists the values and says which ones are worth
retrying.3
Upload the template once
Uploading registers the PSD and names every slot inside it. This belongs in
a rake task, not in a request.Keep the printed values. The mockup UUID and the smart object UUID stay
valid for every later render, so they belong in credentials, in the
environment, or on the product row they describe.
lib/tasks/sudomock.rake
A product photo can stand in for a PSD. Create it with
POST /api/v1/photo-mockups and render it with
POST /api/v1/photo-mockups/{mockup_id}/render. See
Photo mockups.4
Render from a controller
The render call takes the two UUIDs and the artwork, and answers with the
finished file.One render answers with one image, so
config/routes.rb
app/controllers/mockups_controller.rb
print_files carries a single
entry whatever number of layers the request filled:fit decides how the artwork meets the slot, and
Fit and blend modes shows each result. A
long render does not have to hold the connection open: set is_async to
true, read job_id from the 202, and follow it from an Active Job
worker against the jobs endpoint.Next steps
Webhooks
Get a signed callback when an asynchronous render finishes, instead of
polling for it.
Upload a PSD
Every field the upload accepts, and everything the response names.