Class: Purl::RegistryURL

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(purl) ⇒ RegistryURL

Returns a new instance of RegistryURL.



410
411
412
# File 'lib/purl/registry_url.rb', line 410

def initialize(purl)
  @purl = purl
end

Class Method Details

.all_route_patternsObject



400
401
402
403
404
405
406
407
408
# File 'lib/purl/registry_url.rb', line 400

def self.all_route_patterns
  result = {}
  REGISTRY_PATTERNS.each do |type, config|
    if config[:route_patterns]
      result[type] = config[:route_patterns]
    end
  end
  result
end

.build_generation_lambda(type, config, default_registry = nil) ⇒ Object



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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/purl/registry_url.rb', line 82

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) ⇒ Object



26
27
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/purl/registry_url.rb', line 26

def self.build_pattern_config(type, config)
  # Get the default registry for this type from parent config
  type_config = load_types_config["types"][type]
  default_registry = type_config["default_registry"]
  
  # Build full URLs from templates if we have a default registry
  route_patterns = []
  if default_registry
    # Add all template variations
    if config["path_template"]
      route_patterns << default_registry + config["path_template"]
    end
    if config["namespace_path_template"]
      route_patterns << default_registry + config["namespace_path_template"]
    end
    if config["version_path_template"]
      route_patterns << default_registry + config["version_path_template"]
    end
    if config["namespace_version_path_template"]
      route_patterns << default_registry + config["namespace_version_path_template"]
    end
  end
  # Fall back to legacy route_patterns if available
  route_patterns = config["route_patterns"] if route_patterns.empty? && config["route_patterns"]
  
  # 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),
    route_patterns: route_patterns,
    reverse_regex: reverse_regex,
    pattern: build_generation_lambda(type, config, default_registry),
    reverse_parser: reverse_regex ? build_reverse_parser(type, config) : nil
  }
end

.build_reverse_parser(type, config) ⇒ Object



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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/purl/registry_url.rb', line 166

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



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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/purl/registry_url.rb', line 319

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
  
  error_message = 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(
    error_message,
    supported_types: REGISTRY_PATTERNS.keys.select { |k| REGISTRY_PATTERNS[k][:reverse_regex] }
  )
end

.generate(purl, base_url: nil) ⇒ Object



307
308
309
# File 'lib/purl/registry_url.rb', line 307

def self.generate(purl, base_url: nil)
  new(purl).generate(base_url: base_url)
end

.load_registry_patternsObject

Load registry patterns from JSON configuration



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/purl/registry_url.rb', line 6

def self.load_registry_patterns
  @registry_patterns ||= begin
    # Load JSON config directly to avoid circular dependency
    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)
    end
    
    patterns
  end
end

.load_types_configObject

Load types config (needed for accessing default_registry)



74
75
76
77
78
79
80
# File 'lib/purl/registry_url.rb', line 74

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



393
394
395
396
397
398
# File 'lib/purl/registry_url.rb', line 393

def self.route_patterns_for(type)
  pattern_config = REGISTRY_PATTERNS[type.to_s.downcase]
  return [] unless pattern_config
  
  pattern_config[:route_patterns] || []
end

.supported_reverse_typesObject



389
390
391
# File 'lib/purl/registry_url.rb', line 389

def self.supported_reverse_types
  REGISTRY_PATTERNS.select { |_, config| config[:reverse_regex] }.keys.sort
end

.supported_typesObject



311
312
313
# File 'lib/purl/registry_url.rb', line 311

def self.supported_types
  REGISTRY_PATTERNS.keys.sort
end

.supports?(type) ⇒ Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/purl/registry_url.rb', line 315

def self.supports?(type)
  REGISTRY_PATTERNS.key?(type.to_s.downcase)
end

Instance Method Details

#generate(base_url: nil) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/purl/registry_url.rb', line 414

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)
    else
      # Use default base URL
      pattern_config[:pattern].call(@purl)
    end
  rescue MissingRegistryInfoError
    raise
  rescue => e
    raise RegistryError, "Failed to generate registry URL for #{@purl.type}: #{e.message}"
  end
end

#generate_with_version(base_url: nil) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/purl/registry_url.rb', line 440

def generate_with_version(base_url: nil)
  registry_url = generate(base_url: base_url)
  
  case @purl.type.downcase
  when "npm"
    @purl.version ? "#{registry_url}/v/#{@purl.version}" : registry_url
  when "pypi"
    @purl.version ? "#{registry_url}#{@purl.version}/" : registry_url
  when "gem"
    @purl.version ? "#{registry_url}/versions/#{@purl.version}" : registry_url
  when "maven"
    @purl.version ? "#{registry_url}/#{@purl.version}" : registry_url
  when "nuget"
    @purl.version ? "#{registry_url}/#{@purl.version}" : registry_url
  else
    # For other types, just return the base URL since version-specific URLs vary
    registry_url
  end
end