Class: Lich::Util::Update::GitHubClient

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(cache_ttl: 60) ⇒ void

Initializes a new GitHubClient instance.

Parameters:

  • cache_ttl (Integer) (defaults to: 60)

    time-to-live for cache in seconds



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_cacheObject (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.

Examples:

Fetching repository data

client = GitHubClient.new
data = client.fetch_github_json("https://api.github.com/repos/user/repo")

Parameters:

  • url (String)

    the GitHub API URL to fetch data from

Returns:

  • (Hash, nil)

    parsed JSON data or nil if an error occurs



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.message}"
    nil
  end
end

#github_tokenString?

Note:

The token is loaded from DATA_DIR/githubtoken.txt

Retrieves the GitHub authentication token from the specified file.

Returns:

  • (String, nil)

    the Bearer token or nil if not found or empty



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.

Parameters:

  • url (String)

    the URL to send the GET request to

  • auth (Boolean) (defaults to: true)

    whether to include authentication token (default: true)

Returns:

  • (String, nil)

    response body or nil if an error occurs

Raises:

  • (StandardError)

    if a network error occurs



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.message}]"
  nil
end