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
ORGANIZATION_ID =
'CONTENTFUL_ORGANIZATION_ID'.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.



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

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

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



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

def config_path
  @config_path
end

Instance Method Details

#==(other) ⇒ Object



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

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

#config_fileObject



68
69
70
# File 'lib/contentful/bootstrap/token.rb', line 68

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

#config_sectionObject



63
64
65
66
# File 'lib/contentful/bootstrap/token.rb', line 63

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

#filenameObject



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

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

#present?Boolean

Returns:

  • (Boolean)


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

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

#readObject



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

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

#read_organization_idObject



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

def read_organization_id
  config_file[config_section].fetch(ORGANIZATION_ID, nil)
end

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



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

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



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

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

#write_organization_id(organization_id) ⇒ Object



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

def write_organization_id(organization_id)
  write(organization_id, nil, ORGANIZATION_ID)
end

#write_space_id(space_name, space_id) ⇒ Object



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

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