Class: Fileboost::UrlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/fileboost/url_builder.rb

Constant Summary collapse

TRANSFORMATION_PARAMS =

Supported transformation parameters for Fileboost.dev service

%w[
  w width
  h height
  q quality
  f format
  b blur
  br brightness
  c contrast
  r rotation
  fit
].freeze
PARAM_ALIASES =

Parameter aliases for convenience

{
  "width" => "w",
  "height" => "h",
  "quality" => "q",
  "format" => "f",
  "blur" => "b",
  "brightness" => "br",
  "contrast" => "c",
  "rotation" => "r"
}.freeze
RESIZE_PARAMS =

Valid resize parameter keys

%w[w width h height q quality f format b blur br brightness c contrast r rotation fit].freeze

Class Method Summary collapse

Class Method Details

.build_url(asset, **options) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fileboost/url_builder.rb', line 33

def self.build_url(asset, **options)
  raise ConfigurationError, "Invalid configuration" unless Fileboost.config.valid?

  asset_path = extract_asset_path(asset)
  raise AssetPathExtractionError, "Unable to extract asset path" unless !asset_path.nil? && !asset_path.empty?

  project_id = Fileboost.config.project_id
  base_url = Fileboost.config.base_url

  # Build the full asset URL path for Fileboost.dev service
  full_path = "/#{project_id}#{asset_path}"

  # Extract and normalize transformation parameters
  transformation_params = extract_transformation_params(asset, options)

  # Separate disposition from transformation params for signature generation
  disposition = transformation_params.delete("disposition")

  # Generate HMAC signature for secure authentication (excluding disposition)
  signature = Fileboost::SignatureGenerator.generate(
    asset_path: asset_path,
    params: transformation_params
  )

  raise SignatureGenerationError, "Failed to generate signature" unless signature

  # Add signature and disposition back to final URL parameters
  all_params = transformation_params.merge("sig" => signature)
  all_params["disposition"] = disposition if disposition

  # Build final URL
  uri = URI.join(base_url, full_path)
  uri.query = URI.encode_www_form(all_params) unless all_params.empty?

  uri.to_s
end