Class: GemContribute::TokenStore

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_contribute/token_store.rb

Overview

Reads / writes ~/.config/gem-contribute/auth.json (mode 0600), keyed by host. The per-host structure means GitLab / Codeberg adapters drop in without rearranging storage. See ADR-0001 and ADR-0004.

File schema:

{
  "github.com": {
    "access_token": "gho_...",
    "scope": "public_repo",
    "stored_at": 1730000000
  }
}

Honors XDG_CONFIG_HOME so tests stay hermetic and unusual layouts work.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: TokenStore.default_path, clock: -> { Time.now.to_i }) ⇒ TokenStore

Returns a new instance of TokenStore.



22
23
24
25
# File 'lib/gem_contribute/token_store.rb', line 22

def initialize(path: TokenStore.default_path, clock: -> { Time.now.to_i })
  @path = path
  @clock = clock
end

Class Method Details

.default_pathObject



59
60
61
62
# File 'lib/gem_contribute/token_store.rb', line 59

def self.default_path
  base = ENV["XDG_CONFIG_HOME"] || File.expand_path("~/.config")
  File.join(base, "gem-contribute", "auth.json")
end

Instance Method Details

#delete(host) ⇒ Object



48
49
50
51
52
53
# File 'lib/gem_contribute/token_store.rb', line 48

def delete(host)
  data = read
  removed = data.delete(host)
  write(data) if removed
  removed
end

#entry_for(host) ⇒ Hash?

Returns scope, stored_at or nil.

Returns:

  • (Hash, nil)

    scope, stored_at or nil



34
35
36
# File 'lib/gem_contribute/token_store.rb', line 34

def entry_for(host)
  read[host]
end

#hostsObject



55
56
57
# File 'lib/gem_contribute/token_store.rb', line 55

def hosts
  read.keys
end

#store(host, access_token:, scope: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/gem_contribute/token_store.rb', line 38

def store(host, access_token:, scope: nil)
  data = read
  data[host] = {
    "access_token" => access_token,
    "scope" => scope,
    "stored_at" => @clock.call
  }.compact
  write(data)
end

#token_for(host) ⇒ String?

Returns the cached access token for the host, or nil.

Returns:

  • (String, nil)

    the cached access token for the host, or nil



28
29
30
31
# File 'lib/gem_contribute/token_store.rb', line 28

def token_for(host)
  data = read
  data.dig(host, "access_token")
end