contentful_bootstrap 1.6.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +26 -0
  3. data/.rubocop_todo.yml +123 -0
  4. data/.travis.yml +6 -0
  5. data/CHANGELOG.md +25 -0
  6. data/Gemfile +1 -0
  7. data/Guardfile +5 -0
  8. data/README.md +40 -14
  9. data/Rakefile +7 -0
  10. data/bin/contentful_bootstrap +33 -22
  11. data/contentful_bootstrap.gemspec +7 -0
  12. data/lib/contentful/bootstrap.rb +7 -5
  13. data/lib/contentful/bootstrap/command_runner.rb +45 -0
  14. data/lib/contentful/bootstrap/commands.rb +3 -168
  15. data/lib/contentful/bootstrap/commands/base.rb +66 -0
  16. data/lib/contentful/bootstrap/commands/create_space.rb +111 -0
  17. data/lib/contentful/bootstrap/commands/generate_json.rb +41 -0
  18. data/lib/contentful/bootstrap/commands/generate_token.rb +52 -0
  19. data/lib/contentful/bootstrap/constants.rb +2 -2
  20. data/lib/contentful/bootstrap/generator.rb +94 -0
  21. data/lib/contentful/bootstrap/server.rb +25 -17
  22. data/lib/contentful/bootstrap/support.rb +3 -2
  23. data/lib/contentful/bootstrap/templates.rb +4 -4
  24. data/lib/contentful/bootstrap/templates/base.rb +51 -30
  25. data/lib/contentful/bootstrap/templates/blog.rb +33 -33
  26. data/lib/contentful/bootstrap/templates/catalogue.rb +103 -103
  27. data/lib/contentful/bootstrap/templates/gallery.rb +55 -56
  28. data/lib/contentful/bootstrap/templates/json_template.rb +22 -16
  29. data/lib/contentful/bootstrap/templates/links.rb +2 -2
  30. data/lib/contentful/bootstrap/templates/links/asset.rb +2 -2
  31. data/lib/contentful/bootstrap/templates/links/base.rb +3 -3
  32. data/lib/contentful/bootstrap/templates/links/entry.rb +2 -2
  33. data/lib/contentful/bootstrap/token.rb +32 -31
  34. data/lib/contentful/bootstrap/version.rb +1 -1
  35. data/spec/contentful/bootstrap/command_runner_spec.rb +111 -0
  36. data/spec/contentful/bootstrap/commands/base_spec.rb +102 -0
  37. data/spec/contentful/bootstrap/commands/create_space_spec.rb +72 -0
  38. data/spec/contentful/bootstrap/commands/generate_json_spec.rb +64 -0
  39. data/spec/contentful/bootstrap/commands/generate_token_spec.rb +82 -0
  40. data/spec/contentful/bootstrap/generator_spec.rb +15 -0
  41. data/spec/contentful/bootstrap/server_spec.rb +154 -0
  42. data/spec/contentful/bootstrap/support_spec.rb +27 -0
  43. data/spec/contentful/bootstrap/templates/base_spec.rb +34 -0
  44. data/spec/contentful/bootstrap/templates/blog_spec.rb +32 -0
  45. data/spec/contentful/bootstrap/templates/catalogue_spec.rb +40 -0
  46. data/spec/contentful/bootstrap/templates/gallery_spec.rb +40 -0
  47. data/spec/contentful/bootstrap/templates/json_template_spec.rb +52 -0
  48. data/spec/contentful/bootstrap/templates/links/asset_spec.rb +23 -0
  49. data/spec/contentful/bootstrap/templates/links/base_spec.rb +31 -0
  50. data/spec/contentful/bootstrap/templates/links/entry_spec.rb +23 -0
  51. data/spec/contentful/bootstrap/token_spec.rb +143 -0
  52. data/spec/fixtures/ini_fixtures/contentfulrc.ini +2 -0
  53. data/spec/fixtures/ini_fixtures/no_global.ini +2 -0
  54. data/spec/fixtures/ini_fixtures/no_token.ini +1 -0
  55. data/spec/fixtures/ini_fixtures/sections.ini +5 -0
  56. data/spec/fixtures/json_fixtures/issue_22.json +77 -0
  57. data/spec/fixtures/json_fixtures/simple.json +34 -0
  58. data/spec/fixtures/json_fixtures/wl1z0pal05vy.json +437 -0
  59. data/spec/fixtures/vcr_fixtures/generate_json.yml +384 -0
  60. data/spec/fixtures/vcr_fixtures/issue_22.yml +2488 -0
  61. data/spec/spec_helper.rb +51 -0
  62. metadata +167 -4
