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

deprecate: host-usage, param_helpers, and prefix-1-ary #91

Merged
merged 15 commits into from
Aug 17, 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
23 changes: 0 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,6 @@ client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token:
client.path('/images/demo.png').to_url(w: 200)
#=> https://your-subdomain.imgix.net/images/demo.png?w=200&s=2eadddacaa9bba4b88900d245f03f51e

# OR
path = client.path('/images/demo.png')
path.width = 200
path.to_url

# OR
client.path('/images/demo.png').width(200).height(300).to_url

# Some other tricks
path.defaults.width(300).to_url # Resets parameters
path.rect(x: 0, y: 50, width: 200, height: 300).to_url # Rect helper
```

**Deprecation Notice:** Usage of `:host` has been deprecated and will become invalid in the next major release.

In the previous version, `:host` can be used to specify a domain for an `Imgix::Client` like so:

```ruby
Imgix::Client.new(host: 'demo.imgix.net', secure_url_token: 'token')
```

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`.


## Srcset Generation

Expand Down
42 changes: 10 additions & 32 deletions lib/imgix/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,9 @@ class Client

def initialize(options = {})
options = DEFAULTS.merge(options)
host = options[:host]
domain = options[:domain]
@domain = options[:domain]

host_deprecated = "Warning: The identifier `host' has been deprecated and " \
"will\nappear as `domain' in the next major version, e.g. " \
"`@host'\nbecomes `@domain', `options[:host]' becomes " \
"`options[:domain]'.\n"

if host
warn host_deprecated
@host = host
elsif domain
@host = domain
else
@host = host
end

validate_host!
validate_domain!

@secure_url_token = options[:secure_url_token]
@api_key = options[:api_key]
Expand All @@ -39,7 +24,7 @@ def initialize(options = {})
end

def path(path)
p = Path.new(new_prefix, @secure_url_token, path)
p = Path.new(prefix, @secure_url_token, path)
p.ixlib("#{@library}-#{@version}") if @include_library_param
p
end
Expand All @@ -53,7 +38,7 @@ def purge(path)
api_key_error = "A valid api key is required to send purge requests"
raise api_key_error if @api_key.nil?

url = new_prefix + path
url = prefix + path
uri = URI.parse("https://api.imgix.com/v2/image/purger")

user_agent = { "User-Agent" => "imgix #{@library}-#{@version}" }
Expand All @@ -69,28 +54,21 @@ def purge(path)
res
end

def prefix(_path)
msg = "Warning: `Client::prefix' will take zero arguments " \
"in the next major version.\n"
warn msg
new_prefix
end

def new_prefix
"#{@use_https ? 'https' : 'http'}://#{@host}"
def prefix
"#{@use_https ? 'https' : 'http'}://#{@domain}"
end

private

def validate_host!
host_error = "The :host option must be specified"
raise ArgumentError, host_error if @host.nil?
def validate_domain!
domain_error = "The :domain option must be specified"
raise ArgumentError, domain_error if @domain.nil?

domain_error = "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"'\

raise ArgumentError, domain_error if @host.match(DOMAIN_REGEX).nil?
raise ArgumentError, domain_error if @domain.match(DOMAIN_REGEX).nil?
end
end
end
19 changes: 0 additions & 19 deletions lib/imgix/param_helpers.rb

This file was deleted.

53 changes: 4 additions & 49 deletions lib/imgix/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,9 @@
require "base64"
require "cgi/util"
require "erb"
require "imgix/param_helpers"

module Imgix
class Path
include ParamHelpers

ALIASES = {
width: :w,
height: :h,
rotation: :rot,
noise_reduction: :nr,
sharpness: :sharp,
exposure: :exp,
vibrance: :vib,
saturation: :sat,
brightness: :bri,
contrast: :con,
highlight: :high,
shadow: :shad,
gamma: :gam,
pixelate: :px,
halftone: :htn,
watermark: :mark,
text: :txt,
format: :fm,
quality: :q
}.freeze

