class ApeTag

  1. apetag.rb
Superclass: Object

Contains all of the ApeItems found in the filename/file given. MAX_SIZE and MAX_ITEM_COUNT constants are recommended defaults, they can be increased if necessary.

Constants

FILE_OBJ_METHODS = %w'close seek read pos write truncate'  
FOOTER_FLAGS = "\x00\x00\x80"  
HEADER_FLAGS = "\x00\x00\xA0"  
ID3_GENRES = 'Blues, Classic Rock, Country, Dance, Disco, Funk, Grunge, Hip-Hop, Jazz, Metal, New Age, Oldies, Other, Pop, R & B, Rap, Reggae, Rock, Techno, Industrial, Alternative, Ska, Death Metal, Prank, Soundtrack, Euro-Techno, Ambient, Trip-Hop, Vocal, Jazz + Funk, Fusion, Trance, Classical, Instrumental, Acid, House, Game, Sound Clip, Gospel, Noise, Alternative Rock, Bass, Soul, Punk, Space, Meditative, Instrumental Pop, Instrumental Rock, Ethnic, Gothic, Darkwave, Techno-Industrial, Electronic, Pop-Fol, Eurodance, Dream, Southern Rock, Comedy, Cult, Gangsta, Top 40, Christian Rap, Pop/Funk, Jungle, Native US, Cabaret, New Wave, Psychadelic, Rave, Showtunes, Trailer, Lo-Fi, Tribal, Acid Punk, Acid Jazz, Polka, Retro, Musical, Rock & Roll, Hard Rock, Folk, Folk-Rock, National Folk, Swing, Fast Fusion, Bebop, Latin, Revival, Celtic, Bluegrass, Avantgarde, Gothic Rock, Progressive Rock, Psychedelic Rock, Symphonic Rock, Slow Rock, Big Band, Chorus, Easy Listening, Acoustic, Humour, Speech, Chanson, Opera, Chamber Music, Sonata, Symphony, Booty Bass, Primus, Porn Groove, Satire, Slow Jam, Club, Tango, Samba, Folklore, Ballad, Power Ballad, Rhytmic Soul, Freestyle, Duet, Punk Rock, Drum Solo, Acapella, Euro-House, Dance Hall, Goa, Drum & Bass, Club-House, Hardcore, Terror, Indie, BritPop, Negerpunk, Polsk Punk, Beat, Christian Gangsta Rap, Heavy Metal, Black Metal, Crossover, Contemporary Christian, Christian Rock, Merengue, Salsa, Thrash Metal, Anime, Jpop, Synthpop'.split(',').collect{|g| g.strip}  
ID3_GENRES_HASH = CICPHash.new(255.chr)  
MAX_ITEM_COUNT = 64  
MAX_SIZE = 8192  
MP3_RE = Regexp.new('\.mp3\z')  
PREAMBLE = "APETAGEX\xD0\x07\x00\x00"  
RECOMMENDED_KEYS = %w'Title Artist Album Year Comment Genre Track Subtitle Publisher Conductor Composer Copyright Publicationright File EAN/UPC ISBN Catalog LC Media Index Related ISRC Abstract Language Bibliography Introplay Dummy' << 'Debut Album' << 'Record Date' << 'Record Location'  
YEAR_RE = Regexp.new('\d{4}')  

Attributes

Public Class methods

check_id3= (flag)

Set whether to check for id3 tags by default on file objects (defaults to true)

[show source]
# File apetag.rb, line 270
def self.check_id3=(flag)
  raise ApeTagError, "check_id3 must be boolean" unless [true, false].include?(flag)
  @@check_id3 = flag
end
new (filename, check_id3 = nil)

Set the filename or file object to operate on. If the object has all methods in FILE_OBJ_METHODS, it is treated as a file, otherwise, it is treated as a filename. If the filename is invalid, Errno::ENOENT or Errno::EINVAL will probably be raised when calling methods. Optional argument #check_id3 checks for ID3 tags. If #check_id3 is not specified and filename is a file object, the ApeTag default is used. If #check_id3 is not specified and filename is a filename, it checks for ID3 tags only if the filename ends with “.mp3”. If files have APE tags but no ID3 tags, ID3 tags will never be added. If files have neither tag, #check_id3 will decide whether to add an ID3 tag. If files have both tags, make sure #check_id3 is true or it will miss both tags.

[show source]
# File apetag.rb, line 285
def initialize(filename, check_id3 = nil)
  if FILE_OBJ_METHODS.each{|method| break unless filename.respond_to?(method)}
    @file = filename
    @check_id3 = check_id3.nil? ? @@check_id3 : check_id3 
  else
    @filename = filename.to_s
    @check_id3 = check_id3.nil? ? (MP3_RE.match(@filename) ? true : nil) : check_id3 
  end
end

Public Instance methods

exists? ()

Check the file for an APE tag. Returns true or false. Raises ApeTagError for corrupt tags.

[show source]
# File apetag.rb, line 296
def exists?
  @has_tag.nil? ? access_file('rb'){has_tag} : @has_tag
end
fields ()

A CICPHash of ApeItems found in the file, or an empty CICPHash if the file doesn't have an APE tag. Raises ApeTagError for corrupt tags.

[show source]
# File apetag.rb, line 316
def fields
  @fields || access_file('rb'){get_fields}
end
has_id3? ()

Check the file for an ID3 tag. Returns true or false. Raises ApeTagError for corrupt tags.

[show source]
# File apetag.rb, line 301
def has_id3?
  exists?
  id3 != ''
end
pretty_print ()

Pretty print tags, with one line per field, showing key and value.

[show source]
# File apetag.rb, line 321
def pretty_print
  begin
    fields.values.sort_by{|value| value.key}.collect{|value| "#{value.key}: #{value.join(', ')}"}.join("\n")
  rescue ApeTagError => e
    "CORRUPT TAG!: #{e.message}"
  rescue Errno::ENOENT, Errno::EINVAL
    "FILE NOT FOUND!"
  end
end
raw ()

The raw APEv2 + ID3v1.1 tag. If one or the other is empty that part will be missing. Raises ApeTagError for corrupt tags.

[show source]
# File apetag.rb, line 333
def raw
  exists? 
  "#{tag_header}#{tag_data}#{tag_footer}#{id3}"
end
remove! ()

Remove an APE tag from a file, if one exists. Returns true. Raises ApeTagError for corrupt tags.

[show source]
# File apetag.rb, line 308
def remove!
  access_file('rb+'){file.truncate(tag_start) if has_tag || has_id3?}
  @has_tag, @fields, @id3, @tag_size, @tag_start, @tag_data, @tag_header, @tag_footer, @tag_item_count = []
  true
end
update (&block)

Yields a CICPHash of ApeItems found in the file, or an empty CICPHash if the file doesn't have an APE tag. This hash should be modified (not reassigned) inside the block. An APEv2+ID3v1.1 tag with the new fields will overwrite the previous tag. If the file doesn't have an APEv2 tag, one will be created and appended to it. If the file doesn't have an ID3v1.1 tag, one will be generated from the ApeTag fields and appended to it. If the file already has an ID3v1.1 tag, the data in it is ignored, and it is overwritten. Raises ApeTagError if either the existing tag is invalid or the tag to be written would be invalid.

[show source]
# File apetag.rb, line 346
def update(&block)
  access_file('rb+') do 
    yield get_fields
    normalize_fields
    update_id3
    update_ape
    write_tag
  end
  fields
end