Class: DIDKit::PLCImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/didkit/plc_importer.rb

Constant Summary collapse

PLC_SERVICE =
'plc.directory'
MAX_PAGE =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(since: nil) ⇒ PLCImporter

Returns a new instance of PLCImporter.



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

def initialize(since: nil)
  if since.to_s == 'beginning'
    @last_date = nil
  elsif since.is_a?(String)
    @last_date = Time.parse(since)
  elsif since
    @last_date = since
  else
    @last_date = Time.now
    @eof = true
  end
end

Instance Attribute Details

#error_handlerObject

Returns the value of attribute error_handler.



12
13
14
# File 'lib/didkit/plc_importer.rb', line 12

def error_handler
  @error_handler
end

#ignore_errorsObject

Returns the value of attribute ignore_errors.



12
13
14
# File 'lib/didkit/plc_importer.rb', line 12

def ignore_errors
  @ignore_errors
end

#last_dateObject

Returns the value of attribute last_date.



12
13
14
# File 'lib/didkit/plc_importer.rb', line 12

def last_date
  @last_date
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/didkit/plc_importer.rb', line 83

def eof?
  !!@eof
end

#fetch(&block) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/didkit/plc_importer.rb', line 75

def fetch(&block)
  loop do
    operations = fetch_page
    block.call(operations)
    break if eof?
  end
end

#fetch_audit_log(did) ⇒ Object



49
50
51
52
# File 'lib/didkit/plc_importer.rb', line 49

def fetch_audit_log(did)
  response = URI.open("https://#{plc_service}/#{did}/log/audit").read
  JSON.parse(response).map { |j| PLCOperation.new(j) }
end

#fetch_pageObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/didkit/plc_importer.rb', line 54

def fetch_page
  request_time = Time.now

  query = @last_date ? { :after => @last_date.utc.iso8601(6) } : {}
  rows = get_export(query)

  operations = rows.filter_map do |json|
    begin
      PLCOperation.new(json)
    rescue PLCOperation::FormatError, AtHandles::FormatError, ServiceRecord::FormatError => e
      @error_handler ? @error_handler.call(e, json) : raise
      nil
    end
  end

  @last_date = operations.last&.created_at || request_time
  @eof = (rows.length < MAX_PAGE)

  operations
end

#get_export(args = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/didkit/plc_importer.rb', line 41

def get_export(args = {})
  url = URI("https://#{plc_service}/export")
  url.query = URI.encode_www_form(args)

  data = URI.open(url).read
  data.lines.map(&:strip).reject(&:empty?).map { |x| JSON.parse(x) }
end

#plc_serviceObject



27
28
29
# File 'lib/didkit/plc_importer.rb', line 27

def plc_service
  PLC_SERVICE
end