Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable API version URL component #25

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Supports Ruby `2.4` and above, `JRuby`, and `TruffleRuby`.
- [Custom helpers](#custom-helpers)
- [URL aliases](#url-aliases)
- [CNAME](#cname)
- [Optional API version](#optional-api-version)
- [Security](#security)
- [URL signature](#url-signature)
- [URL sealing](#url-sealing)
Expand Down Expand Up @@ -57,15 +58,16 @@ client = Cloudimage::Client.new(token: 'mysecrettoken')

Cloudimage client accepts the following options:

| Option | Type | Additional info |
| ------------------ | ------- | ------------------------------------------------------------- |
| `token` | string | Required if `cname` is missing. |
| `cname` | string | Required if `token` is missing. See [CNAME](#cname). |
| `salt` | string | Optional. See [Security](#security). |
| `signature_length` | integer | Optional. Integer value in the range `6..40`. Defaults to 18. |
| `sign_urls` | boolean | Optional. Defaults to `true`. See [Security](#security). |
| `aliases` | hash | Optional. See [URL aliases](#url-aliases). |
| `api_key` | string | Optional. See [Invalidation API](#invalidation-api). |
| Option | Type | Additional info |
| --------------------- | ------- | ------------------------------------------------------------------------------ |
| `token` | string | Required if `cname` is missing. |
| `cname` | string | Required if `token` is missing. See [CNAME](#cname). |
| `salt` | string | Optional. See [Security](#security). |
| `signature_length` | integer | Optional. Integer value in the range `6..40`. Defaults to 18. |
| `sign_urls` | boolean | Optional. Defaults to `true`. See [Security](#security). |
| `aliases` | hash | Optional. See [URL aliases](#url-aliases). |
| `api_key` | string | Optional. See [Invalidation API](#invalidation-api). |
| `include_api_version` | boolean | Optional. Defaults to true. See [Optional API version](#optional-api-version). |

Calling `path` on the client object returns an instance of `Cloudimage::URI`.
It accepts path to the image as a string and we we will use it to build
Expand Down Expand Up @@ -160,6 +162,17 @@ client.path('/assets/image.jpg').to_url
# => 'https://img.klimo.io/v7/assets/image.jpg'
```

### Optional API version

If your account is configured to work without the API version component in the URL,
you can configure client not to include it in the generated URL:

```ruby
client = Cloudimage::Client.new(cname: 'img.klimo.io', include_api_version: false)
client.path('/assets/image.jpg').to_url
# => "https://img.klimo.io/assets/image.jpg"
```

### Security

#### URL signature
Expand Down
2 changes: 2 additions & 0 deletions lib/cloudimage/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def set_config_defaults(options)
config[:signature_length] =
options[:signature_length] || DEFAULT_SIGNATURE_LENGTH
config[:api_version] = API_VERSION
config[:include_api_version] =
options[:include_api_version].nil? ? true : false
config[:sign_urls] = options[:sign_urls].nil? ? true : false
config[:aliases] = options[:aliases] || {}
end
Expand Down
6 changes: 5 additions & 1 deletion lib/cloudimage/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ def request_uri
end

def build_uri
Addressable::URI.parse(site + api_version + path)
if config[:include_api_version]
Addressable::URI.parse(site + api_version + path)
else
Addressable::URI.parse(site + path)
end
end

def set_uri_params(**extra_params)
Expand Down
8 changes: 8 additions & 0 deletions spec/cloudimage/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@
expect(client.path(path).to_url).to eq base + path
end

it 'allows for optional API version inclusion' do
client = Cloudimage::Client.new(cname: 'img.klimo.io',
include_api_version: false)
base = 'https://img.klimo.io'
path = '/assets/image.jpg'
expect(client.path(path).to_url).to eq base + path
end

context 'custom helpers' do
describe 'positionable_crop' do
it 'returns image URL with params encoded' do
Expand Down