From 36bc1e1503d681bf02165a8947adb32a406e9381 Mon Sep 17 00:00:00 2001 From: Étienne Deparis Date: Mon, 17 Aug 2020 13:54:26 +0200 Subject: feat: Do not use curl to fetch emojis list --- bin/convert_github_emojis.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/bin/convert_github_emojis.rb b/bin/convert_github_emojis.rb index 15ed014..184fd8f 100755 --- a/bin/convert_github_emojis.rb +++ b/bin/convert_github_emojis.rb @@ -1,15 +1,20 @@ #!/usr/bin/env ruby # frozen_string_literal: true +require 'net/http' require 'json' require 'uri' -# emojis.json should be downloaded from https://api.github.com/emojis first. -unless File.exist?('tmp/emojis.json') - Dir.mkdir 'tmp' unless Dir.exist?('tmp') - system('curl', '-o', 'tmp/emojis.json', 'https://api.github.com/emojis') +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(IO.read('tmp/emojis.json')) + +source = JSON.parse(uri_fetch('https://api.github.com/emojis').body) data = {} source.each do |k, v| -- cgit v1.2.1