Class: Raindrop::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/raindrop/config.rb

Constant Summary collapse

CONFIG_DIR_MODE =
0o700
CONFIG_FILE_MODE =
0o600

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



15
16
17
# File 'lib/raindrop/config.rb', line 15

def initialize
  @path = File.expand_path(self.class.default_path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/raindrop/config.rb', line 13

def path
  @path
end

Class Method Details

.default_pathObject



19
20
21
22
23
24
25
# File 'lib/raindrop/config.rb', line 19

def self.default_path
  config_home = ENV.fetch("XDG_CONFIG_HOME") do
    File.join(Dir.home, ".config")
  end

  File.join(config_home, "raindrop-cli", "config.yml")
end

Instance Method Details

#access_tokenObject



27
28
29
# File 'lib/raindrop/config.rb', line 27

def access_token
  data.dig("auth", "access_token").to_s.strip
end

#auth_typeObject



31
32
33
# File 'lib/raindrop/config.rb', line 31

def auth_type
  data.dig("auth", "type").to_s.strip
end

#delete_access_tokenObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/raindrop/config.rb', line 64

def delete_access_token
  return false unless File.file?(@path)

  data = read_data
  token = data.dig("auth", "access_token").to_s.strip
  return false if token.empty?

  data.delete("auth")
  write_data(data)

  true
end

#expires_inObject



43
44
45
# File 'lib/raindrop/config.rb', line 43

def expires_in
  data.dig("auth", "expires_in")
end

#refresh_token?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/raindrop/config.rb', line 35

def refresh_token?
  !data.dig("auth", "refresh_token").to_s.strip.empty?
end

#save_oauth_token(payload) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/raindrop/config.rb', line 47

def save_oauth_token(payload)
  access_token = payload["access_token"].to_s.strip
  raise ConfigError, "OAuth response did not include an access token." if access_token.empty?

  update do |data|
    data["auth"] = {
      "type" => "oauth",
      "access_token" => access_token
    }
    data["auth"]["refresh_token"] = payload["refresh_token"].to_s unless payload["refresh_token"].to_s.empty?
    data["auth"]["token_type"] = payload["token_type"].to_s unless payload["token_type"].to_s.empty?
    data["auth"]["expires_in"] = payload["expires_in"] if payload.key?("expires_in")
  end

  true
end

#token_typeObject



39
40
41
# File 'lib/raindrop/config.rb', line 39

def token_type
  data.dig("auth", "token_type").to_s.strip
end