Class: PoliPage::Resources::Documents

Inherits:
Object
  • Object
show all
Defined in:
lib/poli_page/documents.rb

Overview

‘client.documents` namespace (sdk-ruby-plan.md §13 Phase 4, port of sdk-node/src/documents.ts).

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Documents

Returns a new instance of Documents.



14
15
16
# File 'lib/poli_page/documents.rb', line 14

def initialize(client)
  @client = client
end

Instance Method Details

#delete(id) ⇒ nil

DELETE /v1/documents/:id — returns nil. Re-deleting an already-deleted document surfaces as ‘PoliPage::GoneError` (HTTP 410) — no special handling here.

Examples:

client.documents.delete("doc_abc123")

Parameters:

  • id (String)

Returns:

  • (nil)

Raises:



88
89
90
91
# File 'lib/poli_page/documents.rb', line 88

def delete(id)
  @client.execute_delete(path_for(id))
  nil
end

#get(id) ⇒ PoliPage::DocumentDescriptor

GET /v1/documents/:id — returns a ‘DocumentDescriptor` with the client back-reference attached so `#download_pdf` works.

Examples:

Re-fetch a stored document and download its PDF

doc = client.documents.get("doc_abc123")
File.binwrite("invoice.pdf", doc.download_pdf)

Parameters:

  • id (String)

    the document id (e.g. ‘“doc_abc123”`)

Returns:

Raises:



29
30
31
32
33
# File 'lib/poli_page/documents.rb', line 29

def get(id)
  parsed = @client.execute_get(path_for(id))
  parsed[:metadata] ||= {}
  PoliPage::DocumentDescriptor.new(**parsed, _client: @client)
end

#preview(id) ⇒ PoliPage::DocumentPreviewResult

GET /v1/documents/:id/preview — returns the stored paginated HTML plus the page count carried by the ‘X-Document-Page-Count` response header. The body is `text/html`, NOT a JSON envelope. No engine work — this is a snapshot read of the stored document.

Examples:

stored = client.documents.preview("doc_abc123")
File.write("preview.html", stored.html)
puts "#{stored.page_count} pages"

Parameters:

  • id (String)

Returns:



47
48
49
50
51
52
# File 'lib/poli_page/documents.rb', line 47

def preview(id)
  response = @client.execute_get_raw("#{path_for(id)}/preview")
  page_count_header = response.headers[Internal::Constants::HEADER_DOCUMENT_PAGE_COUNT]
  page_count = page_count_header.to_s.match?(/\A\d+\z/) ? page_count_header.to_i : 0
  PoliPage::DocumentPreviewResult.new(html: response.body.to_s, page_count: page_count)
end

#thumbnails(id, **options) ⇒ Array<PoliPage::Thumbnail>

POST /v1/documents/:id/thumbnails — the deployed API expects the options nested under a ‘thumbnails` key. The response envelope `{ thumbnails: […] }` is unwrapped here. Returns an Array of `PoliPage::Thumbnail`; each carries base64-encoded image bytes in `data` (decode with `Base64.decode64(thumb.data)`).

Examples:

thumbs = client.documents.thumbnails("doc_abc123", width: 320, format: "png", pages: [1])
File.binwrite("page1.png", Base64.decode64(thumbs.first.data))

Parameters:

  • id (String)
  • options (Hash)

    Forwarded to the wire under ‘thumbnails:`.

    • ‘width:` [Integer] (required)

    • ‘format:` [“png” | “jpeg”]

    • ‘quality:` [Integer] JPEG quality 1-100 (jpeg only)

    • ‘pages:` [Array<Integer>] 1-based page indices; default all pages

Returns:



71
72
73
74
75
76
# File 'lib/poli_page/documents.rb', line 71

def thumbnails(id, **options)
  validate_thumbnail_options!(options)
  body = { thumbnails: options.compact }
  parsed = @client.execute_post("#{path_for(id)}/thumbnails", body: body)
  parsed[:thumbnails].map { |t| PoliPage::Thumbnail.new(**t) }
end