jekyll-contentful-data-import 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+
4
+ describe Jekyll::Contentful::DataExporter do
5
+ subject { described_class.new('foo', []) }
6
+
7
+ describe 'instance methods' do
8
+ it '#destination_directory' do
9
+ expected = File.join(Dir.pwd, '_data', 'contentful', 'spaces')
10
+ expect(subject.destination_directory).to eq(expected)
11
+ end
12
+
13
+ it '#destination_file' do
14
+ expected = File.join(Dir.pwd, '_data', 'contentful', 'spaces', 'foo.yaml')
15
+ expect(subject.destination_file).to eq(expected)
16
+ end
17
+
18
+ it '#setup_directory' do
19
+ expect(Dir).to receive(:mkdir).with(File.join(Dir.pwd, '_data'))
20
+ expect(Dir).to receive(:mkdir).with(File.join(Dir.pwd, '_data', 'contentful'))
21
+ expect(Dir).to receive(:mkdir).with(File.join(Dir.pwd, '_data', 'contentful', 'spaces'))
22
+
23
+ subject.setup_directory
24
+ end
25
+
26
+ describe '#run' do
27
+ before do
28
+ allow(subject).to receive(:setup_directory)
29
+ end
30
+
31
+ it 'creates or overwrites data file' do
32
+ expect(File).to receive(:open).with(subject.destination_file, 'w')
33
+ subject.run
34
+ end
35
+
36
+ it 'serializes entries onto data file' do
37
+ allow(File).to receive(:open).and_yield(StringIO.new)
38
+ expect_any_instance_of(::Jekyll::Contentful::Serializer).to receive(:to_yaml)
39
+
40
+ subject.run
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ class ClientDouble
4
+ def entries(options = {})
5
+ []
6
+ end
7
+ end
8
+
9
+ class ExporterDouble
10
+ def run; end
11
+ end
12
+
13
+ describe Jekyll::Contentful::Importer do
14
+ let(:config) do
15
+ {
16
+ 'spaces' => [
17
+ {
18
+ 'example' => {
19
+ 'space' => 'cfexampleapi',
20
+ 'access_token' => 'b4c0n73n7fu1'
21
+ }
22
+ }
23
+ ]
24
+ }
25
+ end
26
+ subject { described_class.new(config) }
27
+
28
+ describe 'instance methods' do
29
+ it '#spaces' do
30
+ expect(subject.spaces).to match([['example', {'space' => 'cfexampleapi', 'access_token' => 'b4c0n73n7fu1'}]])
31
+ end
32
+
33
+ describe '#client' do
34
+ it 'creates client with some defaults' do
35
+ expect(::Contentful::Client).to receive(:new).with(space: 'foo', access_token: 'foobar', dynamic_entries: :auto, raise_errors: true)
36
+
37
+ subject.client('foo', 'foobar')
38
+ end
39
+
40
+ it 'can override the defaults' do
41
+ expect(::Contentful::Client).to receive(:new).with(space: 'foo', access_token: 'foobar', dynamic_entries: :auto, raise_errors: false)
42
+
43
+ subject.client('foo', 'foobar', raise_errors: false)
44
+ end
45
+ end
46
+
47
+ describe '#run' do
48
+ it 'runs exporter for each space' do
49
+ allow(subject).to receive(:spaces).and_return([['foo', {}], ['bar', {}]])
50
+ allow(subject).to receive(:client).and_return(ClientDouble.new)
51
+
52
+ expect(Jekyll::Contentful::DataExporter).to receive(:new).and_return(ExporterDouble.new).twice
53
+
54
+ subject.run
55
+ end
56
+
57
+ it 'runs exporter with correct arguments' do
58
+ allow(subject).to receive(:client).and_return(ClientDouble.new)
59
+
60
+ expect(Jekyll::Contentful::DataExporter).to receive(:new).with('example', [], config['spaces'].first['example']).and_return(ExporterDouble.new)
61
+
62
+ subject.run
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+
3
+ class SomeMapper
4
+ def initialize(entry, config); end
5
+ def map; end
6
+ end
7
+
8
+ describe Jekyll::Contentful::Mappers::Base do
9
+ let(:entry) { EntryDouble.new('foo') }
10
+ subject { described_class.new(entry, {}) }
11
+
12
+ describe 'class methods' do
13
+ describe '::mapper_for' do
14
+ it 'returns default mapper if no config sent' do
15
+ expect(described_class.mapper_for(entry, {})).to be_a described_class
16
+ end
17
+
18
+ it 'returns configured mapper if config sent' do
19
+ config = {'content_types' => { entry.content_type.id => 'SomeMapper' } }
20
+ expect(described_class.mapper_for(entry, config)).to be_a SomeMapper
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'instance methods' do
26
+ describe '#map' do
27
+ class FileDouble
28
+ attr_reader :url
29
+
30
+ def initialize(url)
31
+ @url = url
32
+ end
33
+ end
34
+
35
+ class AssetDouble < Contentful::Asset
36
+ attr_reader :title, :file
37
+ def initialize(title, url)
38
+ @title = title
39
+ @file = FileDouble.new(url)
40
+ end
41
+ end
42
+
43
+ class LocationDouble < Contentful::Location
44
+ def initialize(lat, lon)
45
+ @lat = lat
46
+ @lon = lon
47
+ end
48
+
49
+ def properties
50
+ { 'lat' => @lat, 'lon' => @lon }
51
+ end
52
+ end
53
+
54
+ class LinkDouble < Contentful::Link
55
+ attr_reader :id
56
+
57
+ def initialize(id)
58
+ @id = id
59
+ end
60
+ end
61
+
62
+ it 'maps a simple entry' do
63
+ expected = { 'sys' => { 'id' => 'foo' } }
64
+ expect(subject.map).to eq expected
65
+ end
66
+
67
+ it 'maps a complete entry' do
68
+ entry = EntryDouble.new('foo', ContentTypeDouble.new, {
69
+ 'asset' => AssetDouble.new('some_title', 'some_url'),
70
+ 'location' => LocationDouble.new(12.32, 43.34),
71
+ 'link' => LinkDouble.new('bar'),
72
+ 'entry' => EntryDouble.new('baz'),
73
+ 'array' => [
74
+ LinkDouble.new('foobar'),
75
+ 'blah'
76
+ ],
77
+ 'value' => 'foobar'
78
+ })
79
+
80
+ subject.instance_variable_set(:@entry, entry)
81
+
82
+ expected = {
83
+ 'sys' => { 'id' => 'foo' },
84
+ 'asset' => {
85
+ 'title' => 'some_title',
86
+ 'url' => 'some_url'
87
+ },
88
+ 'location' => {
89
+ 'lat' => 12.32,
90
+ 'lon' => 43.34
91
+ },
92
+ 'link' => {
93
+ 'sys' => { 'id' => 'bar' }
94
+ },
95
+ 'entry' => {
96
+ 'sys' => { 'id' => 'baz' }
97
+ },
98
+ 'array' => [
99
+ { 'sys' => { 'id' => 'foobar' } },
100
+ 'blah'
101
+ ],
102
+ 'value' => 'foobar'
103
+ }
104
+
105
+ expect(subject.map).to match expected
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jekyll::Contentful::Serializer do
4
+ let(:entries) { [EntryDouble.new('foo')] }
5
+ subject { described_class.new(entries, {}) }
6
+
7
+ describe 'instance methods' do
8
+ describe '#serialize' do
9
+ describe 'uses proper mapper' do
10
+ it 'uses default mappen if none specified in config' do
11
+ expect_any_instance_of(Jekyll::Contentful::Mappers::Base).to receive(:map)
12
+
13
+ subject.serialize
14
+ end
15
+
16
+ it 'uses specified mapper' do
17
+ subject.instance_variable_set(:@config, {'content_types' => {'' => 'MapperDouble'}})
18
+
19
+ expect_any_instance_of(MapperDouble).to receive(:map)
20
+
21
+ subject.serialize
22
+ end
23
+ end
24
+
25
+ it 'serializes a single entry without fields' do
26
+ expected = [{'sys' => {'id' => 'foo'}}]
27
+ expect(subject.serialize).to eq(expected)
28
+ end
29
+
30
+ it 'serializes a single entry with fields' do
31
+ subject.instance_variable_set(:@entries, [EntryDouble.new('foo', ContentTypeDouble.new, {'foobar' => 'bar'})])
32
+
33
+ expected = [{'sys' => {'id' => 'foo'}, 'foobar' => 'bar'}]
34
+ expect(subject.serialize).to eq(expected)
35
+ end
36
+
37
+ it 'serializes multiple entries' do
38
+ subject.instance_variable_set(:@entries, [
39
+ EntryDouble.new('foo', ContentTypeDouble.new, {'foobar' => 'bar'}),
40
+ EntryDouble.new('bar', ContentTypeDouble.new, {'foobar' => 'baz'})
41
+ ])
42
+
43
+ expected = [
44
+ {'sys' => {'id' => 'foo'}, 'foobar' => 'bar'},
45
+ {'sys' => {'id' => 'bar'}, 'foobar' => 'baz'}
46
+ ]
47
+ expect(subject.serialize).to match(expected)
48
+ end
49
+ end
50
+
51
+ it '#to_yaml' do
52
+ allow(subject).to receive(:serialize).and_return({'a' => 123})
53
+
54
+ expected = "---\na: 123\n"
55
+ expect(subject.to_yaml).to eq(expected)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jekyll::Commands::Contentful do
4
+ describe 'class methods' do
5
+ describe '::init_with_program' do
6
+ it 'implements jekyll command interface' do
7
+ expect(described_class.respond_to?(:init_with_program)).to be_truthy
8
+ end
9
+ end
10
+
11
+ describe '::process' do
12
+ it 'calls contentful importer' do
13
+ expect_any_instance_of(Jekyll::Contentful::Importer).to receive(:run)
14
+
15
+ described_class.process
16
+ end
17
+
18
+ it 'triggers a rebuild when --rebuild is sent' do
19
+ allow_any_instance_of(Jekyll::Contentful::Importer).to receive(:run)
20
+ expect(Jekyll::Commands::Build).to receive(:process)
21
+
22
+ described_class.process([], {'rebuild' => true})
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,48 @@
1
+ $LOAD_PATH.unshift File.expand_path('lib', __FILE__)
2
+
3
+ require 'vcr'
4
+ require 'yaml'
5
+ require 'rspec'
6
+
7
+ require 'jekyll'
8
+ require File.expand_path('../../lib/jekyll-contentful-data-import.rb', __FILE__)
9
+
10
+ VCR.configure do |config|
11
+ config.cassette_library_dir = "spec/fixtures/vcr_fixtures"
12
+ config.hook_into :webmock
13
+ end
14
+
15
+ def vcr(cassette)
16
+ VCR.use_cassette(cassette) do
17
+ yield if block_given?
18
+ end
19
+ end
20
+
21
+ def yaml(name)
22
+ yaml = YAML.parse(File.read("spec/fixtures/yaml_fixtures/#{name}.yaml")).to_ruby
23
+ yield yaml if block_given?
24
+ yaml
25
+ end
26
+
27
+ class ContentTypeDouble
28
+ attr_reader :id
29
+
30
+ def initialize(id = '')
31
+ @id = id
32
+ end
33
+ end
34
+
35
+ class EntryDouble < Contentful::DynamicEntry
36
+ attr_reader :id, :content_type, :fields
37
+
38
+ def initialize(id = '', content_type = ContentTypeDouble.new, fields = {})
39
+ @id = id
40
+ @content_type = content_type
41
+ @fields = fields
42
+ end
43
+ end
44
+
45
+ class MapperDouble
46
+ def initialize(entry, config); end
47
+ def map; end
48
+ end
metadata ADDED
@@ -0,0 +1,238 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-contentful-data-import
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Contentful GmbH
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.5.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 2.5.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4'
33
+ - !ruby/object:Gem::Dependency
34
+ name: contentful
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.8'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.8'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rubygems-tasks
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.2'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.2'
61
+ - !ruby/object:Gem::Dependency
62
+ name: guard
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: guard-rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: bundler
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.6'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '1.6'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rspec
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '3.0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '3.0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: vcr
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ - !ruby/object:Gem::Dependency
146
+ name: webmock
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ - !ruby/object:Gem::Dependency
160
+ name: pry
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ description: Load blog posts and other managed content into Jekyll
174
+ email:
175
+ - david.litvak@contentful.com
176
+ executables: []
177
+ extensions: []
178
+ extra_rdoc_files: []
179
+ files:
180
+ - ".gitignore"
181
+ - ".travis.yml"
182
+ - ".yardopts"
183
+ - CHANGELOG.md
184
+ - CONTRIBUTING.md
185
+ - Gemfile
186
+ - Guardfile
187
+ - LICENSE.md
188
+ - README.md
189
+ - Rakefile
190
+ - jekyll-contentful.gemspec
191
+ - lib/jekyll-contentful-data-import.rb
192
+ - lib/jekyll-contentful-data-import/data_exporter.rb
193
+ - lib/jekyll-contentful-data-import/importer.rb
194
+ - lib/jekyll-contentful-data-import/mappers.rb
195
+ - lib/jekyll-contentful-data-import/mappers/base.rb
196
+ - lib/jekyll-contentful-data-import/serializer.rb
197
+ - lib/jekyll-contentful-data-import/version.rb
198
+ - lib/jekyll/commands/contentful.rb
199
+ - spec/fixtures/vcr_fixtures/entries.yml
200
+ - spec/jekyll-contentful/data_exporter_spec.rb
201
+ - spec/jekyll-contentful/importer_spec.rb
202
+ - spec/jekyll-contentful/mappers/base_spec.rb
203
+ - spec/jekyll-contentful/serializer_spec.rb
204
+ - spec/jekyll/commands/contentful_spec.rb
205
+ - spec/spec_helper.rb
206
+ homepage: https://www.contentful.com
207
+ licenses:
208
+ - MIT
209
+ metadata: {}
210
+ post_install_message:
211
+ rdoc_options: []
212
+ require_paths:
213
+ - lib
214
+ required_ruby_version: !ruby/object:Gem::Requirement
215
+ requirements:
216
+ - - ">="
217
+ - !ruby/object:Gem::Version
218
+ version: '0'
219
+ required_rubygems_version: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - ">="
222
+ - !ruby/object:Gem::Version
223
+ version: '0'
224
+ requirements: []
225
+ rubyforge_project:
226
+ rubygems_version: 2.5.0
227
+ signing_key:
228
+ specification_version: 4
229
+ summary: Include mangablable content from the Contentful CMS and API into your Jekyll
230
+ projects
231
+ test_files:
232
+ - spec/fixtures/vcr_fixtures/entries.yml
233
+ - spec/jekyll-contentful/data_exporter_spec.rb
234
+ - spec/jekyll-contentful/importer_spec.rb
235
+ - spec/jekyll-contentful/mappers/base_spec.rb
236
+ - spec/jekyll-contentful/serializer_spec.rb
237
+ - spec/jekyll/commands/contentful_spec.rb
238
+ - spec/spec_helper.rb