Class: Contentful::Bootstrap::Commands::GenerateToken

Inherits:
Base
  • Object
show all
Defined in:
lib/contentful/bootstrap/commands/generate_token.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#no_input, #options, #quiet, #space, #token

Instance Method Summary collapse

Methods inherited from Base

#client

Constructor Details

#initialize(token, space, options = {}) ⇒ GenerateToken

Returns a new instance of GenerateToken.



12
13
14
15
16
# File 'lib/contentful/bootstrap/commands/generate_token.rb', line 12

def initialize(token, space, options = {})
  @token_name = options.fetch(:token_name, 'Bootstrap Token')

  super(token, space, options)
end

Instance Attribute Details

#token_nameObject (readonly)

Returns the value of attribute token_name.



11
12
13
# File 'lib/contentful/bootstrap/commands/generate_token.rb', line 11

def token_name
  @token_name
end

Instance Method Details

#fetch_access_tokenObject



44
45
46
47
48
49
# File 'lib/contentful/bootstrap/commands/generate_token.rb', line 44

def fetch_access_token
  @actual_space.api_keys.create(
    name: token_name,
    description: "Created with 'contentful_bootstrap.rb v#{Contentful::Bootstrap::VERSION}'"
  ).access_token
end

#fetch_spaceObject



36
37
38
39
40
41
42
# File 'lib/contentful/bootstrap/commands/generate_token.rb', line 36

def fetch_space
  @actual_space = if @space.is_a?(String)
                    client.spaces.find(@space)
                  else
                    @space
                  end
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/contentful/bootstrap/commands/generate_token.rb', line 18

def run
  output 'Creating Delivery API Token'

  fetch_space

  access_token = fetch_access_token

  output "Token '#{token_name}' created! - '#{access_token}'"
  Support.input('Do you want to write the Delivery Token to your configuration file? (Y/n): ', no_input) do |answer|
    unless answer.casecmp('n').zero?
      @token.write_access_token(@actual_space.name, access_token)
      @token.write_space_id(@actual_space.name, @actual_space.id)
    end
  end

  access_token
end