Class: DIDKit::PLCImporter
- Inherits:
-
Object
- Object
- DIDKit::PLCImporter
- Includes:
- Requests
- Defined in:
- lib/didkit/plc_importer.rb
Constant Summary collapse
- PLC_SERVICE =
'plc.directory'- MAX_PAGE =
1000
Instance Attribute Summary collapse
-
#error_handler ⇒ Object
Returns the value of attribute error_handler.
-
#ignore_errors ⇒ Object
Returns the value of attribute ignore_errors.
-
#last_date ⇒ Object
Returns the value of attribute last_date.
Instance Method Summary collapse
- #eof? ⇒ Boolean
- #fetch(&block) ⇒ Object
- #fetch_audit_log(did) ⇒ Object
- #fetch_page ⇒ Object
- #get_export(args = {}) ⇒ Object
-
#initialize(since: nil) ⇒ PLCImporter
constructor
A new instance of PLCImporter.
- #plc_service ⇒ Object
Constructor Details
#initialize(since: nil) ⇒ PLCImporter
Returns a new instance of PLCImporter.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/didkit/plc_importer.rb', line 25 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_handler ⇒ Object
Returns the value of attribute error_handler.
23 24 25 |
# File 'lib/didkit/plc_importer.rb', line 23 def error_handler @error_handler end |
#ignore_errors ⇒ Object
Returns the value of attribute ignore_errors.
23 24 25 |
# File 'lib/didkit/plc_importer.rb', line 23 def ignore_errors @ignore_errors end |
#last_date ⇒ Object
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
102 103 104 |
# File 'lib/didkit/plc_importer.rb', line 102 def eof? !!@eof end |
#fetch(&block) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/didkit/plc_importer.rb', line 94 def fetch(&block) loop do operations = fetch_page block.call(operations) break if eof? end end |
#fetch_audit_log(did) ⇒ Object
62 63 64 65 |
# File 'lib/didkit/plc_importer.rb', line 62 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_page ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/didkit/plc_importer.rb', line 67 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 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
54 55 56 57 58 59 60 |
# File 'lib/didkit/plc_importer.rb', line 54 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_service ⇒ Object
40 41 42 |
# File 'lib/didkit/plc_importer.rb', line 40 def plc_service PLC_SERVICE end |