Class: Invoance::Resources::Documents
- Inherits:
-
Object
- Object
- Invoance::Resources::Documents
- Defined in:
- lib/invoance/resources/documents.rb
Overview
Documents resource — client.documents.*
Instance Method Summary collapse
-
#anchor(document_hash:, document_ref: nil, event_type: nil, original_bytes_b64: nil, metadata: nil, trace_id: nil, idempotency_key: nil) ⇒ Hash
POST /document/anchor — Anchor a document hash.
-
#anchor_file(file:, is_path: true, document_ref: nil, event_type: nil, metadata: nil, trace_id: nil, idempotency_key: nil, skip_original: false) ⇒ Hash
Convenience helper — reads a file (path String or raw bytes String), computes the SHA-256 hash, base64-encodes the bytes, then anchors.
-
#get(event_id) ⇒ Hash
GET /document/:event_id — Retrieve a single document.
-
#get_original(event_id) ⇒ String
GET /document/:event_id/original — Download the original file.
-
#initialize(http) ⇒ Documents
constructor
A new instance of Documents.
-
#list(page: nil, limit: nil, date_from: nil, date_to: nil, document_ref: nil) ⇒ Hash
GET /document — Paginated document listing.
-
#verify(event_id, document_hash:) ⇒ Hash
POST /document/:event_id/verify — Hash verification.
Constructor Details
#initialize(http) ⇒ Documents
Returns a new instance of Documents.
12 13 14 |
# File 'lib/invoance/resources/documents.rb', line 12 def initialize(http) @http = http end |
Instance Method Details
#anchor(document_hash:, document_ref: nil, event_type: nil, original_bytes_b64: nil, metadata: nil, trace_id: nil, idempotency_key: nil) ⇒ Hash
POST /document/anchor — Anchor a document hash.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/invoance/resources/documents.rb', line 20 def anchor(document_hash:, document_ref: nil, event_type: nil, original_bytes_b64: nil, metadata: nil, trace_id: nil, idempotency_key: nil) Validate.assert_sha256_hex("document_hash", document_hash) body = { "document_hash" => document_hash } body["document_ref"] = document_ref unless document_ref.nil? body["event_type"] = event_type unless event_type.nil? body["original_bytes_b64"] = original_bytes_b64 unless original_bytes_b64.nil? body["metadata"] = unless .nil? body["trace_id"] = trace_id unless trace_id.nil? @http.post("/document/anchor", body, idempotency_key) end |
#anchor_file(file:, is_path: true, document_ref: nil, event_type: nil, metadata: nil, trace_id: nil, idempotency_key: nil, skip_original: false) ⇒ Hash
Convenience helper — reads a file (path String or raw bytes String), computes the SHA-256 hash, base64-encodes the bytes, then anchors.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/invoance/resources/documents.rb', line 40 def anchor_file(file:, is_path: true, document_ref: nil, event_type: nil, metadata: nil, trace_id: nil, idempotency_key: nil, skip_original: false) content = if is_path File.binread(file) else file.dup.force_encoding(Encoding::BINARY) end document_hash = Digest::SHA256.hexdigest(content) ref = document_ref || (is_path ? File.basename(file) : nil) # Strict Base64 (no line breaks) via stdlib pack — avoids depending on # the `base64` gem, which left Ruby's default gems in 3.4+. original_b64 = skip_original ? nil : [content].pack("m0") anchor( document_hash: document_hash, document_ref: ref, event_type: event_type, metadata: , idempotency_key: idempotency_key, original_bytes_b64: original_b64, trace_id: trace_id ) end |
#get(event_id) ⇒ Hash
GET /document/:event_id — Retrieve a single document.
80 81 82 |
# File 'lib/invoance/resources/documents.rb', line 80 def get(event_id) @http.get("/document/#{event_id}") end |
#get_original(event_id) ⇒ String
GET /document/:event_id/original — Download the original file.
86 87 88 |
# File 'lib/invoance/resources/documents.rb', line 86 def get_original(event_id) @http.get_bytes("/document/#{event_id}/original") end |
#list(page: nil, limit: nil, date_from: nil, date_to: nil, document_ref: nil) ⇒ Hash
GET /document — Paginated document listing.
68 69 70 71 72 73 74 75 76 |
# File 'lib/invoance/resources/documents.rb', line 68 def list(page: nil, limit: nil, date_from: nil, date_to: nil, document_ref: nil) @http.get("/document", { "page" => page, "limit" => limit, "date_from" => date_from, "date_to" => date_to, "document_ref" => document_ref }) end |
#verify(event_id, document_hash:) ⇒ Hash
POST /document/:event_id/verify — Hash verification.
92 93 94 95 |
# File 'lib/invoance/resources/documents.rb', line 92 def verify(event_id, document_hash:) Validate.assert_sha256_hex("document_hash", document_hash) @http.post("/document/#{event_id}/verify", { "document_hash" => document_hash }) end |