lib/imgix/client.rb in imgix-3.4.0 vs lib/imgix/client.rb in imgix-4.0.0

- old
+ new

@@ -1,97 +1,106 @@ # frozen_string_literal: true -require 'digest' -require 'addressable/uri' -require 'net/http' -require 'uri' +require "digest" +require "net/http" +require "uri" +require "json" module Imgix class Client DEFAULTS = { use_https: true }.freeze def initialize(options = {}) options = DEFAULTS.merge(options) - host, domain = options[:host], 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" + validate_domain! - if host - warn host_deprecated - @host = host - elsif domain - @host = domain - else - @host = host - 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(new_prefix, @secure_url_token, path) + p = Path.new(prefix, @secure_url_token, path) p.ixlib("#{@library}-#{@version}") if @include_library_param p end def purge(path) - api_key_deprecated = \ - "Warning: Your `api_key` will no longer work after upgrading to\n" \ - "imgix-rb version >= 4.0.0.\n" - warn api_key_deprecated - - api_key_error = 'A valid api key is required to send purge requests' + 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 - uri = URI.parse('https://api.imgix.com/v2/image/purger') + endpoint = URI.parse("https://api.imgix.com/api/v1/purge") + # Ensure the path has been prefixed with '/'. + path = path.start_with?("/") ? path : "/#{path}" + url = prefix + path - user_agent = { 'User-Agent' => "imgix #{@library}-#{@version}" } + req = create_request(endpoint, url, :json_data_from) - req = Net::HTTP::Post.new(uri.path, user_agent) - req.basic_auth @api_key, '' - req.set_form_data({ url: url }) - - sock = Net::HTTP.new(uri.host, uri.port) + sock = Net::HTTP.new(endpoint.host, endpoint.port) sock.use_ssl = true - res = sock.start { |http| http.request(req) } + sock.start { |http| http.request(req) } + end - res + def prefix + "#{@use_https ? 'https' : 'http'}://#{@domain}" end - def prefix(path) - msg = "Warning: `Client::prefix' will take zero arguments " \ - "in the next major version.\n" - warn msg - new_prefix + private + + # Create a request object by specifying it's endpoint, resource, and + # an optional data_fmt. + # + # `endpoint` must be a valid URI object + # `resource` must be a valid URL designating the resource to be purged + # `data_fmt` must be a valid method or Proc object + # + # Specify a `data_fmt` method when a resource (URL) requires + # additional formatting before being included in the request body. + # By default, the data format is specified by the `json_data_from` + # method. + def create_request(endpoint, resource, data_fmt = :json_data_from) + req = Net::HTTP::Post.new(endpoint.path) + req["Content-Type"] = "application/vnd.api+json" + req["Authorization"] = "Bearer #{@api_key}" + req["User-Agent"] = "imgix #{@library}-#{@version}" + + if data_fmt.is_a?(Proc) + req.body = data_fmt.call(resource) + elsif data_fmt.is_a?(Symbol) + req.body = send(data_fmt, resource) + else + fmt_arg_error = "`fmt' is required to be of class Symbol or " \ + "Proc but was found to be\n\s\sof class #{data_fmt.class}\n" + raise ArgumentError, fmt_arg_error + end + + req end - def new_prefix - "#{@use_https ? 'https' : 'http'}://#{@host}" + def json_data_from(url) + { + data: { + attributes: { + url: url + }, + type: "purges" + } + }.to_json end - private + def validate_domain! + domain_error = "The :domain option must be specified" + raise ArgumentError, domain_error if @domain.nil? - def validate_host! - 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'\ + 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 + raise ArgumentError, domain_error if @domain.match(DOMAIN_REGEX).nil? end end end