module Emoninja::Grabber

Utility to grab consortium’s data for emojis

Constants

DATA_HTML

unicode.org/emoji/charts/full-emoji-list.html

LOCAL_CACHE
LOCAL_DATA

Public Class Methods

load() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/emoninja/grabber.rb, line 18
def load
  unless File.exist? LOCAL_CACHE
    open(DATA_HTML) do |f|
      File.write LOCAL_CACHE, f.read
    end
  end

  @data = begin
    if File.exist? LOCAL_DATA
      YAML.load File.read LOCAL_DATA
    else
      Nokogiri::HTML(open(LOCAL_CACHE)).xpath('//table/tr').tap(&:shift).map do |tr|
        {
          name: tr.children[30].text,
          keywords: tr.children[36].text.split(',').map(&:strip),
          code: tr.children[2].text, glyph: tr.children[4].text,
          apple: blob(tr, 6), google: blob(tr, 8), twitter: blob(tr, 10),
          one: blob(tr, 12), fbm: blob(tr, 14), windows: blob(tr, 16),
          samsung: blob(tr, 18)
        }
      end.tap { |data| File.write(LOCAL_DATA, data.to_yaml) }
    end
  rescue
    File.delete(LOCAL_DATA)
    retry
  end.tap do |data|
    data.each do |h|
      next unless h[:name] =~ /≊/

      vals = h[:name].split(/≊/).map(&:strip)
      h[:name] = vals.first
      data << h.dup.tap { |nick| nick[:name] = vals.last }
    end
  end
end

Private Class Methods

blob(tr, col) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/emoninja/grabber.rb, line 57
def blob tr, col
  tr.children[col].children.first.attributes['src'].value rescue nil
end