Module: Emoninja::Data

Includes:
Kungfuig
Defined in:
lib/emoninja/data.rb

Overview

Utility to grab consortium’s data for emojis

Class Method Summary collapse

Class Method Details

.argoObject

Returns Hashie::Mash.

Returns:

  • Hashie::Mash



39
40
41
42
43
44
45
# File 'lib/emoninja/data.rb', line 39

def argo
  @argo ||= data.each_with_object(Hashie::Mash.new) do |h, memo|
    h[:keywords].each do |kw|
      (memo[kw] ||= []) << h[:glyph] unless stopword?(kw)
    end
  end
end

.dataObject



11
12
13
# File 'lib/emoninja/data.rb', line 11

def data
  @data ||= Grabber.load
end

.emoji(term, kind: :glyph, exact: false, number: nil, lang: nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/emoninja/data.rb', line 49

def emoji term, kind: :glyph, exact: false, number: nil, lang: nil
  match = exact(term, lang: lang)[kind]
  return match if match || exact

  return nil unless (match = keywords(term, lang: lang).map { |kw| kw[kind] })
  number ? match[number] : match.sample
end

.exact(term, lang: nil) ⇒ Object

Returns Hashie::Mash.

Returns:

  • Hashie::Mash



26
27
28
29
# File 'lib/emoninja/data.rb', line 26

def exact term, lang: nil
  term = marshal term, lang
  Hashie::Mash.new(cache[:exacts][term.to_s.downcase] ? data[cache[:exacts][term.to_s.downcase]] : {})
end

.keywords(term, lang: nil) ⇒ Hashie::Mash

Returns:

  • (Hashie::Mash)


20
21
22
23
# File 'lib/emoninja/data.rb', line 20

def keywords term, lang: nil
  term = marshal term, lang
  (cache[:keywords][term.to_s.downcase] || []).map(&data.method(:[])).map(&Hashie::Mash.method(:new))
end

.stopword?(term) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/emoninja/data.rb', line 15

def stopword? term
  term.length < STOPWORD_MIN_LENGTH || options[:stopwords].include?(term)
end

.vocabularyObject

Returns Hash.

Returns:

  • Hash



34
35
36
# File 'lib/emoninja/data.rb', line 34

def vocabulary
  @vocabulary ||= data.map { |h| [h[:name], h[:glyph]] }.to_h.reject { |k, _| stopword?(k) }
end