Class: LicenseFinder::PyPI

Inherits:
Object
  • Object
show all
Defined in:
lib/license_finder/package_utils/pypi.rb

Constant Summary collapse

CONNECTION_ERRORS =
[
  EOFError,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::EINVAL,
  Net::OpenTimeout,
  Net::ProtocolError,
  Net::ReadTimeout,
  OpenSSL::OpenSSLError,
  OpenSSL::SSL::SSLError,
  SocketError,
  Timeout::Error
].freeze

Class Method Summary collapse

Class Method Details

.definition(name, version) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/license_finder/package_utils/pypi.rb', line 25

def definition(name, version)
  response = request("https://pypi.org/pypi/#{name}/#{version}/json")
  response.is_a?(Net::HTTPSuccess) ? JSON.parse(response.body).fetch('info', {}) : {}
rescue *CONNECTION_ERRORS => e
  raise e, "Unable to read package from pypi.org #{name} #{version}: #{e}" unless @prepare_no_fail

  {}
end

.request(location, limit = 10) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/license_finder/package_utils/pypi.rb', line 34

def request(location, limit = 10)
  uri = URI(location)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  response = http.get(uri.request_uri).response
  response.is_a?(Net::HTTPRedirection) && limit.positive? ? request(response['location'], limit - 1) : response
end