Skip to content

Commit

Permalink
minor cosmetic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
frostmark committed Jul 31, 2021
1 parent 540e282 commit 1b6409f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 78 deletions.
126 changes: 65 additions & 61 deletions lib/carrierwave/cloudflare/url.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 21 additions & 17 deletions lib/carrierwave/cloudflare/url/query_string.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1b6409f

Please sign in to comment.