imgix 3.2.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2888effee0dc9f00b05832a337fc22d08973703a
4
- data.tar.gz: 0d55efe17b819635689e9e9a361339702ae2df73
2
+ SHA256:
3
+ metadata.gz: a91b4da0e3b9063761f986110ff0ab379938986186642e08ec59be0ae5601cad
4
+ data.tar.gz: 61a69f51d3d453480fa9f5acc496b245f65f94d7088a704fe0ee31fc4f4146dc
5
5
  SHA512:
6
- metadata.gz: 6a4c006d3efb6e4e60552a7330e22632f40f57f8fee2a0b5ea845f354890a8cf1eb8c7594e5e6ca64fc933bcb2fbab6017c0b9f8da276657d25ce8b4abaf968e
7
- data.tar.gz: 0127e8b6c7643b63e2d89437499493bf4d9eb1cf7fb7dd5392436e17a55e480f5e7a923064204f2490084b79339832ec6e9da85af4ccf6aaac03d192ac37d3a8
6
+ metadata.gz: ae1e01d77d40bdccf28cadebb0d4a75704f1da9d0d6619c102b0e05bae680f3801675d1674b67b0fb94fb3ed5e1576e386d8c31cda5bcab252a0e997099245ff
7
+ data.tar.gz: b715696707c314d2a9bf047ff32ba579cf12fca5115601cc59d5db88e8407a09012a1783bb194c10a5bcc910726b8c4ca0d81f44c52e4ce109c17563baa7f4bb
@@ -0,0 +1,2 @@
1
+ Style/StringLiterals:
2
+ EnforcedStyle: double_quotes
@@ -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.0.5.0
9
- - rbx-3.107
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
+
@@ -3,6 +3,33 @@
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.0](https://github.com/imgix/imgix-rb/compare/3.4.0...4.0.0) - August 26, 2020
7
+
8
+ * deprecate: host-usage, param_helpers, and prefix-1-ary ([#91](https://github.com/imgix/imgix-rb/pull/91))
9
+ * fix: update purge operation api keys ([#93](https://github.com/imgix/imgix-rb/pull/93))
10
+
11
+ ## [3.4.0](https://github.com/imgix/imgix-rb/compare/3.3.1...3.4.0) - August 12, 2020
12
+
13
+ * fix: deprecate api key versions ([#87](https://github.com/imgix/imgix-rb/pull/87))
14
+ * docs(readme): deprecation notice for pre-4.0 api keys ([#88](https://github.com/imgix/imgix-rb/pull/88))
15
+
16
+ ## [3.3.1](https://github.com/imgix/imgix-rb/compare/3.3.0...3.3.1) - July 27, 2020
17
+
18
+ * fix: ensure host is initialized ([#82](https://github.com/imgix/imgix-rb/pull/82))
19
+
20
+ ## [3.3.0](https://github.com/imgix/imgix-rb/compare/3.2.1...3.3.0) - July 21, 2020
21
+
22
+ * fix: warn prefix-with-args is deprecated ([#78](https://github.com/imgix/imgix-rb/pull/78))
23
+ * fix: warn host has been deprecated in favor of domain ([#76](https://github.com/imgix/imgix-rb/pull/76))
24
+ * fix: deprecate ALIASES and corresponding define_method(s) ([#75](https://github.com/imgix/imgix-rb/pull/75))
25
+ * fix: deprecate ParamHelpers.rect ([#74](https://github.com/imgix/imgix-rb/pull/74))
26
+ * fix: allow rbx-3.107 to fail (travis-config) ([#70](https://github.com/imgix/imgix-rb/pull/70))
27
+ * fix: remove ensure even requirement ([#69](https://github.com/imgix/imgix-rb/pull/69))
28
+
29
+ ## [3.2.1](https://github.com/imgix/imgix-rb/compare/3.2.0...3.2.1) - November 15, 2019
30
+
31
+ * fix: duplicate entries when using `min_width` or `max_width` ([#59](https://github.com/imgix/imgix-rb/pull/59))
32
+
6
33
  ## [3.2.0](https://github.com/imgix/imgix-rb/compare/3.1.1...3.2.0) - November 15, 2019
7
34
 
8
35
  * feat: append variable qualities to dpr srcsets ([#58](https://github.com/imgix/imgix-rb/pull/58))
data/Gemfile CHANGED
@@ -1,9 +1,11 @@
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'
8
11
  gem 'webmock'
9
- gem 'addressable'
data/README.md CHANGED
@@ -1,8 +1,29 @@
1
- # Imgix
2
-
3
- Official Ruby Gem for signing [imgix](http://imgix.com) URLs. Tested under 2.3.0, 2.2.4, 2.1.8, jruby-9.0.5.0, and rbx-2.11.
4
-
5
- [![Build Status](https://travis-ci.org/imgix/imgix-rb.svg?branch=master)](https://travis-ci.org/imgix/imgix-rb)
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.org/imgix/imgix-rb.svg?branch=main)](https://travis-ci.org/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
+
11
+ ---
12
+ <!-- /ix-docs-ignore -->
13
+
14
+ - [Installation](#installation)
15
+ - [Usage](#usage)
16
+ - [Srcset Generation](#srcset-generation)
17
+ - [Fixed image rendering](#fixed-image-rendering)
18
+ - [Custom Widths](#custom-widths)
19
+ - [Width Tolerance](#width-tolerance)
20
+ - [Minimum and Maximum Width Ranges](#minimum-and-maximum-width-ranges)
21
+ - [Variable Qualities](#variable-qualities)
22
+ - [Multiple Parameters](#multiple-parameters)
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)
6
27
 
7
28
  ## Installation
8
29
 
@@ -23,27 +44,15 @@ Or install it yourself as:
23
44
 
24
45
  ## Usage
25
46
 
26
- Initialize a client with a `:host` and your `:secure_url_token`. By default, HTTPS URLs are generated, but you can toggle that by passing `use_https: false`.
47
+ 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
48
 
28
49
  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
50
 
30
- ``` ruby
31
- client = Imgix::Client.new(host: 'your-subdomain.imgix.net', secure_url_token: 'your-token')
51
+ ```rb
52
+ client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token')
32
53
 
33
54
  client.path('/images/demo.png').to_url(w: 200)
34
55
  #=> 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
56
  ```
48
57
 
49
58
  ## Srcset Generation
@@ -51,7 +60,7 @@ path.rect(x: 0, y: 50, width: 200, height: 300).to_url # Rect helper
51
60
  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
61
 
53
62
  ```rb
54
- client = Imgix::Client.new(host: 'your-subdomain.imgix.net', secure_url_token: 'your-token', include_library_param: false)
63
+ client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token', include_library_param: false)
55
64
  path = client.path('/images/demo.png')
56
65
 
57
66
  srcset = path.to_srcset
@@ -73,7 +82,7 @@ https://your-subdomain.imgix.net/images/demo.png?w=8192&s=9fbd257c53e770e345ce34
73
82
  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`, `h`, and `ar`. By invoking `to_srcset` with either a width **or** the height and aspect ratio (along with `fit=crop`, typically) provided, a different `srcset` will be generated for a fixed-size image instead.
74
83
 
75
84
  ```rb
76
- client = Imgix::Client.new(host: 'your-subdomain.imgix.net', secure_url_token: 'your-token', include_library_param: false)
85
+ client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token', include_library_param: false)
77
86
  path = client.path('/images/demo.png')
78
87
 
79
88
  srcset = path.to_srcset(h:800, ar:'3:2', fit:'crop')
@@ -96,7 +105,7 @@ For more information to better understand `srcset`, we highly recommend [Eric Po
96
105
  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
106
 
98
107
  ```rb
99
- @client ||= Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false)
108
+ @client ||= Imgix::Client.new(domain: 'testing.imgix.net', include_library_param: false)
100
109
  .path('image.jpg')
101
110
  .to_srcset(options: { widths: [100, 500, 1000, 1800] })
102
111
  ```
@@ -119,7 +128,7 @@ The `srcset` width tolerance dictates the maximum tolerated size difference betw
119
128
  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
129
 
121
130
  ```rb
122
- client = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false)
131
+ client = Imgix::Client.new(domain: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false)
123
132
  client.path('image.jpg').to_srcset(options: { width_tolerance: 0.20 })
124
133
  ```
125
134
 
@@ -138,7 +147,7 @@ https://testing.imgix.net/image.jpg?w=8192 8192w
138
147
  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_srcset` and/or `max_srcset` to the `options` keyword parameters:
139
148
 
140
149
  ```rb
141
- client = Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false)
150
+ client = Imgix::Client.new(domain: 'testing.imgix.net', include_library_param: false)
142
151
  client.path('image.jpg').to_srcset(options: { min_srcset: 500, max_srcset: 2000 })
143
152
  ```
144
153
 
@@ -171,7 +180,7 @@ This behavior will respect any overriding `q` value passed in as a parameter. Ad
171
180
  This behavior specifically occurs when a [fixed-size image](https://github.com/imgix/imgix-rb#fixed-image-rendering) is rendered, for example:
172
181
 
173
182
  ```rb
174
- srcset = Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false)
183
+ srcset = Imgix::Client.new(domain: 'testing.imgix.net', include_library_param: false)
175
184
  .path('image.jpg')
176
185
  .to_srcset(w:100)
177
186
  ```
@@ -186,30 +195,20 @@ https://testing.imgix.net/image.jpg?w=100&dpr=4&q=23 4x,
186
195
  https://testing.imgix.net/image.jpg?w=100&dpr=5&q=20 5x
187
196
  ```
188
197
 
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
198
  ## Purge Cache
200
199
 
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, then calling Imgix::Client#purge with the resource path.
200
+ 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
201
 
203
202
  ```ruby
204
- client = Imgix::Client.new(host: 'your-subdomain.imgix.net', api_key: 'your-key')
203
+ client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', api_key: 'your-key')
205
204
  client.purge('/images/demo.png')
206
205
  ```
207
206
 
208
207
  To learn more about purging assets with imgix, [see our docs](https://docs.imgix.com/setup/purging-images).
209
208
 
210
- ## URL encoding and signed ImgIX URLs
209
+ ## URL encoding and signed imgix URLs
211
210
 
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.
211
+ 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
212
 
214
213
  ## What is the `ixlib` param on every request?
215
214
 
@@ -218,7 +217,7 @@ For security and diagnostic purposes, we sign all requests with the language and
218
217
  This can be disabled by including `include_library_param: false` in the instantiation Hash parameter for `Imgix::Client`:
219
218
 
220
219
  ```ruby
221
- client = Imgix::Client.new(host: 'your-subdomain.imgix.net', include_library_param: false )
220
+ client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', include_library_param: false )
222
221
  ```
223
222
 
224
223
  ## Contributing
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
 
3
5
  require 'rake/testtask'
@@ -1,31 +1,28 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
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 = 'imgix'
6
+ spec.name = "imgix"
8
7
  spec.version = Imgix::VERSION
9
- spec.authors = ['Kelly Sutton', 'Sam Soffes', 'Ryan LeFevre', 'Antony Denyer', 'Paul Straw', 'Sherwin Heydarbeygi']
10
- spec.email = ['kelly@imgix.com', 'sam@soff.es', 'ryan@layervault.com', 'email@antonydenyer.co.uk', 'paul@imgix.com', 'sherwin@imgix.com']
11
- spec.description = 'Easily create and sign imgix URLs.'
12
- spec.summary = 'Official Ruby Gem for easily creating and signing imgix URLs.'
13
- spec.homepage = 'https://github.com/imgix/imgix-rb'
14
- spec.license = 'MIT'
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
- 'bug_tracker_uri' => 'https://github.com/imgix/imgix-rb/issues',
18
- 'changelog_uri' => 'https://github.com/imgix/imgix-rb/blob/master/CHANGELOG.md',
19
- 'documentation_uri' => "https://www.rubydoc.info/gems/imgix/#{spec.version}",
20
- 'source_code_uri' => "https://github.com/imgix/imgix-rb/tree/#{spec.version}"
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 = `git ls-files`.split($/)
24
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
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.required_ruby_version = '>= 1.9.0'
29
- spec.add_dependency 'addressable'
30
- spec.add_development_dependency 'webmock'
26
+ spec.require_paths = ["lib"]
27
+ spec.required_ruby_version = ">= 1.9.0"
31
28
  end
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'imgix/version'
4
- require 'imgix/client'
5
- require 'imgix/path'
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
- unless increment_percentage.is_a? Numeric and increment_percentage > 0
24
- raise ArgumentError, "The width_tolerance argument must be passed a positive scalar value"
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(prev <= max_size)
33
+ while prev < max_size
32
34
  # ensures that each width is even
33
- resolutions.push((2 * (prev / 2).round))
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
@@ -1,19 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'digest'
4
- require 'addressable/uri'
5
- require 'net/http'
6
- require 'uri'
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(path), @secure_url_token, path)
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
- raise "Authentication token required" unless !!(@api_key)
33
- url = prefix(path)+path
34
- uri = URI.parse('https://api.imgix.com/v2/image/purger')
35
- req = Net::HTTP::Post.new(uri.path, {"User-Agent" => "imgix #{@library}-#{@version}"})
36
- req.basic_auth @api_key, ''
37
- req.set_form_data({'url' => url})
38
- sock = Net::HTTP.new(uri.host, uri.port)
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
- res = sock.start {|http| http.request(req) }
41
- res
45
+ sock.start { |http| http.request(req) }
42
46
  end
43
47
 
44
- def prefix(path)
45
- "#{@use_https ? 'https' : 'http'}://#{@host}"
48
+ def prefix
49
+ "#{@use_https ? 'https' : 'http'}://#{@domain}"
46
50
  end
47
51
 
48
52
  private
49
53
 
50
- def validate_host!
51
- unless @host != nil
52
- raise ArgumentError, "The :host option must be specified"
53
- end
54
- if @host.match(DOMAIN_REGEX) == nil
55
- raise ArgumentError, "Domains must be passed in as fully-qualified domain names and should not include a protocol or any path element, i.e. \"example.imgix.net\"."
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