Sha256: f1a83640cbb6e08f5a1029b026143cc09046ae7ffa50e271d4c77ac2d8bf9013

Contents?: true

Size: 1.77 KB

Versions: 23

Compression:

Stored size: 1.77 KB

Contents

require "json"

module Bibliothecary
  module Parsers
    class Conda
      include Bibliothecary::Analyser

      def self.mapping
        {
          match_filename("environment.yml") => {
            parser: :parse_conda,
            kind: "manifest",
          },
          match_filename("environment.yaml") => {
            parser: :parse_conda,
            kind: "manifest",
          },
          match_filename("environment.yml.lock") => {
            parser: :parse_conda_lockfile,
            kind: "lockfile",
          },
          match_filename("environment.yaml.lock") => {
            parser: :parse_conda_lockfile,
            kind: "lockfile",
          },
        }
      end

      def self.parse_conda(info, kind = "manifest")
        dependencies = call_conda_parser_web(info, kind)[kind.to_sym]
        dependencies.map { |dep| dep.merge(type: "runtime") }
      end

      def self.parse_conda_lockfile(info)
        parse_conda(info, "lockfile")
      end

      private_class_method def self.call_conda_parser_web(file_contents, kind)
        host = Bibliothecary.configuration.conda_parser_host
        response = Typhoeus.post(
          "#{host}/parse",
          headers: {
            ContentType: "multipart/form-data",
          },
          body: {
            file: file_contents,
            # Unfortunately we do not get the filename in the mapping parsers, so hardcoding the file name depending on the kind
            filename: kind == "manifest" ? "environment.yml" : "environment.yml.lock",
          }
        )
        raise Bibliothecary::RemoteParsingError.new("Http Error #{response.response_code} when contacting: #{host}/parse", response.response_code) unless response.success?

        JSON.parse(response.body, symbolize_names: true)
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
bibliothecary-7.3.6 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.3.5 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.3.4 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.3.3 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.3.2 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.3.1 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.3.0 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.2.1 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.2.0 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.1.5 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.1.4 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.1.3 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.1.2 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.1.1 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.1.0 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.0.2 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.0.1 lib/bibliothecary/parsers/conda.rb
bibliothecary-7.0.0 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.12.3 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.12.2 lib/bibliothecary/parsers/conda.rb