Skip to content

Instantly share code, notes, and snippets.

@peterc
Created December 5, 2020 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterc/a556da117433f84e7924cd03da11ccc2 to your computer and use it in GitHub Desktop.
Save peterc/a556da117433f84e7924cd03da11ccc2 to your computer and use it in GitHub Desktop.
See GitHub API rate limit information in a useful way
# 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