Class: VtAPI::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(response_body) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/vtapi/response.rb', line 6

def initialize(response_body)
  @json = JSON.parse(response_body)
  @json.keys.each do |key|
    Response.class_eval {
      define_method key.to_s do |*args|
        @json[key]
      end
    }
  end
  @json['scan_date'] = Time.parse(@json['scan_date'] + "UTC") if @json.has_key? 'scan_date'
end

Instance Method Details

#[](key) ⇒ Object



34
35
36
# File 'lib/vtapi/response.rb', line 34

def [](key)
  @json.fetch(key.to_s) # raise KeyError when key doesn't exist.
end

#keysObject



18
19
20
# File 'lib/vtapi/response.rb', line 18

def keys
  @json.keys
end

#positive_brandsObject



26
27
28
# File 'lib/vtapi/response.rb', line 26

def positive_brands
  @json.fetch('scans', {}).select{|k,v| v['detected'] }.keys
end

#positive_threatsObject



22
23
24
# File 'lib/vtapi/response.rb', line 22

def positive_threats
  Hash[@json.fetch('scans', {}).select{|k,v| v['detected'] }.map{|k,v| [k, v['result']] }]
end

#scan_resultsObject



30
31
32
# File 'lib/vtapi/response.rb', line 30

def scan_results
  Hash[@json.fetch('scans', {}).map{|k,v| [k, v['result']] }]
end