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

Class Method Details

.from_native(value, ctx) ⇒ String?

Given a pointer, read a size-byte image ID from it.

Parameters:

  • value (FFI::Pointer)
  • ctx

Returns:

  • (String, nil)

    the image ID as a string, or nil



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

Returns:

  • (Boolean)

See Also:

  • NulString.reference_required?


45
46
47
# File 'lib/spotify/data_converters/image_id.rb', line 45

def reference_required?
  true
end

.sizeInteger

Returns bytesize of image ID pointers.

Returns:

  • (Integer)

    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.

Parameters:

  • value (#to_str, nil)

    image id as a string

  • ctx

Returns:

  • (FFI::Pointer)

    pointer to the image ID



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