Class: DIDKit::PLCImporter

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

Constant Summary collapse

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.



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

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 || DIDKit::DEFAULT_PLC_DIRECTORY
  @plc_service = "https://#{@plc_service}" unless @plc_service.include?('://')
end

Instance Attribute Details

#last_date ⇒ Object

Returns the value of attribute last_date.



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

def last_date
  @last_date
end

Instance Method Details

#eof? ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/didkit/plc_importer.rb', line 88

def eof?
  !!@eof
end

#fetch(&block) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/didkit/plc_importer.rb', line 80

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

#fetch_audit_log(did) ⇒ Object



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

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

#fetch_page ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/didkit/plc_importer.rb', line 57

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



44
45
46
47
48
49
50
# File 'lib/didkit/plc_importer.rb', line 44

def get_export(args = {})
  url = URI("#{@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