Module: WahWah::Helper

Defined in:
lib/wahwah/helper.rb

Class Method Summary collapse

Class Method Details

.byte_string_to_guid(byte_string) ⇒ Object



32
33
34
35
# File 'lib/wahwah/helper.rb', line 32

def self.byte_string_to_guid(byte_string)
  guid = byte_string.unpack('NnnA*').pack('VvvA*').unpack('H*').first
  [guid[0..7], guid[8..11], guid[12..15], guid[16..19], guid[20..-1]].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(file_path) ⇒ Object



28
29
30
# File 'lib/wahwah/helper.rb', line 28

def self.file_format(file_path)
  File.extname(file_path).downcase.delete('.')
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..-1] }.join.to_i(2)
  else
    bits_string.to_i(2)
  end
end

.split_with_terminator(string, terminator_size) ⇒ Object



24
25
26
# File 'lib/wahwah/helper.rb', line 24

def self.split_with_terminator(string, terminator_size)
  string.split(Regexp.new(('\x00' * terminator_size).b), 2)
end