Class: TanukiEmoji::Db::EmojiDataParser

Inherits:
Object
  • Object
show all
Defined in:
lib/tanuki_emoji/db/emoji_data_parser.rb

Overview

Reads and extract content from emoji-data.txt and its metadata

Constant Summary collapse

DATA_FILE =
"#{::TanukiEmoji::Db::UNICODE_DATA_DIR}/emoji-data.txt".freeze
PROPERTIES =
{
  'Emoji' => :emoji,
  'Emoji_Presentation' => :emoji_presentation,
  'Emoji_Modifier' => :emoji_modifier,
  'Emoji_Modifier_Base' => :emoji_modifier_base,
  'Emoji_Component' => :emoji_component,
  'Extended_Pictographic' => :extended_pictographic
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_file = self.class.data_file) ⇒ EmojiDataParser

Returns a new instance of EmojiDataParser.



30
31
32
# File 'lib/tanuki_emoji/db/emoji_data_parser.rb', line 30

def initialize(data_file = self.class.data_file)
  @data_file = data_file
end

Instance Attribute Details

#data_fileObject (readonly)

Returns the value of attribute data_file.



28
29
30
# File 'lib/tanuki_emoji/db/emoji_data_parser.rb', line 28

def data_file
  @data_file
end

Class Method Details

.data_filePathname

Return the path to the default data file (emoji-data.txt)

Returns:

  • (Pathname)

    path to the default data file



24
25
26
# File 'lib/tanuki_emoji/db/emoji_data_parser.rb', line 24

def self.data_file
  Pathname.new(File.expand_path(File.join(__dir__, '../../../', DATA_FILE)))
end

Instance Method Details

#dataArray<EmojiData>

Return the parsed data from the data file

Returns:

  • (Array<EmojiData>)

    collection of EmojiData



37
38
39
40
41
42
43
44
45
46
# File 'lib/tanuki_emoji/db/emoji_data_parser.rb', line 37

def data
  parsed = []
  load do |line|
    parse_data(line).tap do |result|
      parsed << result unless result.nil?
    end
  end

  parsed
end

#metadataObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tanuki_emoji/db/emoji_data_parser.rb', line 57

def 
  parsed = []

  load do |line|
    (line).tap do |result|
      parsed << result unless result.nil?
    end
  end

  meta = {}

  # Extract date
  raw_datetime = parsed.detect { |data| data[:key] == "Date" }&.fetch(:value)
  meta[:date] = DateTime.parse(raw_datetime)

  # Extract version
  meta[:version] = parsed.detect { |data| data[:key] == "Version" }&.fetch(:value)

  # Extract total elements
  meta[:total_elements] = parsed.select { |data| data[:key] == "Total elements" }.sum { |data| data[:value].to_i }

  meta
end

#raw_dataObject



48
49
50
51
52
53
54
55
# File 'lib/tanuki_emoji/db/emoji_data_parser.rb', line 48

def raw_data
  lines = []
  load do |line|
    lines << line
  end

  lines
end