Class: Printers::RefreshPrinters

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/printers/refresh_printers.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#determine_store_id_location(ip) ⇒ Object



39
40
41
42
43
44
45
# File 'app/services/printers/refresh_printers.rb', line 39

def determine_store_id_location(ip)
  if IpDetector.warmlyyours_canada_ip?(ip)
    return Store::WARMLYYOURS_CA_ID
  else
    return Store::WARMLYYOURS_US_ID
  end
end

#processObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/printers/refresh_printers.rb', line 6

def process
  client = Printer.print_node_client
  #computers = client.computers
  cloud_printers = client.printers
  discovered_printer_ids = []
  errors = []
  updated = 0
  created = 0
  cloud_printers.each do |cloud_printer|
    printer = Printer.where(print_node_id: cloud_printer.id).first_or_initialize
    discovered_printer_ids << cloud_printer.id
    printer.name = cloud_printer.name
    printer.computer_name = cloud_printer.computer&.name
    printer.state = cloud_printer.state
    printer.capabilities = cloud_printer.capabilities.to_h
    printer.store_id = determine_store_id_location(cloud_printer.computer.inet)

    if printer.new_record?
      created += 1
    else
      updated += 1
    end
    unless printer.save
      errors << [cloud_printer.id,printer.id,printer.errors.full_messages]
    end
  end
  invalid_printers = Printer.where.not(print_node_id: discovered_printer_ids)
  destroyed = invalid_printers.size
  invalid_printers.destroy_all
  Result.new(updated: updated, created: created, destroyed: destroyed, errors: errors)
end