From 1b6409f0357e8051436928d0cf672ebf7270587f Mon Sep 17 00:00:00 2001 From: Mark Frost Date: Sat, 31 Jul 2021 15:53:29 +0500 Subject: [PATCH] minor cosmetic fixes --- lib/carrierwave/cloudflare/url.rb | 126 +++++++++--------- .../cloudflare/url/query_string.rb | 38 +++--- 2 files changed, 86 insertions(+), 78 deletions(-) diff --git a/lib/carrierwave/cloudflare/url.rb b/lib/carrierwave/cloudflare/url.rb index b3657bd..2e5fbae 100644 --- a/lib/carrierwave/cloudflare/url.rb +++ b/lib/carrierwave/cloudflare/url.rb @@ -1,86 +1,90 @@ # frozen_string_literal: true -require 'carrierwave/cloudflare/url/query_string' +require "carrierwave/cloudflare/url/query_string" -module CarrierWave::Cloudflare - module URL - ALLOWED_OPTIONS = %w[width height dpr fit gravity quality format onerror metadata].freeze +module CarrierWave + module Cloudflare + module URL + ALLOWED_OPTIONS = %w[width height dpr fit gravity quality format onerror metadata].freeze - module_function + module_function - def should_modify_path? - ::CarrierWave::Cloudflare.cloudflare_transform - end + def should_modify_path? + ::CarrierWave::Cloudflare.cloudflare_transform + end - def extract(url) - uri = URI.parse(url) - options = {} + def extract(url) + uri = URI.parse(url) + options = {} - if should_modify_path? - if %r{/cdn-cgi/image/([^/]+)(/.*)} =~ uri.path - formatted = $LAST_MATCH_INFO[1] - original_path = $LAST_MATCH_INFO[2] + if should_modify_path? + if %r{/cdn-cgi/image/([^/]+)(/.*)} =~ uri.path + formatted = $LAST_MATCH_INFO[1] + original_path = $LAST_MATCH_INFO[2] - options = parse_options(formatted) - uri.path = original_path - end - else - query = QueryString.new(uri.query) + options = parse_options(formatted) + uri.path = original_path + end + else + query = QueryString.new(uri.query) - if query.key?('cdn-cgi') - options = parse_options(query['cdn-cgi'], separator: '.', assigner: '-') + if query.key?("cdn-cgi") + options = parse_options(query["cdn-cgi"], separator: ".", assigner: "-") + end end - end - [uri, options] - end + [uri, options] + end - def transform(url, **options) - uri, existing_opts = extract(url) - options = existing_opts.merge(options.transform_keys(&:to_s)) + def transform(url, **options) + uri, existing_opts = extract(url) + options = existing_opts.merge(options.transform_keys(&:to_s)) - pairs = sanitized_options(options) + pairs = sanitized_options(options) - if pairs.empty? - url - else - append_options!(uri, pairs) - uri.to_s + if pairs.empty? + url + else + append_options!(uri, pairs) + uri.to_s + end end - end - def append_options!(uri, options) - if should_modify_path? - segment = '/cdn-cgi/image/' + options.map { |k, v| "#{k}=#{v}" }.join(',') - uri.path = segment + uri.path - else - uri.query = QueryString.new(uri.query).tap do |params| - # the format is "width-500.height.200", only safe symbols are used - param_with_options = options.map { |k, v| "#{k}-#{v}" }.join('.') - - params['cdn-cgi'] = param_with_options - end.to_query - end - end + def append_options!(uri, options) + if should_modify_path? + query = options.map { |k, v| "#{k}=#{v}" }.join(",") + + segment = "/cdn-cgi/image/#{query}" + uri.path = segment + uri.path + else + uri.query = QueryString.new(uri.query).tap do |params| + # the format is "width-500.height.200", only safe symbols are used + param_with_options = options.map { |k, v| "#{k}-#{v}" }.join(".") - def sanitized_options(options) - ordered = options.map do |k, v| - idx = ALLOWED_OPTIONS.index(k) - [idx, [k, v]] + params["cdn-cgi"] = param_with_options + end.to_query + end end - filtered = ordered.select { |i,| i }.sort + def sanitized_options(options) + ordered = options.map do |k, v| + idx = ALLOWED_OPTIONS.index(k) + [idx, [k, v]] + end + + filtered = ordered.select { |i,| i }.sort - filtered.map do |_, (k, v)| - v = v.join('x') if v.is_a?(Array) - [k, v] + filtered.map do |_, (k, v)| + v = v.join("x") if v.is_a?(Array) + [k, v] + end end - end - # converts strings "w=foo,h=bar" into hashes - def parse_options(str, separator: ',', assigner: '=') - kv = str.split(separator).map { |s| s.strip.split(assigner) } - Hash[kv] + # converts strings "w=foo,h=bar" into hashes + def parse_options(str, separator: ",", assigner: "=") + kv = str.split(separator).map { |s| s.strip.split(assigner) } + Hash[kv] + end end end end diff --git a/lib/carrierwave/cloudflare/url/query_string.rb b/lib/carrierwave/cloudflare/url/query_string.rb index cc35d89..927341a 100644 --- a/lib/carrierwave/cloudflare/url/query_string.rb +++ b/lib/carrierwave/cloudflare/url/query_string.rb @@ -1,26 +1,30 @@ # frozen_string_literal: true -require 'rack/utils' +require "rack/utils" -module CarrierWave::Cloudflare::URL - class QueryString < Hash - Parser = Object.new.tap do |obj| - obj.extend(Rack::Utils) +module CarrierWave + module Cloudflare + module URL + class QueryString < Hash + Parser = Object.new.tap do |obj| + obj.extend(Rack::Utils) - # these methods are private in Rack::Utils - obj.singleton_class.instance_eval { public :build_query, :parse_query } - end + # these methods are private in Rack::Utils + obj.singleton_class.instance_eval { public :build_query, :parse_query } + end - def initialize(query = '') - super() - merge!(Parser.parse_query(query)) - end + def initialize(query = "") + super() + merge!(Parser.parse_query(query)) + end - def to_query - result = Parser.build_query(self) - result unless result.empty? - end + def to_query + result = Parser.build_query(self) + result unless result.empty? + end - alias to_s to_query + alias to_s to_query + end + end end end