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`).
-
#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.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(arg = nil, filename: nil, url: 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) ⇒ 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:..."}')
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/retab/mime.rb', line 47 def initialize(arg = nil, filename: nil, url: nil) case arg when nil @filename = filename @url = url when Hash sym = arg.transform_keys(&:to_sym) @filename = sym[:filename] @url = sym[:url] when String require 'json' parsed = JSON.parse(arg, symbolize_names: true) @filename = parsed[:filename] @url = parsed[:url] else raise ArgumentError, "cannot initialize Retab::MimeData from #{arg.class}" end end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
32 33 34 |
# File 'lib/retab/mime.rb', line 32 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.
37 38 39 |
# File 'lib/retab/mime.rb', line 37 def last_response @last_response end |
#url ⇒ Object
Returns the value of attribute url.
32 33 34 |
# File 'lib/retab/mime.rb', line 32 def url @url end |
Class Method Details
.coerce(input) ⇒ Object
Single entry point used by every generator-emitted resource method.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/retab/mime.rb', line 67 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 |
Instance Method Details
#as_json ⇒ Object
97 98 99 |
# File 'lib/retab/mime.rb', line 97 def as_json(*) to_h end |
#to_h ⇒ Object
85 86 87 |
# File 'lib/retab/mime.rb', line 85 def to_h { filename: filename, url: url } 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.
92 93 94 95 |
# File 'lib/retab/mime.rb', line 92 def to_json(*args) require 'json' to_h.to_json(*args) end |