Class: Slk::Services::GemojiSync

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/services/gemoji_sync.rb

Overview

Downloads and caches standard emoji database from gemoji

Constant Summary collapse

GEMOJI_URL =
'https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json'
NETWORK_ERRORS =
[
  SocketError,
  Errno::ECONNREFUSED,
  Errno::ETIMEDOUT,
  Net::OpenTimeout,
  Net::ReadTimeout,
  URI::InvalidURIError,
  OpenSSL::SSL::SSLError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir:, on_progress: nil) ⇒ GemojiSync

Returns a new instance of GemojiSync.



19
20
21
22
# File 'lib/slk/services/gemoji_sync.rb', line 19

def initialize(cache_dir:, on_progress: nil)
  @cache_dir = cache_dir
  @on_progress = on_progress
end

Instance Method Details

#emoji_json_pathObject



41
42
43
# File 'lib/slk/services/gemoji_sync.rb', line 41

def emoji_json_path
  File.join(@cache_dir, 'gemoji.json')
end

#syncHash

Download and cache standard emoji database

Returns:

  • (Hash)

    Result with :success, :count, :path, or :error



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/slk/services/gemoji_sync.rb', line 26

def sync
  @on_progress&.call('Downloading standard emoji database...')

  response = fetch_gemoji_data
  return response if response[:error]

  emoji_map = parse_and_transform(response[:body])
  return emoji_map if emoji_map[:error]

  save_result = save_to_cache(emoji_map[:data])
  return save_result if save_result[:error]

  { success: true, count: emoji_map[:data].size, path: emoji_json_path }
end