Class: VtAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/vtapi/api.rb,
lib/vtapi/response.rb,
lib/vtapi/exception.rb

Defined Under Namespace

Classes: AuthError, ExceedAPILimit, Response

Constant Summary collapse

BASE_URL =

base URL of the VirusTotal Public API v2.0

'https://www.virustotal.com/vtapi/v2/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey) ⇒ VtAPI

Returns a new instance of VtAPI.



11
12
13
# File 'lib/vtapi/api.rb', line 11

def initialize(apikey)
  @apikey = apikey
end

Instance Attribute Details

#apikeyObject (readonly)

Returns the value of attribute apikey.



9
10
11
# File 'lib/vtapi/api.rb', line 9

def apikey
  @apikey
end

Instance Method Details

#file_report(resource) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/vtapi/api.rb', line 34

def file_report(resource)
  if resource.is_a? Array
    raise 'limit is up to 4 items' if resource.size > 4
    resource = resource.join(', ')
  end
  http_post('file/report', resource: resource)
end

#file_rescan(resource) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/vtapi/api.rb', line 26

def file_rescan(resource)
  if resource.is_a? Array
    raise 'limit is up to 25 items' if resource.size > 25
    resource = resource.join(', ')
  end
  http_post('file/rescan', resource: resource)
end

#file_scan(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/vtapi/api.rb', line 15

def file_scan(data)
  # TODO: set filename or file path
  tmp = Tempfile.open('tmp')
  tmp.write data
  def tmp.content_type
    'application/octet-stream'
  end
  tmp.pos=0
  http_post('file/scan', file: tmp, multipart: true)
end

#http_post(path, params = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vtapi/api.rb', line 58

def http_post(path, params = {})
  uri = BASE_URL + path
  params['apikey'] = @apikey
  resp = RestClient.post(uri, params) do |resp, req, result, &block|
    case resp.code
    when 204
      raise ExceedAPILimit, "you exceed the public API request rate limit: key[#{@apikey}]"
    when 403
      raise AuthError, "you do not have the required priviledges: key[#{@apikey}]"
    else
      resp.return!(req, result, &block)
    end
  end
  Response.parse(resp.body)
end

#url_report(url) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/vtapi/api.rb', line 50

def url_report(url)
  if url.is_a? Array
    raise 'limit is up to 4 items' if url.size > 4
    url = url.join(", ")
  end
  http_post('url/report', resource: url)
end

#url_scan(url) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/vtapi/api.rb', line 42

def url_scan(url)
  if url.is_a? Array
    raise 'limit is up to 4 items' if url.size > 4
    url = url.join("\n")
  end
  http_post('url/scan', url: url)
end