Class: Ukemi::Moderator

Inherits:
Object
  • Object
show all
Defined in:
lib/ukemi/moderator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lookup(data) ⇒ Object



74
75
76
# File 'lib/ukemi/moderator.rb', line 74

def lookup(data)
  new.lookup data
end

Instance Method Details

#convert_to_date(time) ⇒ Object



67
68
69
70
71
# File 'lib/ukemi/moderator.rb', line 67

def convert_to_date(time)
  return nil unless time

  Time.at(time).to_date.to_s
end

#convert_to_unixtime(date) ⇒ Object



61
62
63
64
65
# File 'lib/ukemi/moderator.rb', line 61

def convert_to_unixtime(date)
  return nil unless date

  Time.parse(date).to_i
end

#format(records) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ukemi/moderator.rb', line 24

def format(records)
  memo = Hash.new { |h, k| h[k] = [] }

  records.each do |record|
    memo[record.data] << {
      first_seen: record.first_seen,
      last_seen: record.last_seen,
      source: record.source
    }
  end
  # Merge first seen last seen and make the sources a list.
  formatted = memo.map do |key, sources|
    first_seens = sources.filter_map { |record| convert_to_unixtime record[:first_seen] }
    last_seens = sources.filter_map { |record| convert_to_unixtime record[:last_seen] }
    [
      key,
      {
        first_seen: convert_to_date(first_seens.min),
        last_seen: convert_to_date(last_seens.max),
        sources: sources
      }
    ]
  end.to_h

  # Sorting
  ordering_key = Ukemi.configuration.ordering_key.to_sym
  sort_order = Ukemi.configuration.sort_order
  formatted.sort_by do |_key, hash|
    value = hash[ordering_key]
    if sort_order == "DESC"
      value ? -convert_to_unixtime(value) : -1
    else
      value ? convert_to_unixtime(value) : Float::MAX.to_i
    end
  end.to_h
end

#lookup(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ukemi/moderator.rb', line 9

def lookup(data)
  records = Parallel.map(Ukemi.services) do |klass|
    service = klass.new
    next unless service.configurated?

    begin
      service.lookup data
    rescue ::PassiveTotal::Error, ::VirusTotal::Error, ::SecurityTrails::Error, PassiveCIRCL::Error, DNSDB::Error, Faraday::Error
      nil
    end
  end.flatten.compact

  format records
end