Class: Purl::RegistryURL
- Inherits:
-
Object
- Object
- Purl::RegistryURL
- Defined in:
- lib/purl/registry_url.rb
Constant Summary collapse
- REGISTRY_PATTERNS =
Registry patterns loaded from JSON configuration
load_registry_patterns.freeze
Class Method Summary collapse
- .all_route_patterns ⇒ Object
- .build_generation_lambda(type, config, default_registry = nil) ⇒ Object
- .build_pattern_config(type, config, type_config) ⇒ Object
- .build_reverse_parser(type, config) ⇒ Object
- .from_url(registry_url, type: nil) ⇒ Object
- .generate(purl, base_url: nil) ⇒ Object
-
.load_registry_patterns ⇒ Object
Load registry patterns from JSON configuration.
-
.load_types_config ⇒ Object
Load types config (needed for accessing default_registry).
- .route_patterns_for(type) ⇒ Object
- .supported_reverse_types ⇒ Object
- .supported_types ⇒ Object
- .supports?(type) ⇒ Boolean
Instance Method Summary collapse
- #generate(base_url: nil) ⇒ Object
- #generate_with_version(base_url: nil) ⇒ Object
-
#initialize(purl) ⇒ RegistryURL
constructor
A new instance of RegistryURL.
Constructor Details
#initialize(purl) ⇒ RegistryURL
Returns a new instance of RegistryURL.
419 420 421 |
# File 'lib/purl/registry_url.rb', line 419 def initialize(purl) @purl = purl end |
Class Method Details
.all_route_patterns ⇒ Object
405 406 407 408 409 410 411 412 |
# File 'lib/purl/registry_url.rb', line 405 def self.all_route_patterns result = {} REGISTRY_PATTERNS.each do |type, config| patterns = route_patterns_for(type) result[type] = patterns unless patterns.empty? end result end |
.build_generation_lambda(type, config, default_registry = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/purl/registry_url.rb', line 68 def self.build_generation_lambda(type, config, default_registry = nil) # Use base_url from config, or build from default_registry + path_template base if config["base_url"] base_url = config["base_url"] elsif default_registry && config["path_template"] # Extract the base path from the template (everything before first :parameter) base_path = config["path_template"].split('/:').first base_url = default_registry + base_path else return nil end case type when "npm" ->(purl) do if purl.namespace "#{base_url}/#{purl.namespace}/#{purl.name}" else "#{base_url}/#{purl.name}" end end when "composer", "maven", "swift" ->(purl) do if purl.namespace "#{base_url}/#{purl.namespace}/#{purl.name}" else raise MissingRegistryInfoError.new( "#{type.capitalize} packages require a namespace", type: purl.type, missing: "namespace" ) end end when "golang" ->(purl) do if purl.namespace "#{base_url}/#{purl.namespace}/#{purl.name}" else "#{base_url}/#{purl.name}" end end when "pypi" ->(purl) { "#{base_url}/#{purl.name}/" } when "hackage" ->(purl) do if purl.version "#{base_url}/#{purl.name}-#{purl.version}" else "#{base_url}/#{purl.name}" end end when "deno" ->(purl) do if purl.version "#{base_url}/#{purl.name}@#{purl.version}" else "#{base_url}/#{purl.name}" end end when "clojars" ->(purl) do if purl.namespace "#{base_url}/#{purl.namespace}/#{purl.name}" else "#{base_url}/#{purl.name}" end end when "elm" ->(purl) do if purl.namespace version = purl.version || "latest" "#{base_url}/#{purl.namespace}/#{purl.name}/#{version}" else raise MissingRegistryInfoError.new( "Elm packages require a namespace", type: purl.type, missing: "namespace" ) end end else ->(purl) { "#{base_url}/#{purl.name}" } end end |
.build_pattern_config(type, config, type_config) ⇒ Object
28 29 30 31 32 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 |
# File 'lib/purl/registry_url.rb', line 28 def self.build_pattern_config(type, config, type_config) # Get the default registry for this type from the extended config default_registry = type_config["default_registry"] # Route patterns are replaced by URI templates # Build reverse regex from template or use legacy format reverse_regex = nil if config["reverse_regex"] if config["reverse_regex"].start_with?("/") && default_registry # Domain-agnostic pattern - combine with default registry domain domain_pattern = default_registry.sub(/^https?:\/\//, '').gsub('.', '\\.') reverse_regex = Regexp.new("^https?://#{domain_pattern}" + config["reverse_regex"]) else # Legacy full pattern reverse_regex = Regexp.new(config["reverse_regex"]) end end { base_url: config["base_url"] || (default_registry ? default_registry + config["path_template"]&.split('/:').first : nil), reverse_regex: reverse_regex, pattern: build_generation_lambda(type, config, default_registry), reverse_parser: reverse_regex ? build_reverse_parser(type, config) : nil, uri_template: config["uri_template"] ? Addressable::Template.new(config["uri_template"]) : nil, uri_template_no_namespace: config["uri_template_no_namespace"] ? Addressable::Template.new(config["uri_template_no_namespace"]) : nil, uri_template_with_version: config["uri_template_with_version"] ? Addressable::Template.new(config["uri_template_with_version"]) : nil, uri_template_with_version_no_namespace: config["uri_template_with_version_no_namespace"] ? Addressable::Template.new(config["uri_template_with_version_no_namespace"]) : nil } end |
.build_reverse_parser(type, config) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/purl/registry_url.rb', line 152 def self.build_reverse_parser(type, config) case type when "npm" ->(match) do namespace = match[1] # @scope or nil name = match[2] version = match[3] # from /v/version or nil { type: type, namespace: namespace, name: name, version: version } end when "gem" ->(match) do name = match[1] version = match[2] # from /versions/version or nil { type: type, namespace: nil, name: name, version: version } end when "maven" ->(match) do namespace = match[1] name = match[2] version = match[3] { type: type, namespace: namespace, name: name, version: version } end when "pypi" ->(match) do name = match[1] version = match[2] unless match[2] == name # avoid duplicate name as version { type: type, namespace: nil, name: name, version: version } end when "cargo" ->(match) do name = match[1] { type: type, namespace: nil, name: name, version: nil } end when "golang" ->(match) do if match[1] && match[2] # Has namespace: pkg.go.dev/namespace/name namespace = match[1] name = match[2] else # No namespace: pkg.go.dev/name namespace = nil name = match[1] || match[2] end { type: type, namespace: namespace, name: name, version: nil } end when "hackage" ->(match) do name = match[1] version = match[2] # from name-version pattern { type: type, namespace: nil, name: name, version: version } end when "deno" ->(match) do name = match[1] version = match[2] # from @version pattern { type: type, namespace: nil, name: name, version: version } end when "homebrew" ->(match) do name = match[1] { type: type, namespace: nil, name: name, version: nil } end when "elm" ->(match) do namespace = match[1] name = match[2] version = match[3] unless match[3] == "latest" { type: type, namespace: namespace, name: name, version: version } end when "cocoapods" ->(match) do name = match[1] { type: type, namespace: nil, name: name, version: nil } end when "composer" ->(match) do namespace = match[1] name = match[2] { type: type, namespace: namespace, name: name, version: nil } end when "conda" ->(match) do name = match[1] { type: type, namespace: nil, name: name, version: nil } end when "cpan" ->(match) do name = match[1] { type: type, namespace: nil, name: name, version: nil } end when "hex" ->(match) do name = match[1] { type: type, namespace: nil, name: name, version: nil } end when "nuget" ->(match) do name = match[1] version = match[2] # from /version pattern { type: type, namespace: nil, name: name, version: version } end when "pub" ->(match) do name = match[1] { type: type, namespace: nil, name: name, version: nil } end when "swift" ->(match) do namespace = match[1] name = match[2] { type: type, namespace: namespace, name: name, version: nil } end when "bioconductor" ->(match) do name = match[1] { type: type, namespace: nil, name: name, version: nil } end when "clojars" ->(match) do if match[1] && match[2] # Has namespace: clojars.org/namespace/name namespace = match[1] name = match[2] else # No namespace: clojars.org/name namespace = nil name = match[1] || match[2] end { type: type, namespace: namespace, name: name, version: nil } end else ->(match) do { type: type, namespace: nil, name: match[1], version: nil } end end end |
.from_url(registry_url, type: nil) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/purl/registry_url.rb', line 305 def self.from_url(registry_url, type: nil) # If type is specified, try that specific type first with domain-agnostic parsing if type normalized_type = type.to_s.downcase config = REGISTRY_PATTERNS[normalized_type] if config && config[:reverse_regex] && config[:reverse_parser] # Create a domain-agnostic version of the regex by replacing the base domain original_regex = config[:reverse_regex].source # For simplified JSON patterns that start with /, create domain-agnostic regex domain_agnostic_regex = nil if original_regex.start_with?("/") # Domain-agnostic pattern - match any domain with this path domain_agnostic_regex = Regexp.new("^https?://[^/]+" + original_regex) else # Legacy full regex pattern if original_regex =~ /\^https?:\/\/[^\/]+(.+)$/ path_pattern = $1 # Create domain-agnostic regex that matches any domain with the same path structure domain_agnostic_regex = Regexp.new("^https?://[^/]+" + path_pattern) end end if domain_agnostic_regex match = registry_url.match(domain_agnostic_regex) if match parsed_data = config[:reverse_parser].call(match) return PackageURL.new( type: parsed_data[:type], namespace: parsed_data[:namespace], name: parsed_data[:name], version: parsed_data[:version] ) end end end # If specified type didn't work, fall through to normal domain-matching logic end # Try to parse the registry URL back into a PURL using domain matching REGISTRY_PATTERNS.each do |registry_type, config| next unless config[:reverse_regex] && config[:reverse_parser] match = registry_url.match(config[:reverse_regex]) if match parsed_data = config[:reverse_parser].call(match) return PackageURL.new( type: parsed_data[:type], namespace: parsed_data[:namespace], name: parsed_data[:name], version: parsed_data[:version] ) end end = if type "Unable to parse registry URL: #{registry_url} as type '#{type}'. " + "URL structure doesn't match expected pattern for this type." else "Unable to parse registry URL: #{registry_url}. No matching pattern found." end raise UnsupportedTypeError.new( , supported_types: REGISTRY_PATTERNS.keys.select { |k| REGISTRY_PATTERNS[k][:reverse_regex] } ) end |
.generate(purl, base_url: nil) ⇒ Object
293 294 295 |
# File 'lib/purl/registry_url.rb', line 293 def self.generate(purl, base_url: nil) new(purl).generate(base_url: base_url) end |
.load_registry_patterns ⇒ Object
Load registry patterns from JSON configuration
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/purl/registry_url.rb', line 8 def self.load_registry_patterns @registry_patterns ||= begin # Load extended registry configs config_path = File.join(__dir__, "..", "..", "purl-types.json") require "json" config = JSON.parse(File.read(config_path)) patterns = {} config["types"].each do |type, type_config| # Only process types that have registry_config next unless type_config["registry_config"] registry_config = type_config["registry_config"] patterns[type] = build_pattern_config(type, registry_config, type_config) end patterns end end |
.load_types_config ⇒ Object
Load types config (needed for accessing default_registry)
60 61 62 63 64 65 66 |
# File 'lib/purl/registry_url.rb', line 60 def self.load_types_config @types_config ||= begin config_path = File.join(__dir__, "..", "..", "purl-types.json") require "json" JSON.parse(File.read(config_path)) end end |
.route_patterns_for(type) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/purl/registry_url.rb', line 379 def self.route_patterns_for(type) pattern_config = REGISTRY_PATTERNS[type.to_s.downcase] return [] unless pattern_config # Generate route patterns from URI templates patterns = [] if pattern_config[:uri_template] patterns << uri_template_to_route_pattern(pattern_config[:uri_template]) end if pattern_config[:uri_template_no_namespace] patterns << uri_template_to_route_pattern(pattern_config[:uri_template_no_namespace]) end if pattern_config[:uri_template_with_version] patterns << uri_template_to_route_pattern(pattern_config[:uri_template_with_version]) end if pattern_config[:uri_template_with_version_no_namespace] patterns << uri_template_to_route_pattern(pattern_config[:uri_template_with_version_no_namespace]) end patterns.uniq end |
.supported_reverse_types ⇒ Object
375 376 377 |
# File 'lib/purl/registry_url.rb', line 375 def self.supported_reverse_types REGISTRY_PATTERNS.select { |_, config| config[:reverse_regex] }.keys.sort end |
.supported_types ⇒ Object
297 298 299 |
# File 'lib/purl/registry_url.rb', line 297 def self.supported_types REGISTRY_PATTERNS.keys.sort end |
.supports?(type) ⇒ Boolean
301 302 303 |
# File 'lib/purl/registry_url.rb', line 301 def self.supports?(type) REGISTRY_PATTERNS.key?(type.to_s.downcase) end |
Instance Method Details
#generate(base_url: nil) ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/purl/registry_url.rb', line 423 def generate(base_url: nil) pattern_config = REGISTRY_PATTERNS[@purl.type.downcase] unless pattern_config raise UnsupportedTypeError.new( "No registry URL pattern defined for type '#{@purl.type}'. Supported types: #{self.class.supported_types.join(", ")}", type: @purl.type, supported_types: self.class.supported_types ) end begin if base_url # Use custom base URL with the same URL structure generate_with_custom_base_url(base_url, pattern_config) elsif pattern_config[:uri_template] # Use URI template if available template = select_uri_template(pattern_config, include_version: false) generate_with_uri_template(template) else # Fall back to legacy lambda pattern pattern_config[:pattern].call(@purl) end rescue MissingRegistryInfoError raise rescue => e raise RegistryError, "Failed to generate registry URL for #{@purl.type}: #{e.}" end end |
#generate_with_version(base_url: nil) ⇒ Object
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/purl/registry_url.rb', line 453 def generate_with_version(base_url: nil) return generate(base_url: base_url) unless @purl.version pattern_config = REGISTRY_PATTERNS[@purl.type.downcase] if base_url # Use custom base URL with version generate_with_custom_base_url_and_version(base_url, pattern_config) elsif pattern_config[:uri_template_with_version] || pattern_config[:uri_template] # Use version-specific URI template if available template = select_uri_template(pattern_config, include_version: true) generate_with_uri_template(template, include_version: true) else # Fall back to legacy version handling registry_url = generate(base_url: base_url) case @purl.type.downcase when "npm" "#{registry_url}/v/#{@purl.version}" when "pypi" "#{registry_url}#{@purl.version}/" when "gem" "#{registry_url}/versions/#{@purl.version}" when "maven" "#{registry_url}/#{@purl.version}" when "nuget" "#{registry_url}/#{@purl.version}" else registry_url end end end |