Module: WahWah::Helper

Defined in:
lib/wahwah/helper.rb

Class Method Summary collapse

Class Method Details

.byte_string_to_guid(byte_string) ⇒ Object



45
46
47
48
# File 'lib/wahwah/helper.rb', line 45

def self.byte_string_to_guid(byte_string)
  guid = byte_string.unpack("NnnA*").pack("VvvA*").unpack1("H*")
  [guid[0..7], guid[8..11], guid[12..15], guid[16..19], guid[20..]].join("-").upcase
end

.encode_to_utf8(string, source_encoding: "") ⇒ Object



5
6
7
8
9
10
11
# File 'lib/wahwah/helper.rb', line 5

def self.encode_to_utf8(string, source_encoding: "")
  encoded_string = source_encoding.empty? ?
    string.force_encoding("utf-8") :
    string.encode("utf-8", source_encoding, invalid: :replace, undef: :replace, replace: "")

  encoded_string.valid_encoding? ? encoded_string.strip : ""
end

.file_format(io) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/wahwah/helper.rb', line 36

def self.file_format(io)
  if io.respond_to?(:path) && io.path && !io.path.empty?
    file_format = file_format_from_extension io.path
    return file_format unless file_format.nil? || file_format.empty?
  end

  file_format_from_signature io
end

.id3_size_caculate(bits_string, has_zero_bit: true) ⇒ Object

ID3 size is encoded with four bytes where may the most significant bit (bit 7) is set to zero in every byte, making a total of 28 bits. The zeroed bits are ignored



16
17
18
19
20
21
22
# File 'lib/wahwah/helper.rb', line 16

def self.id3_size_caculate(bits_string, has_zero_bit: true)
  if has_zero_bit
    bits_string.scan(/.{8}/).map { |byte_string| byte_string[1..] }.join.to_i(2)
  else
    bits_string.to_i(2)
  end
end

.split_with_terminator(string, terminator_size) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wahwah/helper.rb', line 24

def self.split_with_terminator(string, terminator_size)
  terminator = ("\x00" * terminator_size).b

  (0...string.size).step(terminator_size).each do |index|
    if string[index...(index + terminator_size)] == terminator
      return [string[0...index], string[index + terminator_size..]]
    end
  end

  []
end