Class: Contentful::Bootstrap::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/bootstrap/token.rb

Constant Summary collapse

CONFIG_ENV =
"CONTENTFUL_ENV".freeze
DEFAULT_SECTION =
"global".freeze
DEFAULT_PATH =
".contentfulrc".freeze
MANAGEMENT_TOKEN =
"CONTENTFUL_MANAGEMENT_ACCESS_TOKEN"
DELIVERY_TOKEN =
"CONTENTFUL_DELIVERY_ACCESS_TOKEN"

Class Method Summary collapse

Class Method Details

.config_fileObject



49
50
51
# File 'lib/contentful/bootstrap/token.rb', line 49

def self.config_file
  File.exist?(filename) ? IniFile.load(filename) : IniFile.new(filename: filename)
end

.config_pathObject



53
54
55
# File 'lib/contentful/bootstrap/token.rb', line 53

def self.config_path
  @@config_path ||= ""
end

.config_sectionObject



44
45
46
47
# File 'lib/contentful/bootstrap/token.rb', line 44

def self.config_section
  return ENV[CONFIG_ENV] if config_file.has_section? ENV[CONFIG_ENV]
  DEFAULT_SECTION
end

.filenameObject



39
40
41
42
# File 'lib/contentful/bootstrap/token.rb', line 39

def self.filename
  return config_path if File.exist?(config_path)
  File.join(ENV['HOME'], DEFAULT_PATH)
end

.present?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/contentful/bootstrap/token.rb', line 16

def self.present?
  return false unless File.exists? filename
  config_file[config_section].has_key? MANAGEMENT_TOKEN
end

.readObject



21
22
23
24
25
26
27
# File 'lib/contentful/bootstrap/token.rb', line 21

def self.read
  begin
    config_file[config_section].fetch(MANAGEMENT_TOKEN)
  rescue KeyError
    fail "Token not found"
  end
end

.set_path!(config_path = "") ⇒ Object



12
13
14
# File 'lib/contentful/bootstrap/token.rb', line 12

def self.set_path!(config_path = "")
  @@config_path = config_path
end

.write(token, key = MANAGEMENT_TOKEN) ⇒ Object



29
30
31
32
33
# File 'lib/contentful/bootstrap/token.rb', line 29

def self.write(token, key = MANAGEMENT_TOKEN)
  file = config_file
  file[config_section][key] = token
  file.save
end

.write_access_token(token) ⇒ Object



35
36
37
# File 'lib/contentful/bootstrap/token.rb', line 35

def self.write_access_token(token)
  write(token, DELIVERY_TOKEN)
end