Class: VtAPI::Response

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Response

Returns a new instance of Response.



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

def initialize(json)
  @json = json
  @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

Class Method Details

.parse(response_body) ⇒ Object



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

def self.parse(response_body)
  json = JSON.parse(response_body)
  if json.is_a? Array
    return json.map{|e| Response.new(e) }
  else
    return Response.new(json)
  end
end

Instance Method Details

#[](key) ⇒ Object



43
44
45
# File 'lib/vtapi/response.rb', line 43

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

#keysObject



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

def keys
  @json.keys
end

#positive_brandsObject



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

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

#positive_threatsObject



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

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

#scan_resultsObject



39
40
41
# File 'lib/vtapi/response.rb', line 39

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

#to_sObject



47
48
49
# File 'lib/vtapi/response.rb', line 47

def to_s
  @json.to_json
end