Class: Fontist::Macos::Catalog::BaseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/macos/catalog/base_parser.rb

Overview

Base parser for macOS Font catalogs Handles common parsing logic for Font7, Font8, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_path) ⇒ BaseParser

Returns a new instance of BaseParser.



11
12
13
14
# File 'lib/fontist/macos/catalog/base_parser.rb', line 11

def initialize(xml_path)
  @xml_path = xml_path
  @data = nil
end

Instance Attribute Details

#xml_pathObject (readonly)

Returns the value of attribute xml_path.



9
10
11
# File 'lib/fontist/macos/catalog/base_parser.rb', line 9

def xml_path
  @xml_path
end

Instance Method Details

#assetsObject



16
17
18
19
20
21
22
23
# File 'lib/fontist/macos/catalog/base_parser.rb', line 16

def assets
  posted_date_str = posted_date
  framework_ver = framework_version
  parse_assets.map do |asset_data|
    Asset.new(asset_data, posted_date: posted_date_str,
                          framework_version: framework_ver)
  end
end

#catalog_versionObject



42
43
44
45
# File 'lib/fontist/macos/catalog/base_parser.rb', line 42

def catalog_version
  # Extract from filename: com_apple_MobileAsset_Font7.xml -> 7
  File.basename(@xml_path).match(/Font(\d+)/)[1].to_i
end

#framework_versionObject



47
48
49
50
# File 'lib/fontist/macos/catalog/base_parser.rb', line 47

def framework_version
  # Extract from filename: com_apple_MobileAsset_Font7.xml -> 7
  File.basename(@xml_path).match(/Font(\d+)/)[1].to_i
end

#posted_dateObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fontist/macos/catalog/base_parser.rb', line 25

def posted_date
  date_obj = data["postedDate"]
  return nil unless date_obj

  # Plist parser may return DateTime object directly
  if date_obj.is_a?(String)
    Time.parse(date_obj).utc.iso8601
  elsif date_obj.respond_to?(:to_time)
    date_obj.to_time.utc.iso8601
  else
    date_obj.to_s
  end
rescue StandardError => e
  Fontist.ui.error("Could not parse postedDate: #{e.message}")
  nil
end