Sha256: 7463ceac8bc101bd1cb2fe9ac16bf3a343f21c699bad6695517eb2eb02371d9e

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Spandx
  module Python
    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))
        if http.ok?(response)
          JSON.parse(response.body)
        else
          {}
        end
      end

      def ==(other)
        name == other.name &&
          uri.to_s == other.uri.to_s &&
          verify_ssl == other.verify_ssl
      end

      def eql(other)
        self == other
      end

      class << self
        def sources_from(json)
          meta = json['_meta']
          meta['sources'].map do |hash|
            new(hash)
          rescue URI::InvalidURIError
            default
          end
        end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spandx-0.13.4 lib/spandx/python/source.rb
spandx-0.13.3 lib/spandx/python/source.rb