# # 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