Created
December 5, 2020 16:23
-
-
Save peterc/a556da117433f84e7924cd03da11ccc2 to your computer and use it in GitHub Desktop.
See GitHub API rate limit information in a useful way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Return GitHub API rate information in JSON format | |
# GitHub Personal Access Token expected in ~/.githubtoken | |
# I'd pipe this into jq for nicer viewing | |
require 'http' | |
require 'json' | |
TOKEN = File.read(ENV['HOME'] + "/.githubtoken").strip | |
res = HTTP["Authorization" => "token #{TOKEN}"].get("https://api.github.com/rate_limit") | |
rates = JSON.parse(res.to_s) | |
def convert_epoch_to_seconds_remaining(r) | |
r["resets_in_seconds"] = (Time.at(r["reset"].to_i) - Time.now).ceil if r["reset"] | |
r.values.each { |rr| convert_epoch_to_seconds_remaining(rr) if rr.is_a?(Hash) } | |
end | |
convert_epoch_to_seconds_remaining(rates) | |
puts rates.to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment