Class: Lich::Util::Update::GitHubClient
- Inherits:
-
Object
- Object
- Lich::Util::Update::GitHubClient
- Defined in:
- documented/common/update/github_client.rb
Overview
HTTP client for GitHub API with caching and token auth.
Provides JSON and raw GET requests with optional Bearer token auth from DATA_DIR/githubtoken.txt. Includes in-memory cache with TTL for API responses.
Instance Attribute Summary collapse
-
#http_cache ⇒ Object
readonly
Returns the value of attribute http_cache.
Instance Method Summary collapse
-
#fetch_github_json(url) ⇒ Hash?
Fetches JSON data from the specified GitHub URL with caching.
-
#github_token ⇒ String?
Retrieves the GitHub authentication token from the specified file.
-
#http_get(url, auth: true) ⇒ String?
Performs a GET request to the specified URL with optional authentication.
-
#initialize(cache_ttl: 60) ⇒ void
constructor
Initializes a new GitHubClient instance.
Constructor Details
#initialize(cache_ttl: 60) ⇒ void
Initializes a new GitHubClient instance.
26 27 28 29 30 31 |
# File 'documented/common/update/github_client.rb', line 26 def initialize(cache_ttl: 60) @http_cache = {} @cache_ttl = cache_ttl @github_token = nil @github_token_loaded = false end |
Instance Attribute Details
#http_cache ⇒ Object (readonly)
Returns the value of attribute http_cache.
21 22 23 |
# File 'documented/common/update/github_client.rb', line 21 def http_cache @http_cache end |
Instance Method Details
#fetch_github_json(url) ⇒ Hash?
Fetches JSON data from the specified GitHub URL with caching.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'documented/common/update/github_client.rb', line 39 def fetch_github_json(url) now = Time.now.to_i entry = @http_cache[url] if entry && (now - entry[:ts] < @cache_ttl) return entry[:data] end begin raw = http_get(url) return nil unless raw data = JSON.parse(raw) @http_cache[url] = { ts: now, data: data } data rescue => e respond "Update notice: network error fetching #{url.split('/repos/').last || url} (fetch_github_json): #{e.}" nil end end |
#github_token ⇒ String?
The token is loaded from DATA_DIR/githubtoken.txt
Retrieves the GitHub authentication token from the specified file.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'documented/common/update/github_client.rb', line 89 def github_token return @github_token if @github_token_loaded @github_token_loaded = true token_path = File.join(DATA_DIR, 'githubtoken.txt') return nil unless File.exist?(token_path) token = File.read(token_path).strip if token.empty? respond "[lich5-update: GitHub token file is empty. Using unauthenticated access.]" return nil end @github_token = "Bearer #{token}" end |
#http_get(url, auth: true) ⇒ String?
Performs a GET request to the specified URL with optional authentication.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'documented/common/update/github_client.rb', line 63 def http_get(url, auth: true) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == 'https') http.verify_mode = OpenSSL::SSL::VERIFY_PEER request = Net::HTTP::Get.new(uri.request_uri) if auth token = github_token request['Authorization'] = token if token end response = http.request(request) unless response.code == '200' respond "[lich5-update: HTTP #{response.code} fetching #{uri.path}]" return nil end response.body rescue => e respond "[lich5-update: Network error: #{e.}]" nil end |