Skip to content

Commit

Permalink
Merge pull request #2 from jshack3r/master
Browse files Browse the repository at this point in the history
Updated API calls
  • Loading branch information
graysonchao committed Jul 1, 2015
2 parents fb137ed + faf77ee commit ee35ef7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/scan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ def request

request.on_complete do |r|
@data_id = JSON.parse(r.body)["data_id"]
@rest_ip = JSON.parse(r.body)["rest_ip"].split(":")[0] + '/v2/file'
end

request
end

# Returns true iff the Metascan virus scan found no threats.
# If POLL is true (false by default) then retrieve_results first.
def clean?(poll: false)
self.results(poll: poll)["scan_results"]["scan_all_result_i"] == 0
if self.results(poll: poll)["scan_results"]["progress_percentage"] < 100 then
nil
else
self.results(poll: poll)["scan_results"]["scan_all_result_i"] == 0
end
end

# Only useful for testing.
Expand All @@ -69,7 +73,7 @@ def results(poll: false)
# my Batch runs me)
def retrieve_results
request = Typhoeus::Request.new(
Metascan::PATHS[:results_by_data_id] + @data_id,
@rest_ip + '/' + @data_id,
headers: {
'apikey' => @client.api_key
},
Expand Down
6 changes: 3 additions & 3 deletions metascan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module Metascan

# Paths to use for api calls.
PATHS = {
:scan_file => "https://api.metascan-online.com/v1/file",
:results_by_data_id => "https://api.metascan-online.com/v1/file/",
:results_by_file_hash => "https://api.metascan-online.com/v1/hash/"
:scan_file => "https://scan.metascan-online.com/v2/file",
:results_by_data_id => "https://scan.metascan-online.com/v2/file",
:results_by_file_hash => "https://hashlookup.metascan-online.com/v2/hash"
}

# a miserable pile of library classes!
Expand Down
33 changes: 33 additions & 0 deletions scan-example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative 'metascan'

### Create a new scan object ###
scanner = Metascan::Client.new(MY_API_KEY)

### Scan a single file ###
filename = '/bin/bash'

print "Scanning file: #{filename} "
results = scanner.scan_file(filename)

status = nil
until status != nil do
print "."
status = results.clean?(poll: true)
sleep 5
end
puts ' clean: ' + results.clean?.to_s


### Scan multiple files

filenamesArray = ['/bin/cat', '/bin/chmod', '/bin/less']
print "Scanning multiple files "
results = scanner.scan_batch(filenamesArray)

status = nil
until status != nil do
print "."
status = results.clean?
sleep 5
end
puts ' clean: ' + results.clean?.to_s

0 comments on commit ee35ef7

Please sign in to comment.