summaryrefslogtreecommitdiffstats
path: root/bin/convert_github_emojis.rb
blob: 184fd8f7f7c0227e97c7c962c965578cbdf222dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'net/http'
require 'json'
require 'uri'

def uri_fetch(uri_str, limit = 10)
  raise 'Infinit redirect loop' if limit.zero?
  response = Net::HTTP.get_response(URI(uri_str))
  if response.is_a?(Net::HTTPRedirection)
    return uri_fetch(response['location'], limit - 1)
  end
  response
end

source = JSON.parse(uri_fetch('https://api.github.com/emojis').body)

data = {}
source.each do |k, v|
  filename = File.basename(URI.parse(v).path, '.png')
  if filename =~ /[g-z]+/i
    warn "#{filename} is not a valid UTF8 codepoint"
  else
    data[k] = filename.split('-').map { |c| [c.hex].pack('U') }.join('')
  end
end

output = []
data.each do |k, v|
  output << "':#{k}:' => '#{v}'"
end

IO.write('lib/emojis.rb', 'EMOJIS_LIST = {' + output.join(',') + '}.freeze')