class VtApi::ApiV2::File

Class that represents VT Public API 2.0 for files.

Constants

ID_TYPES

Existing ID types @see id

Attributes

scan_count[R]
scan_date[R]
scans[R]
trigger_count[R]

Public Class Methods

from_response(api_resp) click to toggle source

Shorthand for initialize.

@see initialize

# File lib/vt_api/api/v2/file.rb, line 79
def self.from_response(api_resp)
        # noinspection RubyResolve
        if api_resp.response_code.nil? || (api_resp.response_code < 1)
                nil
        else
                report = new api_resp

                report
        end
end
new(api_resp) click to toggle source

Initializes new object from VT API response. @note Direct creation of object cas cause errors since it doesn't contain any validity checks.

Use predefined API method bindings instead.

@see .report @see .schedule_scan @see .schedule_rescan

@param [OpenStruct] api_resp

# File lib/vt_api/api/v2/file.rb, line 101
def initialize(api_resp)
        load_ids! api_resp
        load_meta! api_resp
        load_scans! api_resp
end
report(resource:) click to toggle source

@see developers.virustotal.com/v2.0/reference#file-report

@param [String] resource @return [File] File report if present, nil otherwise.

# File lib/vt_api/api/v2/file.rb, line 39
def report(resource:)
        resp = ApiV2.provider.request 'file.report', apikey: VtApi.options.token, resource: resource
        from_response resp
end
schedule_rescan(resource:) click to toggle source

@see developers.virustotal.com/v2.0/reference#file-rescan

@param [String] resource @return [String] Scan ID.

# File lib/vt_api/api/v2/file.rb, line 48
def schedule_rescan(resource:)
        resp = ApiV2.provider.request 'file.rescan', apikey: VtApi.options.token, resource: resource
        resp.scan_id
end
schedule_scan(file:) click to toggle source

@see developers.virustotal.com/v2.0/reference#file-scan

@param [String|Pathname|UploadIO] file @return [String] Scan ID.

# File lib/vt_api/api/v2/file.rb, line 57
def schedule_scan(file:)
        file = file_to_io file if file.is_a?(String) || file.is_a?(Pathname)

        resp = ApiV2.provider.request 'file.scan', apikey: VtApi.options.token, file: file
        resp.scan_id
end

Private Class Methods

file_to_io(file) click to toggle source
# File lib/vt_api/api/v2/file.rb, line 66
def file_to_io(file)
        file = ::File.realpath file
        file = ::File.absolute_path file

        mime = MimeMagic.by_path file

        UploadIO.new file, mime
end

Public Instance Methods

id(type = :id) click to toggle source

Get file identifier. Since VT API offers many ways to identify the file, you can supply ID type to get specific file.

@see ID_TYPES

@param [Symbol|String] type @return [String] ID string of specified type.

# File lib/vt_api/api/v2/file.rb, line 119
def id(type = :id)
        raise ArgumentError, "There is no such id type (#{type}) in VT API 2.0" unless ID_TYPES.include? type.to_sym

        @ids[type.to_sym]
end
threat_level() click to toggle source

@return [Fixnum]

# File lib/vt_api/api/v2/file.rb, line 108
def threat_level
        @trigger_count.to_f / @scan_count
end

Private Instance Methods

load_ids!(api_resp) click to toggle source

noinspection RubyResolve

# File lib/vt_api/api/v2/file.rb, line 128
def load_ids!(api_resp)
        @ids = OpenStruct.new(
                scan_id:     api_resp.scan_id,
                md5:         api_resp.md5,
                sha1:        api_resp.sha1,
                sha256:      api_resp.sha256,
                resource_id: api_resp.resource
        )
end
load_meta!(api_resp) click to toggle source
# File lib/vt_api/api/v2/file.rb, line 138
def load_meta!(api_resp)
        @scan_date = DateTime.parse(api_resp.scan_date)
        @permalink = URI(api_resp.permalink)
end
load_scans!(api_resp) click to toggle source

noinspection RubyResolve

# File lib/vt_api/api/v2/file.rb, line 144
def load_scans!(api_resp)
        @scan_count    = api_resp.total
        @trigger_count = api_resp.positives

        @scans = api_resp.scans.to_h.to_a.map do |key, value|
                [key, AvResult.new(key.to_s, value)]
        end.to_h
end