Class: Package::Audit::Npm::NpmMetaData

Inherits:
Object
  • Object
show all
Defined in:
lib/package/audit/npm/npm_meta_data.rb

Constant Summary collapse

REGISTRY_URL =
'https://registry.npmjs.org'

Instance Method Summary collapse

Constructor Details

#initialize(packages) ⇒ NpmMetaData

Returns a new instance of NpmMetaData.



10
11
12
# File 'lib/package/audit/npm/npm_meta_data.rb', line 10

def initialize(packages)
  @packages = packages
end

Instance Method Details

#fetchObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/package/audit/npm/npm_meta_data.rb', line 14

def fetch # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  threads = @packages.map do |package|
    Thread.new do
      response = Net::HTTP.get_response(URI.parse("#{REGISTRY_URL}/#{package.name}"))
      raise "Unable to fetch meta data for #{package.name} from #{REGISTRY_URL} (#{response.class})" unless
        response.is_a?(Net::HTTPSuccess)

      json_package = JSON.parse(response.body, symbolize_names: true)
      (package, json_package)
    rescue StandardError => e
      Thread.current[:exception] = e
    end
  end
  threads.each do |thread|
    thread.join
    raise thread[:exception] if thread[:exception]
  end
  @packages
end