Class: Retab::MimeData
- Inherits:
-
Object
- Object
- Retab::MimeData
- Defined in:
- lib/retab/mime.rb
Overview
Wire-shape MimeData. Mirrors the spec’s ‘MIMEData` component schema.
Customers rarely build this directly. Pass any of the supported input shapes to a resource method’s ‘document:` keyword and the generator-emitted code calls `Retab::MimeData.coerce(input)` to produce the canonical wire form before serialization.
Supported input types:
- `Retab::MimeData` (passthrough)
- `Pathname` (file path; reads bytes, base64-encodes)
- `File`, `IO`, `StringIO` (any IO-like; reads bytes, base64-encodes)
- `String` containing a URL (`http://`, `https://`, `data:`, `gs://`)
- `String` containing a path on disk (falls back to file-read)
- `Hash` with `:filename` + `:url` keys (already-built wire shape)
Constant Summary collapse
- EXTENSION_MIME_MAP =
{ ".pdf" => "application/pdf", ".png" => "image/png", ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".gif" => "image/gif", ".webp" => "image/webp", ".txt" => "text/plain", ".csv" => "text/csv", ".json" => "application/json", ".xml" => "application/xml", ".html" => "text/html", ".md" => "text/markdown", ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation" }.freeze
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#last_response ⇒ Object
Set by resource methods when MimeData is returned from the API (see ‘Files#complete_upload`).
-
#mime_type ⇒ Object
Returns the value of attribute mime_type.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
-
.coerce(input) ⇒ Object
Single entry point used by every generator-emitted resource method.
- .coerce_document_map(input) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(arg = nil, filename: nil, url: nil, mime_type: nil) ⇒ MimeData
constructor
Two calling conventions are supported: - keyword form (canonical for customer code): Retab::MimeData.new(filename: ‘x.pdf’, url: ‘data:…’) - positional Hash / JSON string (used by spec-derived model classes whose ‘initialize(json)` deserializes a nested `MIMEData` field via `Retab::MimeData.new(hash)`): Retab::MimeData.new({ filename: ’x.pdf’, url: ‘data:…’ }) Retab::MimeData.new(‘“x.pdf”, “url”: “data:…”’).
- #to_h ⇒ Object
-
#to_json(*args) ⇒ Object
JSON serialization.
Constructor Details
#initialize(arg = nil, filename: nil, url: nil, mime_type: nil) ⇒ MimeData
Two calling conventions are supported:
- keyword form (canonical for customer code):
Retab::MimeData.new(filename: 'x.pdf', url: 'data:...')
- positional Hash / JSON string (used by spec-derived model
classes whose `initialize(json)` deserializes a nested
`MIMEData` field via `Retab::MimeData.new(hash[:document])`):
Retab::MimeData.new({ filename: 'x.pdf', url: 'data:...' })
Retab::MimeData.new('{"filename": "x.pdf", "url": "data:..."}')
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/retab/mime.rb', line 48 def initialize(arg = nil, filename: nil, url: nil, mime_type: nil) case arg when nil @filename = filename @url = url @mime_type = mime_type when Hash sym = arg.transform_keys(&:to_sym) @filename = sym[:filename] @url = sym[:url] @mime_type = sym[:mime_type] || sym[:mimeType] when String require "json" parsed = JSON.parse(arg, symbolize_names: true) @filename = parsed[:filename] @url = parsed[:url] @mime_type = parsed[:mime_type] || parsed[:mimeType] else raise ArgumentError, "cannot initialize Retab::MimeData from #{arg.class}" end end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
33 34 35 |
# File 'lib/retab/mime.rb', line 33 def filename @filename end |
#last_response ⇒ Object
Set by resource methods when MimeData is returned from the API (see ‘Files#complete_upload`). Mirrors the spec-derived models’ BaseModel ‘last_response` field.
38 39 40 |
# File 'lib/retab/mime.rb', line 38 def last_response @last_response end |
#mime_type ⇒ Object
Returns the value of attribute mime_type.
33 34 35 |
# File 'lib/retab/mime.rb', line 33 def mime_type @mime_type end |
#url ⇒ Object
Returns the value of attribute url.
33 34 35 |
# File 'lib/retab/mime.rb', line 33 def url @url end |
Class Method Details
.coerce(input) ⇒ Object
Single entry point used by every generator-emitted resource method.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/retab/mime.rb', line 72 def self.coerce(input) case input when MimeData input when Pathname from_pathname(input) when ::IO, ::StringIO, ::File from_io(input) when Hash from_hash(input) when String from_string(input) else raise( ArgumentError, "cannot coerce #{input.class} to Retab::MimeData; supply a Pathname, IO, String (URL or path), Hash, or MimeData" ) end end |
.coerce_document_map(input) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/retab/mime.rb', line 92 def self.coerce_document_map(input) unless input.respond_to?(:transform_values) raise ArgumentError, "cannot coerce #{input.class} to a Retab::MimeData document map" end input.transform_values do |document| file_ref_like?(document) ? document : coerce(document) end end |
Instance Method Details
#as_json ⇒ Object
115 116 117 |
# File 'lib/retab/mime.rb', line 115 def as_json(*) to_h end |
#to_h ⇒ Object
102 103 104 |
# File 'lib/retab/mime.rb', line 102 def to_h {filename: filename, url: url, mime_type: mime_type} end |
#to_json(*args) ⇒ Object
JSON serialization. Body construction in resource methods passes the MimeData object straight into ‘JSON.generate`; without `to_json` it would fall back to `to_s` and ship the inspect string.
109 110 111 112 113 |
# File 'lib/retab/mime.rb', line 109 def to_json(*args) require "json" to_h.to_json(*args) end |