Class: Everywhere::Platform::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/everywhere/platform/credentials.rb

Overview

Local credential store at ~/.config/everywhere/credentials.json (0600), keyed by Platform base URL so a dev localhost login and a production login coexist. Each entry holds an org-scoped token + the org it's bound to.

{ "https://platform.rubyeverywhere.com": {
  "token": "rbe_…",
  "organization": { "id": "org_…", "name": "Acme", "slug": "acme" } } }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Credentials

Returns a new instance of Credentials.



34
35
36
# File 'lib/everywhere/platform/credentials.rb', line 34

def initialize(data)
  @data = data
end

Class Method Details

.config_homeObject



20
21
22
# File 'lib/everywhere/platform/credentials.rb', line 20

def self.config_home
  ENV["XDG_CONFIG_HOME"].to_s.empty? ? File.expand_path("~/.config") : ENV["XDG_CONFIG_HOME"]
end

.loadObject



24
25
26
27
28
29
30
31
32
# File 'lib/everywhere/platform/credentials.rb', line 24

def self.load
  data =
    if File.exist?(path)
      JSON.parse(File.read(path)) rescue {}
    else
      {}
    end
  new(data.is_a?(Hash) ? data : {})
end

.pathObject



16
17
18
# File 'lib/everywhere/platform/credentials.rb', line 16

def self.path
  ENV["EVERYWHERE_CREDENTIALS"] || File.join(config_home, "everywhere", "credentials.json")
end

Instance Method Details

#delete(url) ⇒ Object



51
52
53
54
55
# File 'lib/everywhere/platform/credentials.rb', line 51

def delete(url)
  removed = @data.delete(normalize(url))
  save
  removed
end

#for(url) ⇒ Object



38
39
40
# File 'lib/everywhere/platform/credentials.rb', line 38

def for(url)
  @data[normalize(url)]
end

#logged_in?(url) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/everywhere/platform/credentials.rb', line 42

def logged_in?(url)
  !self.for(url).nil?
end

#set(url, entry) ⇒ Object



46
47
48
49
# File 'lib/everywhere/platform/credentials.rb', line 46

def set(url, entry)
  @data[normalize(url)] = entry
  save
end