Module: Spotify::ImageID
- Extended by:
- FFI::DataConverter
- Defined in:
- lib/spotify/data_converters/image_id.rb
Overview
A custom data type for Spotify image IDs.
It will convert strings to image ID pointers when handling values from Ruby to C, and it will convert pointers to Ruby strings when handling values from C to Ruby.
Class Method Summary collapse
-
.from_native(value, ctx) ⇒ String?
Given a pointer, read a ImageID.size-byte image ID from it.
- .reference_required? ⇒ Boolean
-
.size ⇒ Integer
Bytesize of image ID pointers.
-
.to_native(value, ctx) ⇒ FFI::Pointer
Given a string, convert it to an image ID pointer.
Class Method Details
.from_native(value, ctx) ⇒ String?
Given a pointer, read a size-byte image ID from it.
40 41 42 |
# File 'lib/spotify/data_converters/image_id.rb', line 40 def from_native(value, ctx) value.get_bytes(0, size) unless value.null? end |
.reference_required? ⇒ Boolean
45 46 47 |
# File 'lib/spotify/data_converters/image_id.rb', line 45 def reference_required? true end |
.size ⇒ Integer
Returns bytesize of image ID pointers.
13 14 15 |
# File 'lib/spotify/data_converters/image_id.rb', line 13 def size 20 end |
.to_native(value, ctx) ⇒ FFI::Pointer
Given a string, convert it to an image ID pointer.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/spotify/data_converters/image_id.rb', line 22 def to_native(value, ctx) value && begin value = value.to_str if value.bytesize != size raise ArgumentError, "image id bytesize must be #{size}, was #{value.bytesize}" end pointer = FFI::MemoryPointer.new(:char, size) pointer.write_string(value) end end |