Sha256: 69b2c566e717d4b16164f247c9887f2ca0021d41194e0d2cf45c9ee947f17d4d

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 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
    # @return [String] Checksum for those dependencies
    def submit(dependencies)
      body = {
        hash: _calc_hash(dependencies),
        pkg_manager: {
          name: :rubygems
        },
        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

3 entries across 3 versions & 1 rubygems

Version Path
hrw-0.2.1 lib/hrw/api.rb
hrw-0.2.0 lib/hrw/api.rb
hrw-0.1.0 lib/hrw/api.rb