Sha256: 2d3f3ecdb24ab6889d0a732d536f932abd38a12199138007fb8b221abccc76f5

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

module Spotify
  # 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.
  module ImageID
    extend FFI::DataConverter
    native_type FFI::Type::POINTER

    # Given a string, convert it to an image ID pointer.
    #
    # @param [String] value image id as a string
    # @param ctx
    # @return [FFI::Pointer] pointer to the image ID
    def self.to_native(value, ctx)
      pointer = if value
        if value.bytesize != 20
          raise ArgumentError, "image id bytesize must be 20, was #{value.bytesize}"
        end

        pointer = FFI::MemoryPointer.new(:char, 20)
        pointer.write_string(value.to_s)
      end

      super(pointer, ctx)
    end

    # Given a pointer, read a 20-byte image ID from it.
    #
    # @param [FFI::Pointer] value
    # @param ctx
    # @return [String, nil] the image ID as a string, or nil
    def self.from_native(value, ctx)
      value.read_string(20) unless value.null?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spotify-12.2.0 lib/spotify/types/image_id.rb
spotify-12.0.3 lib/spotify/types/image_id.rb
spotify-12.0.2 lib/spotify/types/image_id.rb
spotify-12.0.1 lib/spotify/types/image_id.rb