How to scan a file for viruses in Ruby

Viruses can be a huge problem, but how can they be dealt with easily and reliably? Well, there’s an API for that. Start by adding the virus scan client to your Gemfile.

gem 'cloudmersive-virus-scan-api-client', '~> 1.3.0'

Then run scan_file:

# load the gem
require 'cloudmersive-virus-scan-api-client'
# setup authorization
CloudmersiveVirusScanApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveVirusScanApiClient::ScanApi.newinput_file = File.new("/path/to/file") # File | Input file to perform the operation on.begin
#Scan a file for viruses
result = api_instance.scan_file(input_file)
p result
rescue CloudmersiveVirusScanApiClient::ApiError => e
puts "Exception when calling ScanApi->scan_file: #{e}"
end

And that’s really all there is to it. Your output will consist of positive/negative and the item’s identity in the case of a positive.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store