Class: DIDKit::PLCImporter

Inherits:
Object
  • Object
show all
Includes:
Requests
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

Methods included from Requests

#content_type_matches, #get_data, #get_json, #get_response, #uri_origin

Constructor Details

#initialize(since: nil) ⇒ PLCImporter

Returns a new instance of PLCImporter.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/didkit/plc_importer.rb', line 22

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

  @last_page_cids = []
end

Instance Attribute Details

#error_handlerObject

Returns the value of attribute error_handler.



20
21
22
# File 'lib/didkit/plc_importer.rb', line 20

def error_handler
  @error_handler
end

#ignore_errorsObject

Returns the value of attribute ignore_errors.



20
21
22
# File 'lib/didkit/plc_importer.rb', line 20

def ignore_errors
  @ignore_errors
end

#last_dateObject

Returns the value of attribute last_date.



20
21
22
# File 'lib/didkit/plc_importer.rb', line 20

def last_date
  @last_date
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/didkit/plc_importer.rb', line 99

def eof?
  !!@eof
end

#fetch(&block) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/didkit/plc_importer.rb', line 91

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

#fetch_audit_log(did) ⇒ Object



59
60
61
62
# File 'lib/didkit/plc_importer.rb', line 59

def fetch_audit_log(did)
  json = get_json("https://#{plc_service}/#{did}/log/audit", :content_type => :json)
  json.map { |j| PLCOperation.new(j) }
end

#fetch_pageObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/didkit/plc_importer.rb', line 64

def fetch_page
  request_time = Time.now

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

  operations = rows.filter_map { |json|
    begin
      PLCOperation.new(json)
    rescue PLCOperation::FormatError, AtHandles::FormatError, ServiceRecord::FormatError => e
      @error_handler ? @error_handler.call(e, json) : raise
      nil
    end
  }.reject { |op|
    # when you pass the most recent op's timestamp to ?after, it will be returned as the first op again,
    # so we need to use this CID list to filter it out (so pages will usually be 999 items long)

    @last_page_cids.include?(op.cid)
  }

  @last_date = operations.last&.created_at || request_time
  @last_page_cids = Set.new(operations.map(&:cid))
  @eof = (rows.length < MAX_PAGE)

  operations
end

#get_export(args = {}) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/didkit/plc_importer.rb', line 51

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

  data = get_data(url, content_type: 'application/jsonlines')
  data.lines.map(&:strip).reject(&:empty?).map { |x| JSON.parse(x) }
end

#plc_serviceObject



37
38
39
# File 'lib/didkit/plc_importer.rb', line 37

def plc_service
  PLC_SERVICE
end