Sha256: 6279e181464d6ea03f4bfde0edf7e0cf1ce2e9622b034f6f2ee8e734d133c83d

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 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)
          "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

9 entries across 9 versions & 1 rubygems

Version Path
spandx-0.10.1 lib/spandx/gateways/pypi.rb
spandx-0.10.0 lib/spandx/gateways/pypi.rb
spandx-0.9.0 lib/spandx/gateways/pypi.rb
spandx-0.8.0 lib/spandx/gateways/pypi.rb
spandx-0.7.0 lib/spandx/gateways/pypi.rb
spandx-0.6.0 lib/spandx/gateways/pypi.rb
spandx-0.5.0 lib/spandx/gateways/pypi.rb
spandx-0.4.1 lib/spandx/gateways/pypi.rb
spandx-0.4.0 lib/spandx/gateways/pypi.rb