Class: Rafflesia::MimeData
- Inherits:
-
Object
- Object
- Rafflesia::MimeData
- Defined in:
- lib/rafflesia/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 Rafflesia::MimeData.coerce(input) to
produce the canonical wire form before serialization.
Supported input types:
- `Rafflesia::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 = nil, client: nil, **rest) ⇒ Object
Single entry point used by every generator-emitted resource method.
- .coerce_document_map(input, client: nil) ⇒ 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): Rafflesia::MimeData.new(filename: 'x.pdf', url: 'data:...') - positional Hash / JSON string (used by spec-derived model classes whose
initialize(json)deserializes a nestedMIMEDatafield viaRafflesia::MimeData.new(hash[:document])): Rafflesia::MimeData.new({ filename: 'x.pdf', url: 'data:...' }) Rafflesia::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):
Rafflesia::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 `Rafflesia::MimeData.new(hash[:document])`):
Rafflesia::MimeData.new({ filename: 'x.pdf', url: 'data:...' })
Rafflesia::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 |
# File 'lib/rafflesia/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 Rafflesia::MimeData from #{arg.class}" end end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
33 34 35 |
# File 'lib/rafflesia/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/rafflesia/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/rafflesia/mime.rb', line 33 def mime_type @mime_type end |
#url ⇒ Object
Returns the value of attribute url.
33 34 35 |
# File 'lib/rafflesia/mime.rb', line 33 def url @url end |
Class Method Details
.coerce(input = nil, client: nil, **rest) ⇒ Object
Single entry point used by every generator-emitted resource method.
A file-id document input (a Rafflesia::FileRef object, or a Hash
carrying an :id key) is resolved CLIENT-SIDE into URL-backed
MimeData by calling the Files download-link endpoint — the document
routes now accept only URL-backed MimeData {filename, url} and 422
a file-id wire body. Mirrors the Node SDK and Go CLI
(resolveFileIDToMIMEData). client: is the Rafflesia::Client
threaded through from the resource method body; it must be present to
resolve a file-id input.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rafflesia/mime.rb', line 80 def self.coerce(input = nil, client: nil, **rest) # Backward-compatible bare-keyword form: `coerce(filename: ..., url: ...)` # slurps the keywords (minus `client:`) into the Hash input shape. input = rest if input.nil? && !rest.empty? return resolve_file_ref(input, client: client) if file_ref_like?(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 Rafflesia::MimeData; supply a Pathname, IO, String (URL or path), Hash, or MimeData" end end |
.coerce_document_map(input, client: nil) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/rafflesia/mime.rb', line 104 def self.coerce_document_map(input, client: nil) unless input.respond_to?(:transform_values) raise ArgumentError, "cannot coerce #{input.class} to a Rafflesia::MimeData document map" end input.transform_values { |document| coerce(document, client: client) } end |
Instance Method Details
#as_json ⇒ Object
124 125 126 |
# File 'lib/rafflesia/mime.rb', line 124 def as_json(*) to_h end |
#to_h ⇒ Object
112 113 114 |
# File 'lib/rafflesia/mime.rb', line 112 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.
119 120 121 122 |
# File 'lib/rafflesia/mime.rb', line 119 def to_json(*args) require 'json' to_h.to_json(*args) end |