Class: Contentful::Bootstrap::Commands

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/contentful/bootstrap/commands.rb

Instance Method Summary collapse

Methods included from Support

#silence_stderr

Constructor Details

#initialize(config_path = "") ⇒ Commands

Returns a new instance of Commands.



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

def initialize(config_path = "")
  Token.set_path!(config_path)
end

Instance Method Details

#create_space(space_name, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/contentful/bootstrap/commands.rb', line 20

def create_space(space_name, options = {})
  template_name = options.fetch(:template, nil)
  json_template = options.fetch(:json_template, nil)
  trigger_oauth = options.fetch(:trigger_oauth, true)

  get_configuration if trigger_oauth

  management_client_init

  puts "Creating Space '#{space_name}'"
  space = nil
  begin
    space = Contentful::Management::Space.create(name: space_name)
  rescue Contentful::Management::NotFound
    puts "Your account has multiple organizations:"
    puts get_organizations.join("\n")
    print "Please insert the Organization ID you'd want to create the spaces for: "
    organization_id = gets.chomp
    space = Contentful::Management::Space.create(name: space_name, organization_id: organization_id)
  end

  puts "Space '#{space_name}' created!"
  puts

  unless template_name.nil?
    if templates.has_key? template_name.to_sym
      puts "Creating Template '#{template_name}'"

      templates[template_name.to_sym].new(space).run
      puts "Template '#{template_name}' created!"
    else
      puts "Template '#{template_name}' not found. Valid templates are '#{templates.keys.map(&:to_s).join('\', \'')}'"
    end
    puts
  end

  unless json_template.nil?
    if File.exist?(json_template)
      puts "Creating JSON Template '#{json_template}'"
      Templates::JsonTemplate.new(space, json_template).run
      puts "JSON Template '#{json_template}' created!"
    else
      puts "JSON Template '#{json_template}' does not exist. Please check that you specified the correct file name."
    end
    puts
  end

  token = generate_token(space, trigger_oauth: false)
  puts
  puts "Space ID: '#{space.id}'"
  puts "Access Token: '#{token}'"
  puts
  puts "You can now insert those values into your configuration blocks"
  puts "Manage your content at https://app.contentful.com/spaces/#{space.id}"
end

#generate_token(space, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/contentful/bootstrap/commands.rb', line 76

def generate_token(space, options = {})
  token_name = options.fetch(:name, "Bootstrap Token")
  trigger_oauth = options.fetch(:trigger_oauth, true)

  get_configuration if trigger_oauth
  management_client_init if trigger_oauth

  if space.is_a?(String)
    space = Contentful::Management::Space.find(space)
  end

  puts
  puts "Creating Delivery API Token"

  response = Contentful::Management::Request.new(
    "/#{space.id}/api_keys",
    'name' => token_name,
    'description' => "Created with 'contentful_bootstrap.rb v#{Contentful::Bootstrap::VERSION}'"
  ).post
  fail response if response.object.is_a?(Contentful::Management::Error)
  token = response.object["accessToken"]

  puts "Token '#{token_name}' created! - '#{token}'"
  print "Do you want to write the Delivery Token to your configuration file? (Y/n): "
  unless gets.chomp.downcase == "n"
    Token.write_access_token(space.name, token)
    Token.write_space_id(space.name, space.id)
  end

  token
end