The Ruby wrapper allows you to seamlessly interface with the Cloudimage API.
Add this line to your application's Gemfile:
And then execute:
$ bundle install
Or install it yourself as:
$ gem install cloudimage
The only requirement to get started is your customer token. You can find it within your Admin interface.
In order to interact with Cloudimage, we'll first initialize a client service object:
client = Cloudimage::Client.new(token: 'mysecrettoken')
The Cloudimage client accepts the following options:
The calling path on the client object returns an instance of Cloudimage::URI. It accepts the path to the image as a string and we we will use it to build Cloudimage URLs.
uri = client.path('/assets/image.png')
Here are some common approaches for constructing Cloudimage URLs using this gem:
Hash of params
Pass a hash to
to_url. Every key becomes a param in the final Cloudimage URL so this gives you the freedom to pass arbitrary params if need be.
uri.to_url(w: 200, h: 400, sharp: 1, gravity: 'west', ci_info: 1)
# => "https://mysecrettoken.cloudimg.io/assets/image.png?ci_info=1&gravity=west&h=400&sharp=1&w=200"
Every param supported by Cloudimage can be used as a helper method.
# => "https://mysecrettoken.cloudimg.io/assets/image.png?gravity=west&h=400&w=200"
While every key passed into the
to_urlmethod gets appended to the URL, chainable helper methods will throw a
NoMethodErrorwhen using an unsupported method.
# NoMethodError (undefined method `height' for #<Cloudimage::URI:0x00007fae461c42a0>)
This is useful for catching typos and identifying deprecated methods in case Cloudimage's API changes.
The gem comes with a handful of useful aliases. Consult the Cloudimage::Params module for their full list.
# => "https://mysecrettoken.cloudimg.io/assets/image.png?ci_info=1&org_if_sml=1"
From the example above you can see that parameters that only serve as a flag don't need to accept arguments and will be translated into
param=1within the final URL.