def initialize(prefix, secure_url_token, path = "/")
@prefix = prefix
Expand Down Expand Up @@ -61,33 +37,12 @@ def defaults
self
end

def method_missing(method, *args)
key = method.to_s.gsub("=", "")
if args.empty?
return @options[key]
elsif args.first.nil? && @options.key?(key)
@options.delete(key) and return self
end

@options[key] = args.join(",")
self
def ixlib(lib_version)
@options[:ixlib] = lib_version
end

ALIASES.each do |from, to|
define_method from do |*args|
warn "Warning: `Path.#{from}' has been deprecated and " \
"will be removed in the next major version (along " \
"with all parameter `ALIASES`).\n"
send(to, *args)
end

define_method "#{from}=" do |*args|
warn "Warning: `Path.#{from}=' has been deprecated and " \
"will be removed in the next major version (along " \
"with all parameter `ALIASES`).\n"
send("#{to}=", *args)
return self
end
def ixlib=(lib_version)
@options[:ixlib] = lib_version
sherwinski marked this conversation as resolved.
Show resolved Hide resolved
end

def to_srcset(options: {}, **params)
Expand Down
6 changes: 3 additions & 3 deletions test/units/domains_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

class DomainsTest < Imgix::Test
def test_invalid_domain_append_slash
assert_raises(ArgumentError) {Imgix::Client.new(host: "assets.imgix.net/")}
assert_raises(ArgumentError) {Imgix::Client.new(domain: "assets.imgix.net/")}
end

def test_invalid_domain_prepend_scheme
assert_raises(ArgumentError) {Imgix::Client.new(host: "https://assets.imgix.net")}
assert_raises(ArgumentError) {Imgix::Client.new(domain: "https://assets.imgix.net")}
end

def test_invalid_domain_append_dash
assert_raises(ArgumentError) {Imgix::Client.new(host: "assets.imgix.net-")}
assert_raises(ArgumentError) {Imgix::Client.new(domain: "assets.imgix.net-")}
end
end
23 changes: 0 additions & 23 deletions test/units/param_helpers_test.rb

This file was deleted.

79 changes: 8 additions & 71 deletions test/units/path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@
require "test_helper"

class PathTest < Imgix::Test
def test_prefix_with_arg_warns
prefix_warn = "Warning: `Client::prefix' will take zero arguments " \
"in the next major version.\n"

assert_output(nil, prefix_warn) do
Imgix::Client.new(
domain: "test.imgix.net",
include_library_param: false
).prefix("")
end

# `new_prefix' is a placeholder until the bump, when it will become
# `prefix`.
assert_output(nil, nil) do
Imgix::Client.new(
domain: "test.imgix.net",
include_library_param: false
).new_prefix
end
end

def test_creating_a_path
path = client.path("/images/demo.png")
assert_equal "https://demo.imgix.net/images/demo.png?s=2c7c157eaf23b06a0deb2f60b81938c4", path.to_url
Expand All @@ -34,69 +13,27 @@ def test_creating_a_path

def test_signing_path_with_param
url = "https://demo.imgix.net/images/demo.png?w=200&s=da421114ca238d1f4a927b889f67c34e"
path = client.path("/images/demo.png")

assert_output(nil, "Warning: `Path.width=' has been deprecated and " \
"will be removed in the next major version (along " \
"with all parameter `ALIASES`).\n") do
path.width = 200
end

assert_equal url, path.to_url

path = client.path("/images/demo.png")
assert_equal url, path.to_url(w: 200)

path = client.path("/images/demo.png")

assert_output(nil, "Warning: `Path.width' has been deprecated and " \
"will be removed in the next major version (along " \
"with all parameter `ALIASES`).\n") do
assert_equal url, path.width(200).to_url
end
end

def test_resetting_defaults
url = "https://demo.imgix.net/images/demo.png?w=200&s=da421114ca238d1f4a927b889f67c34e"
path = client.path("/images/demo.png")

