Module: Spotify::APIHelpers
Overview
Some methods used in the implementation that makes the C calls less insane to make.
Class Method Summary collapse
-
.with_buffer(type, options = {}) ⇒ Object
Allocate some memory and yield it to the given block.
-
.with_string_buffer(length, *args) ⇒ Object
Allocate some memory, specifically for a string, and yield it to the given block.
Class Method Details
.with_buffer(type, options = {}) ⇒ Object
Allocate some memory and yield it to the given block.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/spotify/api_helpers.rb', line 15 def with_buffer(type, = {}) size = .fetch(:size, 1) clear = .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.
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 |