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'.freeze
DELIVERY_TOKEN =
'CONTENTFUL_DELIVERY_ACCESS_TOKEN'.freeze
SPACE_ID =
'SPACE_ID'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path = '') ⇒ Token

Returns a new instance of Token.



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

def initialize(config_path = '')
  @config_path = config_path
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



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

def config_path
  @config_path
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
22
# File 'lib/contentful/bootstrap/token.rb', line 19

def ==(other)
  return false unless other.is_a?(Contentful::Bootstrap::Token)
  other.config_path == @config_path
end

#config_fileObject



59
60
61
# File 'lib/contentful/bootstrap/token.rb', line 59

def config_file
  @file ||= ::File.exist?(filename) ? IniFile.load(filename) : IniFile.new(filename: filename)
end

#config_sectionObject



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

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

#filenameObject



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

def filename
  return config_path unless config_path.empty?
  ::File.join(ENV['HOME'], DEFAULT_PATH)
end

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  return false unless ::File.exist? filename
  config_file[config_section].key? MANAGEMENT_TOKEN
end

#readObject



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

def read
  config_file[config_section].fetch(MANAGEMENT_TOKEN)
rescue KeyError
  raise 'Token not found'
end

#write(value, section = nil, key = MANAGEMENT_TOKEN) ⇒ Object



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

def write(value, section = nil, key = MANAGEMENT_TOKEN)
  file = config_file
  file[section || config_section][key] = value
  file.save
end

#write_access_token(space_name, token) ⇒ Object



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

def write_access_token(space_name, token)
  write(token, space_name, DELIVERY_TOKEN)
end

#write_space_id(space_name, space_id) ⇒ Object



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

def write_space_id(space_name, space_id)
  write(space_id, space_name, SPACE_ID)
end