assert_output(nil, "Warning: `Path.height=' has been deprecated and " \
"will be removed in the next major version (along " \
"with all parameter `ALIASES`).\n") do
path.height = 300
end

assert_equal url, path.defaults.to_url(w: 200)
end

def test_path_with_multiple_params
url = "https://demo.imgix.net/images/demo.png?h=200&w=200&s=d570a1ecd765470f7b34a69b56718a7a"
path = client.path("/images/demo.png")

assert_equal url, path.to_url(h: 200, w: 200)

path = client.path("/images/demo.png")

assert_output(nil, "Warning: `Path.height' has been deprecated and " \
"will be removed in the next major version (along " \
"with all parameter `ALIASES`).\n") do
path.height(200)
end

assert_output(nil, "Warning: `Path.width' has been deprecated and " \
"will be removed in the next major version (along " \
"with all parameter `ALIASES`).\n") do
path.width(200)
end

assert_equal url, path.to_url
end

def test_path_with_multi_value_param_safely_encoded
url = "https://demo.imgix.net/images/demo.png?markalign=middle%2Ccenter&s=f0d0e28a739f022638f4ba6dddf9b694"
path = client.path("/images/demo.png")

assert_equal url, path.markalign("middle", "center").to_url
assert_equal url, path.to_url(markalign: "middle,center")
end

def test_param_keys_are_escaped
Expand All @@ -117,20 +54,20 @@ def test_base64_param_variants_are_base64_encoded
assert_equal "https://demo.imgix.net/~text?txt64=SSBjYW5uw7h0IGJlbMOuw6l24oiRIGl0IHdvcu-jv3MhIPCfmLE", ix_url
end

def test_host_is_required
def test_domain_is_required
assert_raises(ArgumentError) { Imgix::Client.new }
end

def test_token_is_optional
client = Imgix::Client.new(host: "demo.imgix.net", include_library_param: false)
client = Imgix::Client.new(domain: "demo.imgix.net", include_library_param: false)
url = "https://demo.imgix.net/images/demo.png"
path = client.path("/images/demo.png")

assert_equal url, path.to_url
end

def test_https_is_optional
client = Imgix::Client.new(host: "demo.imgix.net", include_library_param: false, use_https: false)
client = Imgix::Client.new(domain: "demo.imgix.net", include_library_param: false, use_https: false)
url = "http://demo.imgix.net/images/demo.png"
path = client.path("/images/demo.png")

Expand All @@ -149,7 +86,7 @@ def test_full_url_with_a_space
end

def test_include_library_param
client = Imgix::Client.new(host: "demo.imgix.net") # enabled by default
client = Imgix::Client.new(domain: "demo.imgix.net") # enabled by default
url = client.path("/images/demo.png").to_url

assert_equal "ixlib=rb-#{Imgix::VERSION}", URI(url).query
Expand All @@ -158,7 +95,7 @@ def test_include_library_param
def test_configure_library_param
library = "sinatra"
version = Imgix::VERSION
client = Imgix::Client.new(host: "demo.imgix.net", library_param: library, library_version: version) # enabled by default
client = Imgix::Client.new(domain: "demo.imgix.net", library_param: library, library_version: version) # enabled by default
url = client.path("/images/demo.png").to_url

assert_equal "ixlib=#{library}-#{version}", URI(url).query
Expand All @@ -167,10 +104,10 @@ def test_configure_library_param
private

def client
@client ||= Imgix::Client.new(host: "demo.imgix.net", secure_url_token: "10adc394", include_library_param: false)
@client ||= Imgix::Client.new(domain: "demo.imgix.net", secure_url_token: "10adc394", include_library_param: false)
end

def unsigned_client
@unsigned_client ||= Imgix::Client.new(host: "demo.imgix.net", include_library_param: false)
@unsigned_client ||= Imgix::Client.new(domain: "demo.imgix.net", include_library_param: false)
end
end
Loading