@@ -19,7 +19,14 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.6"
21
21
  spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "vcr"
24
+ spec.add_development_dependency "webmock"
25
+ spec.add_development_dependency "rr"
26
+ spec.add_development_dependency "guard"
27
+ spec.add_development_dependency "guard-rspec"
22
28
  spec.add_runtime_dependency "launchy"
23
29
  spec.add_runtime_dependency "contentful-management"
30
+ spec.add_runtime_dependency "contentful", "~> 0.7"
24
31
  spec.add_runtime_dependency "inifile"
25
32
  end
@@ -1,5 +1,7 @@
1
- require "contentful/bootstrap/version"
2
- require "contentful/bootstrap/server"
3
- require "contentful/bootstrap/commands"
4
- require "contentful/bootstrap/templates"
5
- require "contentful/bootstrap/token"
1
+ require 'contentful/bootstrap/token'
2
+ require 'contentful/bootstrap/server'
3
+ require 'contentful/bootstrap/version'
4
+ require 'contentful/bootstrap/commands'
5
+ require 'contentful/bootstrap/templates'
6
+ require 'contentful/bootstrap/command_runner'
7
+ require 'contentful/bootstrap/templates/links'
@@ -0,0 +1,45 @@
1
+ require 'contentful/bootstrap/token'
2
+ require 'contentful/bootstrap/commands'
3
+
4
+ module Contentful
5
+ module Bootstrap
6
+ class CommandRunner
7
+ attr_reader :config_path, :token
8
+
9
+ def initialize(config_path = '')
10
+ @config_path = config_path
11
+ @token = Token.new(config_path)
12
+ end
13
+
14
+ def create_space(space_name, options = {})
15
+ template_name = options.fetch(:template, nil)
16
+ json_template = options.fetch(:json_template, nil)
17
+ trigger_oauth = options.fetch(:trigger_oauth, true)
18
+
19
+ Contentful::Bootstrap::Commands::CreateSpace.new(
20
+ @token, space_name, template_name, json_template, trigger_oauth
21
+ ).run
22
+ end
23
+
24
+ def generate_token(space, options = {})
25
+ token_name = options.fetch(:name, 'Bootstrap Token')
26
+ trigger_oauth = options.fetch(:trigger_oauth, true)
27
+
28
+ Contentful::Bootstrap::Commands::GenerateToken.new(
29
+ @token, space, token_name, trigger_oauth
30
+ ).run
31
+ end
32
+
33
+ def generate_json(space_id, options = {})
34
+ filename = options.fetch(:filename, nil)
35
+ access_token = options.fetch(:access_token, nil)
36
+
37
+ fail 'Access Token required' if access_token.nil?
38
+
39
+ Contentful::Bootstrap::Commands::GenerateJson.new(
40
+ space_id, access_token, filename
41
+ ).run
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,168 +1,3 @@
1
- require "net/http"
2
- require "contentful/management"
3
- require "contentful/management/request"
4
- require "contentful/management/error"
5
- require "contentful/bootstrap/token"
6
- require "contentful/bootstrap/server"
7
- require "contentful/bootstrap/support"
8
- require "contentful/bootstrap/templates"
9
- require "contentful/bootstrap/version"
10
-
11
- module Contentful
12
- module Bootstrap
13
- class Commands
14
- include Support
15
-
16
- def initialize(config_path = "")
17
- Token.set_path!(config_path)
18
- end
19
-
20
- def create_space(space_name, options = {})
21
- template_name = options.fetch(:template, nil)
22
- json_template = options.fetch(:json_template, nil)
23
- trigger_oauth = options.fetch(:trigger_oauth, true)
24
-
25
- get_configuration if trigger_oauth
26
-
27
- management_client_init
28
-
29
- puts "Creating Space '#{space_name}'"
30
- space = nil
31
- begin
32
- space = Contentful::Management::Space.create(name: space_name)
33
- rescue Contentful::Management::NotFound
34
- puts "Your account has multiple organizations:"
35
- puts get_organizations.join("\n")
36
- print "Please insert the Organization ID you'd want to create the spaces for: "
37
- organization_id = gets.chomp
38
- space = Contentful::Management::Space.create(name: space_name, organization_id: organization_id)
39
- end
40
-
41
- puts "Space '#{space_name}' created!"
42
- puts
43
-
44
- unless template_name.nil?
45
- if templates.has_key? template_name.to_sym
46
- puts "Creating Template '#{template_name}'"
47
-
48
- templates[template_name.to_sym].new(space).run
49
- puts "Template '#{template_name}' created!"
50
- else
51
- puts "Template '#{template_name}' not found. Valid templates are '#{templates.keys.map(&:to_s).join('\', \'')}'"
52
- end
53
- puts
54
- end
55
-
56
- unless json_template.nil?
57
- if File.exist?(json_template)
58
- puts "Creating JSON Template '#{json_template}'"
59
- Templates::JsonTemplate.new(space, json_template).run
60
- puts "JSON Template '#{json_template}' created!"
61
- else
62
- puts "JSON Template '#{json_template}' does not exist. Please check that you specified the correct file name."
63
- end
64
- puts
65
- end
66
-
67
- token = generate_token(space, trigger_oauth: false)
68
- puts
69
- puts "Space ID: '#{space.id}'"
70
- puts "Access Token: '#{token}'"
71
- puts
72
- puts "You can now insert those values into your configuration blocks"
73
- puts "Manage your content at https://app.contentful.com/spaces/#{space.id}"
74
- end
75
-
76
- def generate_token(space, options = {})
77
- token_name = options.fetch(:name, "Bootstrap Token")
78
- trigger_oauth = options.fetch(:trigger_oauth, true)
79
-
80
- get_configuration if trigger_oauth
81
- management_client_init if trigger_oauth
82
-
83
- if space.is_a?(String)
84
- space = Contentful::Management::Space.find(space)
85
- end
86
-
87
- puts
88
- puts "Creating Delivery API Token"
89
-
90
- response = Contentful::Management::Request.new(
91
- "/#{space.id}/api_keys",
92
- 'name' => token_name,
93
- 'description' => "Created with 'contentful_bootstrap.rb v#{Contentful::Bootstrap::VERSION}'"
94
- ).post
95
- fail response if response.object.is_a?(Contentful::Management::Error)
96
- token = response.object["accessToken"]
97
-
98
- puts "Token '#{token_name}' created! - '#{token}'"
99
- print "Do you want to write the Delivery Token to your configuration file? (Y/n): "
100
- unless gets.chomp.downcase == "n"
101
- Token.write_access_token(space.name, token)
102
- Token.write_space_id(space.name, space.id)
103
- end
104
-
105
- token
106
- end
107
-
108
- private
109
- def get_configuration
110
- if !Token.present?
111
- print "OAuth Token not found, do you want to create a new configuration file? (Y/n): "
112
- if gets.chomp.downcase == "n"
113
- puts "Exiting!"
114
- return
115
- end
116
- puts "Configuration will be saved on #{Token.filename}"
117
-
118
- puts "A new tab on your browser will open for requesting OAuth permissions"
119
- get_token
120
- else
121
- puts "OAuth token found, moving on!"
122
- end
123
- puts
124
- end
125
-
126
- def management_client_init
127
- Contentful::Management::Client.new(Token.read, raise_errors: true)
128
- end
129
-
130
- def get_organizations
131
- client = management_client_init
132
- url = client.base_url.sub('spaces', 'token')
133
- response = Contentful::Management::Client.get_http(url, nil, client.request_headers)
134
- JSON.load(response.body.to_s)["includes"]["Organization"].map { |org|
135
- "%-20s #{org["sys"]["id"]}" % org["name"]
136
- }.sort
137
- end
138
-
139
- def get_token
140
- silence_stderr do # Don't show any WEBrick related stuff
141
- server = Server.new
142
-
143
- server.start
144
-
145
- while !server.running? # Wait for Server Init
146
- sleep(1)
147
- end
148
-
149
- Net::HTTP.get(URI('http://localhost:5123'))
150
-
151
- while !Token.present? # Wait for User to do OAuth cycle
152
- sleep(1)
153
- end
154
-
155
- server.stop
156
- end
157
- end
158
-
159
- def templates
160
- {
161
- blog: Templates::Blog,
162
- gallery: Templates::Gallery,
163
- catalogue: Templates::Catalogue
164
- }
165
- end
166
- end
167
- end
168
- end
1
+ require 'contentful/bootstrap/commands/create_space'
2
+ require 'contentful/bootstrap/commands/generate_token'
3
+ require 'contentful/bootstrap/commands/generate_json'
@@ -0,0 +1,66 @@
1
+ require 'net/http'
2
+ require 'contentful/management'
3
+ require 'contentful/bootstrap/server'
4
+ require 'contentful/bootstrap/support'
5
+
6
+ module Contentful
7
+ module Bootstrap
8
+ module Commands
9
+ class Base
10
+ include Support
11
+ attr_reader :space, :token
12
+
13
+ def initialize(token, space, trigger_oauth = true)
14
+ @token = token
15
+ configuration if trigger_oauth
16
+ management_client_init if trigger_oauth
17
+ @space = space
18
+ end
19
+
20
+ def run
21
+ fail 'must implement'
22
+ end
23
+
24
+ private
25
+
26
+ def management_client_init
27
+ Contentful::Management::Client.new(@token.read, raise_errors: true)
28
+ end
29
+
30
+ def configuration
31
+ if @token.present?
32
+ puts 'OAuth token found, moving on!'
33
+ return
34
+ end
35
+
36
+ print 'OAuth Token not found, do you want to create a new configuration file? (Y/n): '
37
+ if gets.chomp.downcase == 'n'
38
+ puts 'Exiting!'
39
+ exit
40
+ end
41
+
42
+ puts "Configuration will be saved on #{@token.filename}"
43
+ puts 'A new tab on your browser will open for requesting OAuth permissions'
44
+ token_server
45
+ puts
46
+ end
47
+
48
+ def token_server
49
+ silence_stderr do # Don't show any WEBrick related stuff
50
+ server = Contentful::Bootstrap::Server.new(@token)
51
+
52
+ server.start
53
+
54
+ sleep(1) until server.running? # Wait for Server Init
55
+
56
+ Net::HTTP.get(URI('http://localhost:5123'))
57
+
58
+ sleep(1) until @token.present? # Wait for User to do OAuth cycle
59
+
60
+ server.stop
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,111 @@
1
+ require 'contentful/management'
2
+ require 'contentful/management/error'
3
+ require 'contentful/bootstrap/templates'
4
+ require 'contentful/bootstrap/commands/base'
5
+ require 'contentful/bootstrap/commands/generate_token'
6
+
7
+ module Contentful
8
+ module Bootstrap
9
+ module Commands
10
+ class CreateSpace < Base
11
+ attr_reader :template_name, :json_template
12
+ def initialize(token, space_name, template_name = nil, json_template = nil, trigger_oauth = true)
13
+ super(token, space_name, trigger_oauth)
14
+ @template_name = template_name
15
+ @json_template = json_template
16
+ end
17
+
18
+ def run
19
+ puts "Creating Space '#{@space}'"
20
+
21
+ new_space = fetch_space
22
+
23
+ puts "Space '#{@space}' created!"
24
+ puts
25
+
26
+ create_template(new_space) unless @template_name.nil?
27
+ create_json_template(new_space) unless @json_template.nil?
28
+
29
+ access_token = generate_token(new_space)
30
+ puts
31
+ puts "Space ID: '#{new_space.id}'"
32
+ puts "Access Token: '#{access_token}'"
33
+ puts
34
+ puts 'You can now insert those values into your configuration blocks'
35
+ puts "Manage your content at https://app.contentful.com/spaces/#{new_space.id}"
36
+
37
+ new_space
38
+ end
39
+
40
+ protected
41
+
42
+ def fetch_space
43
+ new_space = nil
44
+ begin
45
+ new_space = Contentful::Management::Space.create(name: @space)
46
+ rescue Contentful::Management::NotFound
47
+ puts 'Your account has multiple organizations:'
48
+ puts organizations.join("\n")
49
+ print 'Please insert the Organization ID you\'d want to create the spaces for: '
50
+ organization_id = gets.chomp
51
+ new_space = Contentful::Management::Space.create(
52
+ name: @space,
53
+ organization_id: organization_id
54
+ )
55
+ end
56
+
57
+ new_space
58
+ end
59
+
60
+ private
61
+
62
+ def organizations
63
+ client = management_client_init
64
+ url = client.base_url.sub('spaces', 'token')
65
+ response = Contentful::Management::Client.get_http(url, nil, client.request_headers)
66
+ organization_ids = JSON.load(response.body.to_s)['includes']['Organization'].map do |org|
67
+ "#{org['name']} - #{org['sys']['id']}"
68
+ end
69
+ organization_ids.sort
70
+ end
71
+
72
+ def templates
73
+ {
74
+ blog: Templates::Blog,
75
+ gallery: Templates::Gallery,
76
+ catalogue: Templates::Catalogue
77
+ }
78
+ end
79
+
80
+ def create_template(space)
81
+ if templates.key? @template_name.to_sym
82
+ puts "Creating Template '#{@template_name}'"
83
+
84
+ templates[@template_name.to_sym].new(space).run
85
+ puts "Template '#{@template_name}' created!"
86
+ else
87
+ puts "Template '#{@template_name}' not found. Valid templates are '#{templates.keys.map(&:to_s).join('\', \'')}'"
88
+ end
89
+ puts
90
+ end
91
+
92
+ def create_json_template(space)
93
+ if ::File.exist?(@json_template)
94
+ puts "Creating JSON Template '#{@json_template}'"
95
+ Templates::JsonTemplate.new(space, @json_template).run
96
+ puts "JSON Template '#{@json_template}' created!"
97
+ else
98
+ puts "JSON Template '#{@json_template}' does not exist. Please check that you specified the correct file name."
99
+ end
100
+ puts
101
+ end
102
+
103
+ def generate_token(space)
104
+ Contentful::Bootstrap::Commands::GenerateToken.new(
105
+ @token, space, 'Bootstrap Token', false
106
+ ).run
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,41 @@
1
+ require 'contentful/bootstrap/generator'
2
+ require 'contentful/bootstrap/commands/base'
3
+
4
+ module Contentful
5
+ module Bootstrap
6
+ module Commands
7
+ class GenerateJson
8
+ attr_reader :space_id, :filename, :access_token
9
+ def initialize(space_id, access_token, filename = nil)
10
+ @space_id = space_id
11
+ @access_token = access_token
12
+ @filename = filename
13
+ end
14
+
15
+ def run
16
+ if access_token.nil?
17
+ puts 'Access Token not specified'
18
+ puts 'Exiting!'
19
+ exit
20
+ end
21
+
22
+ puts "Generating JSON Template '#{filename}' for Space: '#{space_id}'"
23
+
24
+ json = Contentful::Bootstrap::Generator.new(space_id, access_token).generate_json
25
+
26
+ puts
27
+ write(json)
28
+ end
29
+
30
+ def write(json)
31
+ if filename.nil?
32
+ puts "#{json}\n"
33
+ else
34
+ puts "Saving JSON template to '#{filename}'"
35
+ ::File.write(filename, json)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end