imgix 3.2.1 → 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +2 -0
- data/.travis.yml +16 -2
- data/CHANGELOG.md +29 -0
- data/Gemfile +3 -1
- data/README.md +48 -45
- data/Rakefile +2 -0
- data/imgix.gemspec +19 -22
- data/lib/imgix.rb +12 -10
- data/lib/imgix/client.rb +72 -26
- data/lib/imgix/path.rb +117 -91
- data/lib/imgix/version.rb +1 -1
- metadata +12 -53
- data/lib/imgix/param_helpers.rb +0 -18
- data/test/test_helper.rb +0 -13
- data/test/units/domains_test.rb +0 -17
- data/test/units/path_test.rb +0 -132
- data/test/units/purge_test.rb +0 -25
- data/test/units/srcset_test.rb +0 -716
- data/test/units/url_test.rb +0 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d45f1a9130411b2b890ad779f5e67ffd506958d0c939d79e3eac2e7a1ff3a7c0
|
4
|
+
data.tar.gz: 9a1a729003bdb13dd0b390af5afe60b16e95691aa49f483fa6b37c1c0c27332f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 470a6ff91572f79a5d93d2040c5ce50d035e379739aa207e2af4b3a45d0c510042276d42c73839bbc154fc4d4112d913005cccfecdcee930ed36098a06d36055
|
7
|
+
data.tar.gz: 390c08cee470af444bc95140758fe39e108270df0e2ca6d66ac2b3e42c59665c7786ff99965652758a246b19a6486f12e19185528c8dad3c8bcc2fca5a93cd18
|
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
@@ -2,8 +2,22 @@ dist: trusty
|
|
2
2
|
language: ruby
|
3
3
|
bundler_args: --without development
|
4
4
|
rvm:
|
5
|
+
- 2.7.0
|
6
|
+
- 2.6.3
|
5
7
|
- 2.3.0
|
6
8
|
- 2.2.4
|
7
9
|
- 2.1.8
|
8
|
-
- jruby-9.
|
9
|
-
|
10
|
+
- jruby-9.2.11.0
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
# Use `fast_finish`, don't wait for any allowed failures.
|
14
|
+
fast_finish: true
|
15
|
+
|
16
|
+
include:
|
17
|
+
- name: Rubinius
|
18
|
+
rvm: rbx-3.107
|
19
|
+
dist: trusty
|
20
|
+
|
21
|
+
allow_failures:
|
22
|
+
- name: Rubinius
|
23
|
+
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,35 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
5
5
|
|
6
|
+
## [4.0.1](https://github.com/imgix/imgix-rb/compare/4.0.0...4.0.1) - March 24, 2021
|
7
|
+
|
8
|
+
* fix: dpr srcset when only h param ([#101](https://github.com/imgix/imgix-rb/pull/101))
|
9
|
+
* docs: update travis badge to point to travis-ci.com ([#99](https://github.com/imgix/imgix-rb/pull/99))
|
10
|
+
* docs: fixed typo w/ srcset min/max widths ([#97](https://github.com/imgix/imgix-rb/pull/97))
|
11
|
+
|
12
|
+
## [4.0.0](https://github.com/imgix/imgix-rb/compare/3.4.0...4.0.0) - August 26, 2020
|
13
|
+
|
14
|
+
* deprecate: host-usage, param_helpers, and prefix-1-ary ([#91](https://github.com/imgix/imgix-rb/pull/91))
|
15
|
+
* fix: update purge operation api keys ([#93](https://github.com/imgix/imgix-rb/pull/93))
|
16
|
+
|
17
|
+
## [3.4.0](https://github.com/imgix/imgix-rb/compare/3.3.1...3.4.0) - August 12, 2020
|
18
|
+
|
19
|
+
* fix: deprecate api key versions ([#87](https://github.com/imgix/imgix-rb/pull/87))
|
20
|
+
* docs(readme): deprecation notice for pre-4.0 api keys ([#88](https://github.com/imgix/imgix-rb/pull/88))
|
21
|
+
|
22
|
+
## [3.3.1](https://github.com/imgix/imgix-rb/compare/3.3.0...3.3.1) - July 27, 2020
|
23
|
+
|
24
|
+
* fix: ensure host is initialized ([#82](https://github.com/imgix/imgix-rb/pull/82))
|
25
|
+
|
26
|
+
## [3.3.0](https://github.com/imgix/imgix-rb/compare/3.2.1...3.3.0) - July 21, 2020
|
27
|
+
|
28
|
+
* fix: warn prefix-with-args is deprecated ([#78](https://github.com/imgix/imgix-rb/pull/78))
|
29
|
+
* fix: warn host has been deprecated in favor of domain ([#76](https://github.com/imgix/imgix-rb/pull/76))
|
30
|
+
* fix: deprecate ALIASES and corresponding define_method(s) ([#75](https://github.com/imgix/imgix-rb/pull/75))
|
31
|
+
* fix: deprecate ParamHelpers.rect ([#74](https://github.com/imgix/imgix-rb/pull/74))
|
32
|
+
* fix: allow rbx-3.107 to fail (travis-config) ([#70](https://github.com/imgix/imgix-rb/pull/70))
|
33
|
+
* fix: remove ensure even requirement ([#69](https://github.com/imgix/imgix-rb/pull/69))
|
34
|
+
|
6
35
|
## [3.2.1](https://github.com/imgix/imgix-rb/compare/3.2.0...3.2.1) - November 15, 2019
|
7
36
|
|
8
37
|
* fix: duplicate entries when using `min_width` or `max_width` ([#59](https://github.com/imgix/imgix-rb/pull/59))
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,30 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
<!-- ix-docs-ignore -->
|
2
|
+
![imgix logo](https://assets.imgix.net/sdk-imgix-logo.svg)
|
3
|
+
|
4
|
+
`imgix-rb` is a client library for generating image URLs with [imgix](https://www.imgix.com/). It is tested under Ruby versions `2.3.0`, `2.2.4`, `2.1.8`, `jruby-9.2.11.0`, and `rbx-3.107`.
|
5
|
+
|
6
|
+
[![Gem Version](https://img.shields.io/gem/v/imgix.svg)](https://rubygems.org/gems/imgix)
|
7
|
+
[![Build Status](https://travis-ci.com/imgix/imgix-rb.svg?branch=main)](https://travis-ci.com/imgix/imgix-rb)
|
8
|
+
![Downloads](https://img.shields.io/gem/dt/imgix)
|
9
|
+
[![License](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://github.com/imgix/imgix-rb/blob/main/LICENSE)
|
10
|
+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fimgix%2Fimgix-rb.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fimgix%2Fimgix-rb?ref=badge_shield)
|
11
|
+
|
12
|
+
---
|
13
|
+
<!-- /ix-docs-ignore -->
|
14
|
+
|
15
|
+
- [Installation](#installation)
|
16
|
+
- [Usage](#usage)
|
17
|
+
- [Srcset Generation](#srcset-generation)
|
18
|
+
* [Fixed image rendering](#fixed-image-rendering)
|
19
|
+
* [Custom Widths](#custom-widths)
|
20
|
+
* [Width Tolerance](#width-tolerance)
|
21
|
+
* [Minimum and Maximum Width Ranges](#minimum-and-maximum-width-ranges)
|
22
|
+
* [Variable Qualities](#variable-qualities)
|
23
|
+
- [Purge Cache](#purge-cache)
|
24
|
+
- [URL encoding and signed imgix URLs](#url-encoding-and-signed-imgix-urls)
|
25
|
+
- [What is the `ixlib` param on every request?](#what-is-the-ixlib-param-on-every-request)
|
26
|
+
- [Contributing](#contributing)
|
27
|
+
- [License](#license)
|
6
28
|
|
7
29
|
## Installation
|
8
30
|
|
@@ -23,27 +45,15 @@ Or install it yourself as:
|
|
23
45
|
|
24
46
|
## Usage
|
25
47
|
|
26
|
-
Initialize a client with a `:
|
48
|
+
Initialize a client with a `:domain` and your `:secure_url_token`. By default, HTTPS URLs are generated, but you can toggle that by passing `use_https: false`.
|
27
49
|
|
28
50
|
Call `Imgix::Client#path` with the resource path to get an `Imgix::Path` object back. You can then manipulate the path parameters, and call `Imgix#Path#to_url` when you're done.
|
29
51
|
|
30
|
-
```
|
31
|
-
client = Imgix::Client.new(
|
52
|
+
```rb
|
53
|
+
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token')
|
32
54
|
|
33
55
|
client.path('/images/demo.png').to_url(w: 200)
|
34
56
|
#=> https://your-subdomain.imgix.net/images/demo.png?w=200&s=2eadddacaa9bba4b88900d245f03f51e
|
35
|
-
|
36
|
-
# OR
|
37
|
-
path = client.path('/images/demo.png')
|
38
|
-
path.width = 200
|
39
|
-
path.to_url
|
40
|
-
|
41
|
-
# OR
|
42
|
-
client.path('/images/demo.png').width(200).height(300).to_url
|
43
|
-
|
44
|
-
# Some other tricks
|
45
|
-
path.defaults.width(300).to_url # Resets parameters
|
46
|
-
path.rect(x: 0, y: 50, width: 200, height: 300).to_url # Rect helper
|
47
57
|
```
|
48
58
|
|
49
59
|
## Srcset Generation
|
@@ -51,7 +61,7 @@ path.rect(x: 0, y: 50, width: 200, height: 300).to_url # Rect helper
|
|
51
61
|
The imgix gem allows for generation of custom `srcset` attributes, which can be invoked through `Imgix::Path#to_srcset`. By default, the `srcset` generated will allow for responsive size switching by building a list of image-width mappings.
|
52
62
|
|
53
63
|
```rb
|
54
|
-
client = Imgix::Client.new(
|
64
|
+
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token', include_library_param: false)
|
55
65
|
path = client.path('/images/demo.png')
|
56
66
|
|
57
67
|
srcset = path.to_srcset
|
@@ -70,10 +80,10 @@ https://your-subdomain.imgix.net/images/demo.png?w=8192&s=9fbd257c53e770e345ce34
|
|
70
80
|
|
71
81
|
### Fixed image rendering
|
72
82
|
|
73
|
-
In cases where enough information is provided about an image's dimensions, `to_srcset` will instead build a `srcset` that will allow for an image to be served at different resolutions. The parameters taken into consideration when determining if an image is fixed-width are `w
|
83
|
+
In cases where enough information is provided about an image's dimensions, `to_srcset` will instead build a `srcset` that will allow for an image to be served at different resolutions. The parameters taken into consideration when determining if an image is fixed-width are `w` or `h`. By invoking `to_srcset` with either a width **or** height provided, a different `srcset` will be generated for a fixed-size image instead.
|
74
84
|
|
75
85
|
```rb
|
76
|
-
client = Imgix::Client.new(
|
86
|
+
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token', include_library_param: false)
|
77
87
|
path = client.path('/images/demo.png')
|
78
88
|
|
79
89
|
srcset = path.to_srcset(h:800, ar:'3:2', fit:'crop')
|
@@ -96,7 +106,7 @@ For more information to better understand `srcset`, we highly recommend [Eric Po
|
|
96
106
|
In situations where specific widths are desired when generating `srcset` pairs, a user can specify them by passing an array of integers via `widths` to the `options` keyword argument.
|
97
107
|
|
98
108
|
```rb
|
99
|
-
@client ||= Imgix::Client.new(
|
109
|
+
@client ||= Imgix::Client.new(domain: 'testing.imgix.net', include_library_param: false)
|
100
110
|
.path('image.jpg')
|
101
111
|
.to_srcset(options: { widths: [100, 500, 1000, 1800] })
|
102
112
|
```
|
@@ -119,7 +129,7 @@ The `srcset` width tolerance dictates the maximum tolerated size difference betw
|
|
119
129
|
By default this rate is set to 8 percent, which we consider to be the ideal rate for maximizing cache hits without sacrificing visual quality. Users can specify their own width tolerance by passing a positive numeric value to `width_tolerance` within the `options` keyword argument:
|
120
130
|
|
121
131
|
```rb
|
122
|
-
client = Imgix::Client.new(
|
132
|
+
client = Imgix::Client.new(domain: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false)
|
123
133
|
client.path('image.jpg').to_srcset(options: { width_tolerance: 0.20 })
|
124
134
|
```
|
125
135
|
|
@@ -135,11 +145,11 @@ https://testing.imgix.net/image.jpg?w=8192 8192w
|
|
135
145
|
|
136
146
|
### Minimum and Maximum Width Ranges
|
137
147
|
|
138
|
-
If the exact number of minimum/maximum physical pixels that an image will need to be rendered at is known, a user can specify them by passing an integer via `
|
148
|
+
If the exact number of minimum/maximum physical pixels that an image will need to be rendered at is known, a user can specify them by passing an integer via `min_width` and/or `max_width` to the `options` keyword parameters:
|
139
149
|
|
140
150
|
```rb
|
141
|
-
client = Imgix::Client.new(
|
142
|
-
client.path('image.jpg').to_srcset(options: {
|
151
|
+
client = Imgix::Client.new(domain: 'testing.imgix.net', include_library_param: false)
|
152
|
+
client.path('image.jpg').to_srcset(options: { min_width: 500, max_width: 2000 })
|
143
153
|
```
|
144
154
|
|
145
155
|
Will result in a smaller, more tailored srcset.
|
@@ -158,7 +168,7 @@ https://testing.imgix.net/image.jpg?w=1902 1902w,
|
|
158
168
|
https://testing.imgix.net/image.jpg?w=2000 2000w
|
159
169
|
```
|
160
170
|
|
161
|
-
Remember that browsers will apply a device pixel ratio as a multiplier when selecting which image to download from a `srcset`. For example, even if you know your image will render no larger than 1000px, specifying `options: {
|
171
|
+
Remember that browsers will apply a device pixel ratio as a multiplier when selecting which image to download from a `srcset`. For example, even if you know your image will render no larger than 1000px, specifying `options: { max_width: 1000 }` will give your users with DPR higher than 1 no choice but to download and render a low-resolution version of the image. Therefore, it is vital to factor in any potential differences when choosing a minimum or maximum range.
|
162
172
|
|
163
173
|
Also please note that according to the [imgix API](https://docs.imgix.com/apis/url/size/w), the maximum renderable image width is 8192 pixels.
|
164
174
|
|
@@ -171,7 +181,7 @@ This behavior will respect any overriding `q` value passed in as a parameter. Ad
|
|
171
181
|
This behavior specifically occurs when a [fixed-size image](https://github.com/imgix/imgix-rb#fixed-image-rendering) is rendered, for example:
|
172
182
|
|
173
183
|
```rb
|
174
|
-
srcset = Imgix::Client.new(
|
184
|
+
srcset = Imgix::Client.new(domain: 'testing.imgix.net', include_library_param: false)
|
175
185
|
.path('image.jpg')
|
176
186
|
.to_srcset(w:100)
|
177
187
|
```
|
@@ -186,30 +196,20 @@ https://testing.imgix.net/image.jpg?w=100&dpr=4&q=23 4x,
|
|
186
196
|
https://testing.imgix.net/image.jpg?w=100&dpr=5&q=20 5x
|
187
197
|
```
|
188
198
|
|
189
|
-
## Multiple Parameters
|
190
|
-
|
191
|
-
When the imgix api requires multiple parameters you have to use the method rather than an accessor.
|
192
|
-
|
193
|
-
For example to use the noise reduction:
|
194
|
-
|
195
|
-
``` ruby
|
196
|
-
path.noise_reduction(50,50)
|
197
|
-
```
|
198
|
-
|
199
199
|
## Purge Cache
|
200
200
|
|
201
|
-
If you need to remove or update an image on imgix, you can purge it from our cache by initializing a client with your
|
201
|
+
If you need to remove or update an image on imgix, you can purge it from our cache by initializing a client with your [API key](http://dashboard.imgix.com/api-keys), then calling `Imgix::Client#purge` with the resource path.
|
202
202
|
|
203
203
|
```ruby
|
204
|
-
client = Imgix::Client.new(
|
204
|
+
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', api_key: 'your-key')
|
205
205
|
client.purge('/images/demo.png')
|
206
206
|
```
|
207
207
|
|
208
208
|
To learn more about purging assets with imgix, [see our docs](https://docs.imgix.com/setup/purging-images).
|
209
209
|
|
210
|
-
## URL encoding and signed
|
210
|
+
## URL encoding and signed imgix URLs
|
211
211
|
|
212
|
-
Some important third parties (like Facebook) apply URL escaping to query string components, which can cause correctly signed
|
212
|
+
Some important third parties (like Facebook) apply URL escaping to query string components, which can cause correctly signed imgix URLs to to be transformed into incorrectly signed ones. We URL encode the query part of the URL before signing, so you don't have to worry about this.
|
213
213
|
|
214
214
|
## What is the `ixlib` param on every request?
|
215
215
|
|
@@ -218,9 +218,12 @@ For security and diagnostic purposes, we sign all requests with the language and
|
|
218
218
|
This can be disabled by including `include_library_param: false` in the instantiation Hash parameter for `Imgix::Client`:
|
219
219
|
|
220
220
|
```ruby
|
221
|
-
client = Imgix::Client.new(
|
221
|
+
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', include_library_param: false )
|
222
222
|
```
|
223
223
|
|
224
224
|
## Contributing
|
225
225
|
|
226
226
|
See the [contributing guide](Contributing.markdown).
|
227
|
+
|
228
|
+
## License
|
229
|
+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fimgix%2Fimgix-rb.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fimgix%2Fimgix-rb?ref=badge_large)
|
data/Rakefile
CHANGED
data/imgix.gemspec
CHANGED
@@ -1,31 +1,28 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
require 'imgix/version'
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/imgix/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
6
|
+
spec.name = "imgix"
|
8
7
|
spec.version = Imgix::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
8
|
+
spec.authors = ["Kelly Sutton", "Sam Soffes", "Ryan LeFevre", "Antony Denyer", "Paul Straw", "Sherwin Heydarbeygi"]
|
9
|
+
spec.email = ["kelly@imgix.com", "sam@soff.es", "ryan@layervault.com", "email@antonydenyer.co.uk", "paul@imgix.com", "sherwin@imgix.com"]
|
10
|
+
spec.description = "Easily create and sign imgix URLs."
|
11
|
+
spec.summary = "Official Ruby Gem for easily creating and signing imgix URLs."
|
12
|
+
spec.homepage = "https://github.com/imgix/imgix-rb"
|
13
|
+
spec.license = "MIT"
|
15
14
|
|
16
15
|
spec.metadata = {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
"bug_tracker_uri" => "https://github.com/imgix/imgix-rb/issues",
|
17
|
+
"changelog_uri" => "https://github.com/imgix/imgix-rb/blob/main/CHANGELOG.md",
|
18
|
+
"documentation_uri" => "https://www.rubydoc.info/gems/imgix/#{spec.version}",
|
19
|
+
"source_code_uri" => "https://github.com/imgix/imgix-rb/tree/#{spec.version}"
|
21
20
|
}
|
22
21
|
|
23
|
-
spec.files =
|
24
|
-
|
25
|
-
|
26
|
-
spec.require_paths = ['lib']
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|.github)/}) }
|
24
|
+
end
|
27
25
|
|
28
|
-
spec.
|
29
|
-
spec.
|
30
|
-
spec.add_development_dependency 'webmock'
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
spec.required_ruby_version = ">= 1.9.0"
|
31
28
|
end
|
data/lib/imgix.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "imgix/version"
|
4
|
+
require "imgix/client"
|
5
|
+
require "imgix/path"
|
6
6
|
|
7
7
|
module Imgix
|
8
8
|
# regex pattern used to determine if a domain is valid
|
9
|
-
DOMAIN_REGEX = /^(?:[a-z\d\-_]{1,62}\.){0,125}(?:[a-z\d](?:\-(?=\-*[a-z\d])|[a-z]|\d){0,62}\.)[a-z\d]{1,63}$/i
|
9
|
+
DOMAIN_REGEX = /^(?:[a-z\d\-_]{1,62}\.){0,125}(?:[a-z\d](?:\-(?=\-*[a-z\d])|[a-z]|\d){0,62}\.)[a-z\d]{1,63}$/i.freeze
|
10
10
|
|
11
11
|
# determines the growth rate when building out srcset pair widths
|
12
12
|
DEFAULT_WIDTH_TOLERANCE = 0.08
|
@@ -20,17 +20,19 @@ module Imgix
|
|
20
20
|
# returns an array of width values used during scrset generation
|
21
21
|
TARGET_WIDTHS = lambda { |tolerance, min, max|
|
22
22
|
increment_percentage = tolerance || DEFAULT_WIDTH_TOLERANCE
|
23
|
-
|
24
|
-
|
23
|
+
|
24
|
+
unless increment_percentage.is_a?(Numeric) && increment_percentage > 0
|
25
|
+
width_increment_error = "error: `width_tolerance` must be a positive `Numeric` value"
|
26
|
+
raise ArgumentError, width_increment_error
|
25
27
|
end
|
26
28
|
|
27
29
|
max_size = max || MAX_WIDTH
|
28
30
|
resolutions = []
|
29
31
|
prev = min || MIN_WIDTH
|
30
32
|
|
31
|
-
while
|
33
|
+
while prev < max_size
|
32
34
|
# ensures that each width is even
|
33
|
-
resolutions.push(
|
35
|
+
resolutions.push(prev.round)
|
34
36
|
prev *= 1 + (increment_percentage * 2)
|
35
37
|
end
|
36
38
|
|
@@ -40,10 +42,10 @@ module Imgix
|
|
40
42
|
|
41
43
|
# hash of default quality parameter values mapped by each dpr srcset entry
|
42
44
|
DPR_QUALITY = {
|
43
|
-
1 => 75,
|
45
|
+
1 => 75,
|
44
46
|
2 => 50,
|
45
47
|
3 => 35,
|
46
48
|
4 => 23,
|
47
49
|
5 => 20
|
48
|
-
}
|
50
|
+
}.freeze
|
49
51
|
end
|
data/lib/imgix/client.rb
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
3
|
+
require "digest"
|
4
|
+
require "net/http"
|
5
|
+
require "uri"
|
6
|
+
require "json"
|
7
7
|
|
8
8
|
module Imgix
|
9
9
|
class Client
|
10
|
-
DEFAULTS = { use_https: true }
|
10
|
+
DEFAULTS = { use_https: true }.freeze
|
11
11
|
|
12
12
|
def initialize(options = {})
|
13
13
|
options = DEFAULTS.merge(options)
|
14
|
+
@domain = options[:domain]
|
15
|
+
|
16
|
+
validate_domain!
|
14
17
|
|
15
|
-
@host = options[:host]
|
16
|
-
validate_host!
|
17
18
|
@secure_url_token = options[:secure_url_token]
|
18
19
|
@api_key = options[:api_key]
|
19
20
|
@use_https = options[:use_https]
|
@@ -23,38 +24,83 @@ module Imgix
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def path(path)
|
26
|
-
p = Path.new(prefix
|
27
|
+
p = Path.new(prefix, @secure_url_token, path)
|
27
28
|
p.ixlib("#{@library}-#{@version}") if @include_library_param
|
28
29
|
p
|
29
30
|
end
|
30
31
|
|
31
32
|
def purge(path)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
api_key_error = "A valid API key is required to send purge requests"
|
34
|
+
raise api_key_error if @api_key.nil?
|
35
|
+
|
36
|
+
endpoint = URI.parse("https://api.imgix.com/api/v1/purge")
|
37
|
+
# Ensure the path has been prefixed with '/'.
|
38
|
+
path = path.start_with?("/") ? path : "/#{path}"
|
39
|
+
url = prefix + path
|
40
|
+
|
41
|
+
req = create_request(endpoint, url, :json_data_from)
|
42
|
+
|
43
|
+
sock = Net::HTTP.new(endpoint.host, endpoint.port)
|
39
44
|
sock.use_ssl = true
|
40
|
-
|
41
|
-
res
|
45
|
+
sock.start { |http| http.request(req) }
|
42
46
|
end
|
43
47
|
|
44
|
-
def prefix
|
45
|
-
"#{@use_https ? 'https' : 'http'}://#{@
|
48
|
+
def prefix
|
49
|
+
"#{@use_https ? 'https' : 'http'}://#{@domain}"
|
46
50
|
end
|
47
51
|
|
48
52
|
private
|
49
53
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
54
|
+
# Create a request object by specifying it's endpoint, resource, and
|
55
|
+
# an optional data_fmt.
|
56
|
+
#
|
57
|
+
# `endpoint` must be a valid URI object
|
58
|
+
# `resource` must be a valid URL designating the resource to be purged
|
59
|
+
# `data_fmt` must be a valid method or Proc object
|
60
|
+
#
|
61
|
+
# Specify a `data_fmt` method when a resource (URL) requires
|
62
|
+
# additional formatting before being included in the request body.
|
63
|
+
# By default, the data format is specified by the `json_data_from`
|
64
|
+
# method.
|
65
|
+
def create_request(endpoint, resource, data_fmt = :json_data_from)
|
66
|
+
req = Net::HTTP::Post.new(endpoint.path)
|
67
|
+
req["Content-Type"] = "application/vnd.api+json"
|
68
|
+
req["Authorization"] = "Bearer #{@api_key}"
|
69
|
+
req["User-Agent"] = "imgix #{@library}-#{@version}"
|
70
|
+
|
71
|
+
if data_fmt.is_a?(Proc)
|
72
|
+
req.body = data_fmt.call(resource)
|
73
|
+
elsif data_fmt.is_a?(Symbol)
|
74
|
+
req.body = send(data_fmt, resource)
|
75
|
+
else
|
76
|
+
fmt_arg_error = "`fmt' is required to be of class Symbol or " \
|
77
|
+
"Proc but was found to be\n\s\sof class #{data_fmt.class}\n"
|
78
|
+
raise ArgumentError, fmt_arg_error
|
56
79
|
end
|
80
|
+
|
81
|
+
req
|
82
|
+
end
|
83
|
+
|
84
|
+
def json_data_from(url)
|
85
|
+
{
|
86
|
+
data: {
|
87
|
+
attributes: {
|
88
|
+
url: url
|
89
|
+
},
|
90
|
+
type: "purges"
|
91
|
+
}
|
92
|
+
}.to_json
|
93
|
+
end
|
94
|
+
|
95
|
+
def validate_domain!
|
96
|
+
domain_error = "The :domain option must be specified"
|
97
|
+
raise ArgumentError, domain_error if @domain.nil?
|
98
|
+
|
99
|
+
domain_error = "Domains must be passed in as fully-qualified"\
|
100
|
+
"domain names and should not include a protocol"\
|
101
|
+
'or any path element, i.e. "example.imgix.net"'\
|
102
|
+
|
103
|
+
raise ArgumentError, domain_error if @domain.match(DOMAIN_REGEX).nil?
|
57
104
|
end
|
58
|
-
|
59
105
|
end
|
60
106
|
end
|