Class: Contentful::Bootstrap::Token
- Inherits:
-
Object
- Object
- Contentful::Bootstrap::Token
- 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
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #config_file ⇒ Object
- #config_section ⇒ Object
- #filename ⇒ Object
-
#initialize(config_path = '') ⇒ Token
constructor
A new instance of Token.
- #present? ⇒ Boolean
- #read ⇒ Object
- #write(value, section = nil, key = MANAGEMENT_TOKEN) ⇒ Object
- #write_access_token(space_name, token) ⇒ Object
- #write_space_id(space_name, space_id) ⇒ Object
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_path ⇒ Object (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_file ⇒ Object
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_section ⇒ Object
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 |
#filename ⇒ Object
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
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 |
#read ⇒ Object
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 |