lib/imgix/client.rb in imgix-3.2.1 vs lib/imgix/client.rb in imgix-3.3.0
- old
+ new
@@ -5,56 +5,86 @@
require 'net/http'
require 'uri'
module Imgix
class Client
- DEFAULTS = { use_https: true }
+ DEFAULTS = { use_https: true }.freeze
def initialize(options = {})
options = DEFAULTS.merge(options)
+ host, domain = options[:host], options[:domain]
- @host = options[:host]
+ 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
+ end
+
validate_host!
+
@secure_url_token = options[:secure_url_token]
@api_key = options[:api_key]
@use_https = options[:use_https]
@include_library_param = options.fetch(:include_library_param, true)
- @library = options.fetch(:library_param, "rb")
+ @library = options.fetch(:library_param, 'rb')
@version = options.fetch(:library_version, Imgix::VERSION)
end
def path(path)
- p = Path.new(prefix(path), @secure_url_token, path)
+ p = Path.new(new_prefix, @secure_url_token, path)
p.ixlib("#{@library}-#{@version}") if @include_library_param
p
end
def purge(path)
- raise "Authentication token required" unless !!(@api_key)
- url = prefix(path)+path
+ token_error = 'Authentication token required'
+ raise token_error if @api_key.nil?
+
+ url = new_prefix + path
uri = URI.parse('https://api.imgix.com/v2/image/purger')
- req = Net::HTTP::Post.new(uri.path, {"User-Agent" => "imgix #{@library}-#{@version}"})
+
+ user_agent = { 'User-Agent' => "imgix #{@library}-#{@version}" }
+
+ req = Net::HTTP::Post.new(uri.path, user_agent)
req.basic_auth @api_key, ''
- req.set_form_data({'url' => url})
+ req.set_form_data({ url: url })
+
sock = Net::HTTP.new(uri.host, uri.port)
sock.use_ssl = true
- res = sock.start {|http| http.request(req) }
+ res = sock.start { |http| http.request(req) }
+
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}"
end
private
def validate_host!
- unless @host != nil
- raise ArgumentError, "The :host option must be specified"
+ host_error = 'The :host option must be specified'
+ raise ArgumentError, host_error if @host.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"'\
+
+ if @host.match(DOMAIN_REGEX).nil?
+ raise ArgumentError, domain_error
end
- if @host.match(DOMAIN_REGEX) == nil
- 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\"."
- end
end
-
end
end