Sha256: 5e5aaa5d379b2fda14e835a53ba68b43d4c21cd50ff897b4c5e8a99e05ce6e09

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Spandx
  module Gateways
    class PyPI
      class Source
        attr_reader :name, :uri, :verify_ssl

        def initialize(source)
          @name = source['name']
          @uri = URI.parse(source['url'])
          @verify_ssl = source['verify_ssl']
        end

        def host
          @uri.host
        end

        def uri_for(name, version)
          URI.parse("https://#{host}/pypi/#{name}/#{version}/json")
        end

        def lookup(name, version, http: Spandx.http)
          response = http.get(uri_for(name, version))
          response if http.ok?(response)
        end

        class << self
          def sources_from(json)
            meta = json['_meta']
            meta['sources'].map do |hash|
              Gateways::PyPI::Source.new(hash)
            end
          end

          def default
            new(
              'name' => 'pypi',
              'url' => 'https://pypi.org/simple',
              'verify_ssl' => true
            )
          end
        end
      end

      def initialize(sources: [Source.default])
        @sources = sources
      end

      def definition_for(name, version)
        @sources.each do |source|
          response = source.lookup(name, version)
          return JSON.parse(response.body).fetch('info', {}) if response
        end
        {}
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spandx-0.3.0 lib/spandx/gateways/pypi.rb
spandx-0.2.0 lib/spandx/gateways/pypi.rb
spandx-0.1.7 lib/spandx/gateways/pypi.rb
spandx-0.1.6 lib/spandx/gateways/pypi.rb
spandx-0.1.5 lib/spandx/gateways/pypi.rb
spandx-0.1.4 lib/spandx/gateways/pypi.rb
spandx-0.1.3 lib/spandx/gateways/pypi.rb
spandx-0.1.2 lib/spandx/gateways/pypi.rb