Sha256: 452b258816b6e7ed0d4926cd0a36606ba64dfdafe7253e52493b073e230b0a2a

Contents?: true

Size: 1.37 KB

Versions: 12

Compression:

Stored size: 1.37 KB

Contents

#
# Standard libraries
#
require 'digest'

#
# Third-party libraries
#
require 'http'

module Hrw
  #
  # Used to interact with Horus server
  #
  class API
    #
    # Errors
    #
    class HTTPCodeError < StandardError; end

    # Class constructor
    #
    # @param [String] url API remote address
    def initialize(url)
      @submit_url = url.chomp('/') + '/dependency'
      @retrieve_url = url.chomp('/') + '/ancestry'
    end

    # Submit dependencies to server
    #
    # @param [Array] dependencies
    # @param [String] pkg_manager
    # @return [String] Checksum for those dependencies
    def submit(dependencies, pkg_manager)
      body = {
        hash: _calc_hash(dependencies),
        pkg_manager: {
          name: pkg_manager
        },
        packages: dependencies
      }

      res = HTTP.post(@submit_url, json: body)
      raise HTTPCodeError, "#{res.code}: #{res.body}" if res.code != 200

      body[:hash]
    end

    # Used to retrieve scan results
    #
    # @param [String] hash
    # @return [Hash] result
    def retrieve(hash)
      res = HTTP.get(@retrieve_url + "/#{hash}")
      raise HTTPCodeError, "#{res.code}: #{res.body}" if res.code != 200

      res.parse
    end

    private

    def _calc_hash(dependencies)
      str = dependencies.map { |d| "#{d[:name]}@#{d[:version]}" }.sort.join('|')
      Digest::SHA256.hexdigest(str)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hrw-0.3.9 lib/hrw/api.rb
hrw-0.3.8 lib/hrw/api.rb
hrw-0.3.7 lib/hrw/api.rb
hrw-0.3.6 lib/hrw/api.rb
hrw-0.3.5 lib/hrw/api.rb
hrw-0.3.4 lib/hrw/api.rb
hrw-0.3.3 lib/hrw/api.rb
hrw-0.3.2 lib/hrw/api.rb
hrw-0.3.1 lib/hrw/api.rb
hrw-0.3.0 lib/hrw/api.rb
hrw-0.2.3 lib/hrw/api.rb
hrw-0.2.2 lib/hrw/api.rb