Class: PasswordBreachAlert::Models::Breach

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/password_breach_alert/models/breach.rb

Overview

rubocop:disable ApplicationRecord

Class Method Summary collapse

Class Method Details

.create_new_from_api(api_breaches) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/password_breach_alert/models/breach.rb', line 29

def self.create_new_from_api(api_breaches)
  created_ids = []
  present_names = Breach.pluck(:name)

  api_breaches.each do |api_breach|
    next if api_breach['Name'].in?(present_names)

    breach = Breach.create(
      name: api_breach['Name'],
      title: api_breach['Title'],
      domain: api_breach['Domain'],
      breach_date: api_breach['BreachDate'],
      added_date: api_breach['AddedDate'],
      pwn_count: api_breach['PwnCount'],
      description: api_breach['Description'],
      logo_path: api_breach['LogoPath'],
      data_classes: api_breach['DataClasses']
    )

    created_ids << breach.id
  end

  Breach.where(id: created_ids).sorted
end