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

Constructor Details

#initialize(since: nil, service: nil) ⇒ PLCImporter

Returns a new instance of PLCImporter.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/didkit/plc_importer.rb', line 25

def initialize(since: nil, service: 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 = []
  @plc_service = service || PLC_SERVICE
end

Instance Attribute Details

#last_dateObject

Returns the value of attribute last_date.



23
24
25
# File 'lib/didkit/plc_importer.rb', line 23

def last_date
  @last_date
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/didkit/plc_importer.rb', line 85

def eof?
  !!@eof
end

#fetch(&block) ⇒ Object



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

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)
  json = get_json("https://#{@plc_service}/#{did}/log/audit", :content_type => :json)
  json.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
74
75
# File 'lib/didkit/plc_importer.rb', line 54

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

  operations = rows.map { |json|
    PLCOperation.new(json)
  }.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)
  }

  unless operations.empty?
    @last_date = operations.last.created_at
    @last_page_cids = Set.new(operations.map(&:cid))
  end

  @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 = get_data(url, content_type: 'application/jsonlines')
  data.lines.map(&:strip).reject(&:empty?).map { |x| JSON.parse(x) }
end