imgix 3.3.1 → 4.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/.travis.yml +3 -5
- data/.vscode/tasks.json +29 -0
- data/CHANGELOG.md +30 -0
- data/Gemfile +6 -1
- data/README.md +17 -46
- data/Rakefile +2 -0
- data/imgix.gemspec +19 -22
- data/lib/imgix.rb +5 -4
- data/lib/imgix/client.rb +65 -51
- data/lib/imgix/path.rb +130 -86
- data/lib/imgix/version.rb +1 -1
- data/script/bench_path.rb +11 -0
- data/script/bench_to_url.rb +12 -0
- metadata +14 -58
- data/.github/CODEOWNERS +0 -2
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -28
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -27
- data/.github/ISSUE_TEMPLATE/question.md +0 -17
- data/.github/pull_request_template.md +0 -73
- data/lib/imgix/param_helpers.rb +0 -19
- data/test/test_helper.rb +0 -13
- data/test/units/domains_test.rb +0 -17
- data/test/units/param_helpers_test.rb +0 -23
- data/test/units/path_test.rb +0 -182
- data/test/units/purge_test.rb +0 -25
- data/test/units/srcset_test.rb +0 -755
- data/test/units/url_test.rb +0 -80
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6ab4780027f2b1f8835f0fe5ad177afbb1ee954f95bd4fa76e92986bb8a31264
|
|
4
|
+
data.tar.gz: 5c1c49734258dd0c3c99cffa3564d99e140cfede4a6a117a6d3433902a019413
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a12613119005a7e9cf84d054b25c459aa6e6cc34e3e9001c11b55e72c3f76ca1566bc4e6737e1891e1445513ae1eb0b8cc247497312937a1a875740b7064afbf
|
|
7
|
+
data.tar.gz: e9de151e47fa1c555d2199d21539dcb52c27b5879dcb8ece7c822fc96c81f4a7e204a47d73602e349d38728defc38256759d1fb8ad0267369be46d168e0e770e
|
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
|
@@ -2,9 +2,8 @@ dist: trusty
|
|
|
2
2
|
language: ruby
|
|
3
3
|
bundler_args: --without development
|
|
4
4
|
rvm:
|
|
5
|
-
- 2.
|
|
6
|
-
- 2.
|
|
7
|
-
- 2.1.8
|
|
5
|
+
- 2.7
|
|
6
|
+
- 2.6
|
|
8
7
|
- jruby-9.2.11.0
|
|
9
8
|
|
|
10
9
|
jobs:
|
|
@@ -13,9 +12,8 @@ jobs:
|
|
|
13
12
|
|
|
14
13
|
include:
|
|
15
14
|
- name: Rubinius
|
|
16
|
-
rvm: rbx-3
|
|
15
|
+
rvm: rbx-3
|
|
17
16
|
dist: trusty
|
|
18
17
|
|
|
19
18
|
allow_failures:
|
|
20
19
|
- name: Rubinius
|
|
21
|
-
|
data/.vscode/tasks.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "2.0.0",
|
|
3
|
+
"tasks": [
|
|
4
|
+
{
|
|
5
|
+
"label": "rake: test",
|
|
6
|
+
"type": "shell",
|
|
7
|
+
"group":{
|
|
8
|
+
"kind": "test",
|
|
9
|
+
"isDefault": true
|
|
10
|
+
},
|
|
11
|
+
"problemMatcher": {
|
|
12
|
+
"owner": "ruby",
|
|
13
|
+
"fileLocation": ["relative", "${workspaceFolder}"],
|
|
14
|
+
"pattern": [
|
|
15
|
+
{
|
|
16
|
+
"regexp": "^([^:]+: .+)",
|
|
17
|
+
"message": 1
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"regexp": "^ ([^:]+):(\\d+)",
|
|
21
|
+
"file": 1,
|
|
22
|
+
"line": 2
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
"command": "bundle exec rake test"
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
}
|
data/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,36 @@
|
|
|
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.3](https://github.com/imgix/imgix-rb/compare/4.0.2...4.0.3) - May 05, 2021
|
|
7
|
+
|
|
8
|
+
* fix: encode web proxy paths using url_encode ([#111](https://github.com/imgix/imgix-rb/pull/111))
|
|
9
|
+
|
|
10
|
+
## [4.0.2](https://github.com/imgix/imgix-rb/compare/4.0.1...4.0.2) - April 22, 2021
|
|
11
|
+
|
|
12
|
+
* fix(path): correctly encode reserved chars ([#108](https://github.com/imgix/imgix-rb/pull/108))
|
|
13
|
+
* test: add benchmark script for Imgix::Path#to_url ([4b00c4b](https://github.com/imgix/imgix-rb/commit/4b00c4b6d27b87787464438b36954b9c166096eb))
|
|
14
|
+
* perf: avoid calculating path_and_params twice ([#109](https://github.com/imgix/imgix-rb/pull/109))
|
|
15
|
+
* ci: use major version only for Rubinius 3 ([921e41a](https://github.com/imgix/imgix-rb/commit/921e41ac7789a55c78b42d60ac99cc0b641a69fd))
|
|
16
|
+
* ci: use optimistic versioning ([#107](https://github.com/imgix/imgix-rb/pull/107))
|
|
17
|
+
* ci: remove support for ruby version <2.5 ([#106](https://github.com/imgix/imgix-rb/pull/106))
|
|
18
|
+
* perf: precompute DEFAULT_TARGET_WIDTHS to avoid repeating on Path.new ([#105](https://github.com/imgix/imgix-rb/pull/105))
|
|
19
|
+
|
|
20
|
+
## [4.0.1](https://github.com/imgix/imgix-rb/compare/4.0.0...4.0.1) - March 24, 2021
|
|
21
|
+
|
|
22
|
+
* fix: dpr srcset when only h param ([#101](https://github.com/imgix/imgix-rb/pull/101))
|
|
23
|
+
* docs: update travis badge to point to travis-ci.com ([#99](https://github.com/imgix/imgix-rb/pull/99))
|
|
24
|
+
* docs: fixed typo w/ srcset min/max widths ([#97](https://github.com/imgix/imgix-rb/pull/97))
|
|
25
|
+
|
|
26
|
+
## [4.0.0](https://github.com/imgix/imgix-rb/compare/3.4.0...4.0.0) - August 26, 2020
|
|
27
|
+
|
|
28
|
+
* deprecate: host-usage, param_helpers, and prefix-1-ary ([#91](https://github.com/imgix/imgix-rb/pull/91))
|
|
29
|
+
* fix: update purge operation api keys ([#93](https://github.com/imgix/imgix-rb/pull/93))
|
|
30
|
+
|
|
31
|
+
## [3.4.0](https://github.com/imgix/imgix-rb/compare/3.3.1...3.4.0) - August 12, 2020
|
|
32
|
+
|
|
33
|
+
* fix: deprecate api key versions ([#87](https://github.com/imgix/imgix-rb/pull/87))
|
|
34
|
+
* docs(readme): deprecation notice for pre-4.0 api keys ([#88](https://github.com/imgix/imgix-rb/pull/88))
|
|
35
|
+
|
|
6
36
|
## [3.3.1](https://github.com/imgix/imgix-rb/compare/3.3.0...3.3.1) - July 27, 2020
|
|
7
37
|
|
|
8
38
|
* fix: ensure host is initialized ([#82](https://github.com/imgix/imgix-rb/pull/82))
|
data/Gemfile
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
source 'https://rubygems.org'
|
|
2
4
|
|
|
3
5
|
# Specify your gem's dependencies in imgix.gemspec
|
|
4
6
|
gemspec
|
|
5
7
|
|
|
6
8
|
gem 'rake'
|
|
9
|
+
gem 'json'
|
|
7
10
|
gem 'minitest'
|
|
11
|
+
gem 'minitest-reporters'
|
|
8
12
|
gem 'webmock'
|
|
9
|
-
gem '
|
|
13
|
+
gem 'benchmark-ips'
|
|
14
|
+
|
data/README.md
CHANGED
|
@@ -4,9 +4,10 @@
|
|
|
4
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
5
|
|
|
6
6
|
[](https://rubygems.org/gems/imgix)
|
|
7
|
-
[](https://travis-ci.com/imgix/imgix-rb)
|
|
8
8
|

|
|
9
9
|
[](https://github.com/imgix/imgix-rb/blob/main/LICENSE)
|
|
10
|
+
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fimgix%2Fimgix-rb?ref=badge_shield)
|
|
10
11
|
|
|
11
12
|
---
|
|
12
13
|
<!-- /ix-docs-ignore -->
|
|
@@ -14,16 +15,16 @@
|
|
|
14
15
|
- [Installation](#installation)
|
|
15
16
|
- [Usage](#usage)
|
|
16
17
|
- [Srcset Generation](#srcset-generation)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
- [Multiple Parameters](#multiple-parameters)
|
|
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
23
|
- [Purge Cache](#purge-cache)
|
|
24
24
|
- [URL encoding and signed imgix URLs](#url-encoding-and-signed-imgix-urls)
|
|
25
25
|
- [What is the `ixlib` param on every request?](#what-is-the-ixlib-param-on-every-request)
|
|
26
26
|
- [Contributing](#contributing)
|
|
27
|
+
- [License](#license)
|
|
27
28
|
|
|
28
29
|
## Installation
|
|
29
30
|
|
|
@@ -48,36 +49,13 @@ Initialize a client with a `:domain` and your `:secure_url_token`. By default, H
|
|
|
48
49
|
|
|
49
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.
|
|
50
51
|
|
|
51
|
-
```
|
|
52
|
+
```rb
|
|
52
53
|
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token')
|
|
53
54
|
|
|
54
55
|
client.path('/images/demo.png').to_url(w: 200)
|
|
55
56
|
#=> https://your-subdomain.imgix.net/images/demo.png?w=200&s=2eadddacaa9bba4b88900d245f03f51e
|
|
56
|
-
|
|
57
|
-
# OR
|
|
58
|
-
path = client.path('/images/demo.png')
|
|
59
|
-
path.width = 200
|
|
60
|
-
path.to_url
|
|
61
|
-
|
|
62
|
-
# OR
|
|
63
|
-
client.path('/images/demo.png').width(200).height(300).to_url
|
|
64
|
-
|
|
65
|
-
# Some other tricks
|
|
66
|
-
path.defaults.width(300).to_url # Resets parameters
|
|
67
|
-
path.rect(x: 0, y: 50, width: 200, height: 300).to_url # Rect helper
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
**Deprecation Notice:** Usage of `:host` has been deprecated and will become invalid in the next major release.
|
|
71
|
-
|
|
72
|
-
In the previous version, `:host` can be used to specify a domain for an `Imgix::Client` like so:
|
|
73
|
-
|
|
74
|
-
```ruby
|
|
75
|
-
Imgix::Client.new(host: 'demo.imgix.net', secure_url_token: 'token')
|
|
76
57
|
```
|
|
77
58
|
|
|
78
|
-
Code using `:host` like the above will continue to work until the next major release. While using `:host` will remain valid until the next major release, its usage will result in a deprecation warning. In order to resolve these deprecation warnings before upgrading to the new release, use `:domain` instead of `:host`.
|
|
79
|
-
|
|
80
|
-
|
|
81
59
|
## Srcset Generation
|
|
82
60
|
|
|
83
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.
|
|
@@ -102,7 +80,7 @@ https://your-subdomain.imgix.net/images/demo.png?w=8192&s=9fbd257c53e770e345ce34
|
|
|
102
80
|
|
|
103
81
|
### Fixed image rendering
|
|
104
82
|
|
|
105
|
-
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.
|
|
106
84
|
|
|
107
85
|
```rb
|
|
108
86
|
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token', include_library_param: false)
|
|
@@ -167,11 +145,11 @@ https://testing.imgix.net/image.jpg?w=8192 8192w
|
|
|
167
145
|
|
|
168
146
|
### Minimum and Maximum Width Ranges
|
|
169
147
|
|
|
170
|
-
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:
|
|
171
149
|
|
|
172
150
|
```rb
|
|
173
151
|
client = Imgix::Client.new(domain: 'testing.imgix.net', include_library_param: false)
|
|
174
|
-
client.path('image.jpg').to_srcset(options: {
|
|
152
|
+
client.path('image.jpg').to_srcset(options: { min_width: 500, max_width: 2000 })
|
|
175
153
|
```
|
|
176
154
|
|
|
177
155
|
Will result in a smaller, more tailored srcset.
|
|
@@ -190,7 +168,7 @@ https://testing.imgix.net/image.jpg?w=1902 1902w,
|
|
|
190
168
|
https://testing.imgix.net/image.jpg?w=2000 2000w
|
|
191
169
|
```
|
|
192
170
|
|
|
193
|
-
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.
|
|
194
172
|
|
|
195
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.
|
|
196
174
|
|
|
@@ -218,19 +196,9 @@ https://testing.imgix.net/image.jpg?w=100&dpr=4&q=23 4x,
|
|
|
218
196
|
https://testing.imgix.net/image.jpg?w=100&dpr=5&q=20 5x
|
|
219
197
|
```
|
|
220
198
|
|
|
221
|
-
## Multiple Parameters
|
|
222
|
-
|
|
223
|
-
When the imgix api requires multiple parameters you have to use the method rather than an accessor.
|
|
224
|
-
|
|
225
|
-
For example to use the noise reduction:
|
|
226
|
-
|
|
227
|
-
``` ruby
|
|
228
|
-
path.noise_reduction(50,50)
|
|
229
|
-
```
|
|
230
|
-
|
|
231
199
|
## Purge Cache
|
|
232
200
|
|
|
233
|
-
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.
|
|
234
202
|
|
|
235
203
|
```ruby
|
|
236
204
|
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', api_key: 'your-key')
|
|
@@ -256,3 +224,6 @@ client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', include_library_p
|
|
|
256
224
|
## Contributing
|
|
257
225
|
|
|
258
226
|
See the [contributing guide](Contributing.markdown).
|
|
227
|
+
|
|
228
|
+
## License
|
|
229
|
+
[](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,8 +1,8 @@
|
|
|
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
|
|
@@ -22,7 +22,7 @@ module Imgix
|
|
|
22
22
|
increment_percentage = tolerance || DEFAULT_WIDTH_TOLERANCE
|
|
23
23
|
|
|
24
24
|
unless increment_percentage.is_a?(Numeric) && increment_percentage > 0
|
|
25
|
-
width_increment_error =
|
|
25
|
+
width_increment_error = "error: `width_tolerance` must be a positive `Numeric` value"
|
|
26
26
|
raise ArgumentError, width_increment_error
|
|
27
27
|
end
|
|
28
28
|
|
|
@@ -39,6 +39,7 @@ module Imgix
|
|
|
39
39
|
resolutions.push(max_size)
|
|
40
40
|
return resolutions
|
|
41
41
|
}
|
|
42
|
+
DEFAULT_TARGET_WIDTHS = TARGET_WIDTHS.call(DEFAULT_WIDTH_TOLERANCE, MIN_WIDTH, MAX_WIDTH).freeze
|
|
42
43
|
|
|
43
44
|
# hash of default quality parameter values mapped by each dpr srcset entry
|
|
44
45
|
DPR_QUALITY = {
|
data/lib/imgix/client.rb
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
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
|
|
@@ -11,82 +11,96 @@ module Imgix
|
|
|
11
11
|
|
|
12
12
|
def initialize(options = {})
|
|
13
13
|
options = DEFAULTS.merge(options)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
host_deprecated = "Warning: The identifier `host' has been deprecated and " \
|
|
17
|
-
"will\nappear as `domain' in the next major version, e.g. " \
|
|
18
|
-
"`@host'\nbecomes `@domain', `options[:host]' becomes " \
|
|
19
|
-
"`options[:domain]'.\n"
|
|
20
|
-
|
|
21
|
-
if host
|
|
22
|
-
warn host_deprecated
|
|
23
|
-
@host = host
|
|
24
|
-
elsif domain
|
|
25
|
-
@host = domain
|
|
26
|
-
else
|
|
27
|
-
@host = host
|
|
28
|
-
end
|
|
14
|
+
@domain = options[:domain]
|
|
29
15
|
|
|
30
|
-
|
|
16
|
+
validate_domain!
|
|
31
17
|
|
|
32
18
|
@secure_url_token = options[:secure_url_token]
|
|
33
19
|
@api_key = options[:api_key]
|
|
34
20
|
@use_https = options[:use_https]
|
|
35
21
|
@include_library_param = options.fetch(:include_library_param, true)
|
|
36
|
-
@library = options.fetch(:library_param,
|
|
22
|
+
@library = options.fetch(:library_param, "rb")
|
|
37
23
|
@version = options.fetch(:library_version, Imgix::VERSION)
|
|
38
24
|
end
|
|
39
25
|
|
|
40
26
|
def path(path)
|
|
41
|
-
p = Path.new(
|
|
27
|
+
p = Path.new(prefix, @secure_url_token, path)
|
|
42
28
|
p.ixlib("#{@library}-#{@version}") if @include_library_param
|
|
43
29
|
p
|
|
44
30
|
end
|
|
45
31
|
|
|
46
32
|
def purge(path)
|
|
47
|
-
|
|
48
|
-
raise
|
|
49
|
-
|
|
50
|
-
url = new_prefix + path
|
|
51
|
-
uri = URI.parse('https://api.imgix.com/v2/image/purger')
|
|
33
|
+
api_key_error = "A valid API key is required to send purge requests"
|
|
34
|
+
raise api_key_error if @api_key.nil?
|
|
52
35
|
|
|
53
|
-
|
|
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
|
|
54
40
|
|
|
55
|
-
req =
|
|
56
|
-
req.basic_auth @api_key, ''
|
|
57
|
-
req.set_form_data({ url: url })
|
|
41
|
+
req = create_request(endpoint, url, :json_data_from)
|
|
58
42
|
|
|
59
|
-
sock = Net::HTTP.new(
|
|
43
|
+
sock = Net::HTTP.new(endpoint.host, endpoint.port)
|
|
60
44
|
sock.use_ssl = true
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
res
|
|
45
|
+
sock.start { |http| http.request(req) }
|
|
64
46
|
end
|
|
65
47
|
|
|
66
|
-
def prefix
|
|
67
|
-
|
|
68
|
-
"in the next major version.\n"
|
|
69
|
-
warn msg
|
|
70
|
-
new_prefix
|
|
48
|
+
def prefix
|
|
49
|
+
"#{@use_https ? 'https' : 'http'}://#{@domain}"
|
|
71
50
|
end
|
|
72
51
|
|
|
73
|
-
|
|
74
|
-
|
|
52
|
+
private
|
|
53
|
+
|
|
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
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
req
|
|
75
82
|
end
|
|
76
83
|
|
|
77
|
-
|
|
84
|
+
def json_data_from(url)
|
|
85
|
+
{
|
|
86
|
+
data: {
|
|
87
|
+
attributes: {
|
|
88
|
+
url: url
|
|
89
|
+
},
|
|
90
|
+
type: "purges"
|
|
91
|
+
}
|
|
92
|
+
}.to_json
|
|
93
|
+
end
|
|
78
94
|
|
|
79
|
-
def
|
|
80
|
-
|
|
81
|
-
raise ArgumentError,
|
|
95
|
+
def validate_domain!
|
|
96
|
+
domain_error = "The :domain option must be specified"
|
|
97
|
+
raise ArgumentError, domain_error if @domain.nil?
|
|
82
98
|
|
|
83
|
-
domain_error =
|
|
84
|
-
|
|
99
|
+
domain_error = "Domains must be passed in as fully-qualified"\
|
|
100
|
+
"domain names and should not include a protocol"\
|
|
85
101
|
'or any path element, i.e. "example.imgix.net"'\
|
|
86
102
|
|
|
87
|
-
if @
|
|
88
|
-
raise ArgumentError, domain_error
|
|
89
|
-
end
|
|
103
|
+
raise ArgumentError, domain_error if @domain.match(DOMAIN_REGEX).nil?
|
|
90
104
|
end
|
|
91
105
|
end
|
|
92
106
|
end
|