Module: Spotify::APIHelpers

Included in:
API, API
Defined in:
lib/spotify/api_helpers.rb

Overview

Some methods used in the implementation that makes the C calls less insane to make.

Class Method Summary collapse

Class Method Details

.with_buffer(type, options = {}) ⇒ Object

Allocate some memory and yield it to the given block.

Parameters:

  • type
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :size (Integer) — default: 1
  • :clear (Boolean) — default: false


15
16
17
18
19
20
21
22
23
24
# File 'lib/spotify/api_helpers.rb', line 15

def with_buffer(type, options = {})
  size = options.fetch(:size, 1)
  clear = options.fetch(:clear, false)

  if size > 0
    FFI::MemoryPointer.new(type, size, clear) do |buffer|
      return yield buffer, buffer.size
    end
  end
end

.with_string_buffer(length, *args) ⇒ Object

Allocate some memory, specifically for a string, and yield it to the given block.

Parameters:

  • length (Integer)
  • options (Hash)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/spotify/api_helpers.rb', line 31

def with_string_buffer(length, *args)
  if length > 0
    with_buffer(:char, size: length + 1) do |buffer, size|
      error = yield buffer, size

      if error.is_a?(Symbol) and error != :ok
        ""
      else
        buffer.get_string(0, length).force_encoding(Encoding::UTF_8)
      end
    end
  else
    ""
  end
end