contentful_middleman 1.0.4 → 1.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9139679962d383306d6e3bc2ac41003499bad3c
4
- data.tar.gz: 7775dbb392e95e021cf9fa8adfb9a97a5e722ad2
3
+ metadata.gz: 702b32a151db22f1766007ab9037dd0f97514772
4
+ data.tar.gz: a1b77abea5946045d0ec3c0d78206a947cb91a37
5
5
  SHA512:
6
- metadata.gz: f89ab95709e0510226915f0e3991d4561c193b80cb419a2f830206725c1a71c038852eb81d3e56bded678913e99f820b306700e4ddcd842c7ac7e57ff8e5b8fd
7
- data.tar.gz: 118262ec63062c0962fce52c1ff643a40376c95668bf7fa219cc5ec8b8aee37313549e66d4cb3e9f4530f667facc2bcce7e041e11ac6d70bc18c12f54523277e
6
+ metadata.gz: 9347872cd447c2be6ba366e98578320239d5335957bbf41b263055fed811a4b7320493bdf4dc18a4c9928d0bdf147a593a0834bd4b3836acd73962e194dc2122
7
+ data.tar.gz: b481b2f37faee22a12a9bfbc172dff6d7fa76765d3f7b022564ec42bdca689c902a7bb67832ae329b2357fd821a99a149f7f4da71a6c7610abc4a943392e9e0f
@@ -1,5 +1,16 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.1.0
4
+ ### Added
5
+ * New extension option: `use_preview_api`
6
+ * An example mapper that allows references from linked to linking entries
7
+
8
+ ### Changed
9
+ * Make the all the entries available inside the mapper
10
+
11
+ ### Fixed
12
+ * Calculate the version hash using the `updated_at` value of every entry instead of its revision
13
+
3
14
  ## 1.0.4
4
15
  ### Fixed
5
16
  * Typo in the require statements
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Contentful Middleman
2
2
 
3
- [![Build Status](https://travis-ci.org/contentful/contentful_middleman.png)](https://travis-ci.org/contentful/contentful_middleman)
3
+ [![Build Status](https://travis-ci.org/contentful-labs/contentful_middleman.svg?branch=master)](https://travis-ci.org/contentful-labs/contentful_middleman)
4
4
 
5
5
  Contentful Middleman is a [Middleman](http://middlemanapp.com/) extension to use the Middleman static site generator together with the API-driven [Contentful CMS](https://www.contentful.com). It is powered by the [Contentful Ruby Gem](https://github.com/contentful/contentful.rb).
6
6
 
@@ -44,12 +44,13 @@ activate :contentful do |f|
44
44
  end
45
45
  ```
46
46
 
47
- Parameter | Description
48
- ---------- |------------
49
- space | Hash with an user choosen name for the space as key and the space id as value
50
- access_token | Contentful Delivery API access token
51
- cda_query | Hash describing query configuration. See [contentful.rb](https://github.com/contentful/contentful.rb) for more info (look for filter options there)
52
- content_types | Hash describing the mapping applied to entries of the imported content types
47
+ Parameter | Description
48
+ ---------- | ------------
49
+ space | Hash with an user choosen name for the space as key and the space id as value
50
+ access_token | Contentful Delivery API access token
51
+ cda_query | Hash describing query configuration. See [contentful.rb](https://github.com/contentful/contentful.rb) for more info (look for filter options there)
52
+ content_types | Hash describing the mapping applied to entries of the imported content types
53
+ use_preview_api | Boolean to toogle the used API. Set it to `false` to use `cdn.contentful.com` (default value). Set it to `true` to use `preview.contentful.com`. More info in [the documentation](https://www.contentful.com/developers/documentation/content-delivery-api/#preview-api)
53
54
 
54
55
  You can activate the extension multiple times to import entries from different spaces.
55
56
  ## Entry mapping
@@ -81,7 +82,7 @@ end
81
82
  If you don't want to map all the fields by hand inherit from the Base mappper:
82
83
 
83
84
  ```ruby
84
- class MyAwesomeMapper < ContentfulMiddleman::Mappers::Base
85
+ class MyAwesomeMapper < ContentfulMiddleman::Mapper::Base
85
86
  def map(context, entry)
86
87
  super
87
88
  # After calling super the context object
@@ -91,6 +92,8 @@ class MyAwesomeMapper < ContentfulMiddleman::Mappers::Base
91
92
  end
92
93
  ```
93
94
 
95
+ There's also an example back-reference mapper in the examples directory for adding back-references onto entries that are linked to by other entries.
96
+
94
97
  ## Configuration: examples
95
98
 
96
99
  ```ruby
@@ -0,0 +1,59 @@
1
+ # A function that returns a Mapper class for adding back-references to
2
+ # contentful data files. If a Product content type had a "company" entry field
3
+ # that referenced a Company content type, using the following BackrefMapper on
4
+ # the Company content type would add a "products" field to the company data
5
+ # files with a list of all Products that reference that Company:
6
+ #
7
+ # BackrefMapper(EMPLOYEE_CONTENT_TYPE_ID, 'department', 'employees')
8
+ #
9
+ # For example, given these two files:
10
+ #
11
+ # ---
12
+ # :id: 12345
13
+ # :name: Jet-Propelled Tennis Shoes
14
+ # :company:
15
+ # - :id: 54321
16
+ # :name: Acme
17
+ #
18
+ # ---
19
+ # :id: 54321
20
+ # :name: Acme
21
+ # :website: http://acme.example.com/
22
+ #
23
+ # The Acme company YAML file would become:
24
+ #
25
+ # ---
26
+ # :id: 54321
27
+ # :name: Acme
28
+ # :website: http://acme.example.com/
29
+ # :products:
30
+ # - :id: 12345
31
+ # :name: Jet-Propelled Tennis Shoes
32
+ # :company:
33
+ # - :id: 54321
34
+ # :name: Acme
35
+ #
36
+ # Params:
37
+ # +content_type_id+:: The ID of the content_type that references the
38
+ # content_type that this mapper is being applied to
39
+ # +content_type_field+:: The field name on the content_type to look for
40
+ # references
41
+ # +backref_field+:: The new field to create to hold the list of references
42
+ def self.BackrefMapper(content_type_id, content_type_field, backref_field)
43
+ klass = Class.new ContentfulMiddleman::Mapper::Base do
44
+
45
+ @@content_type_id = content_type_id
46
+ @@content_type_field = content_type_field
47
+ @@backref_field = backref_field
48
+
49
+ def map(context, entry)
50
+ super
51
+ content_type_entries = @entries.select { |e| e.sys[:contentType].id == @@content_type_id }
52
+ referencing_entries = content_type_entries.select{ |e| e.send(@@content_type_field).map{|x| x.id}.include? entry.id }
53
+ referencing_ids = referencing_entries.map{ |e| e.id }
54
+ context.set(@@backref_field, map_value(referencing_ids))
55
+ end
56
+ end
57
+
58
+ return klass
59
+ end
@@ -71,7 +71,9 @@ module Middleman
71
71
  end
72
72
 
73
73
  def shared_instance
74
- @shared_instance ||= ::Middleman::Application.server.inst
74
+ @shared_instance ||= ::Middleman::Application.server.inst do
75
+ set :environment, :contentful
76
+ end
75
77
  end
76
78
  end
77
79
 
@@ -25,6 +25,9 @@ module ContentfulMiddleman
25
25
  option :content_types, {},
26
26
  'The mapping of Content Types names to ids'
27
27
 
28
+ option :use_preview_api, false,
29
+ 'Use the Preview API when fetching content'
30
+
28
31
 
29
32
  helpers ContentfulMiddleman::Helpers
30
33
 
@@ -25,11 +25,12 @@ module ContentfulMiddleman
25
25
  private
26
26
  def local_data_files
27
27
  entries.map do |entry|
28
- content_type_mapper = @content_type_mappers.fetch(entry.content_type.id)
29
- content_type_name = @content_type_names.fetch(entry.content_type.id).to_s
30
- context = ContentfulMiddleman::Context.new
28
+ content_type_mapper_class = @content_type_mappers.fetch(entry.content_type.id)
29
+ content_type_name = @content_type_names.fetch(entry.content_type.id).to_s
30
+ context = ContentfulMiddleman::Context.new
31
31
 
32
- content_type_mapper.new.map(context, entry)
32
+ content_type_mapper = content_type_mapper_class.new(entries)
33
+ content_type_mapper.map(context, entry)
33
34
 
34
35
  LocalData::File.new(context.to_yaml, File.join(@space_name, content_type_name, entry.id))
35
36
  end
@@ -1,5 +1,7 @@
1
1
  module ContentfulMiddleman
2
2
  class Instance
3
+ API_PREVIEW_URL = 'preview.contentful.com'
4
+
3
5
  def initialize(extension)
4
6
  @extension = extension
5
7
  end
@@ -30,12 +32,19 @@ module ContentfulMiddleman
30
32
 
31
33
  private
32
34
  def client
33
- @client ||= Contentful::Client.new(
34
- access_token: options.access_token,
35
- space: options.space.fetch(:id),
36
- dynamic_entries: :auto,
37
- raise_errors: true
38
- )
35
+ @client ||= Contentful::Client.new(client_options)
36
+ end
37
+
38
+ def client_options
39
+ client_options = {
40
+ access_token: options.access_token,
41
+ space: options.space.fetch(:id),
42
+ dynamic_entries: :auto,
43
+ raise_errors: true
44
+ }
45
+
46
+ client_options[:api_url] = API_PREVIEW_URL if options.use_preview_api
47
+ client_options
39
48
  end
40
49
 
41
50
  def options
@@ -3,6 +3,12 @@ require_relative '../commands/context'
3
3
  module ContentfulMiddleman
4
4
  module Mapper
5
5
  class Base
6
+ attr_reader :entries
7
+
8
+ def initialize(entries)
9
+ @entries = entries
10
+ end
11
+
6
12
  def map(context, entry)
7
13
  context.id = entry.id
8
14
  entry.fields.each {|k, v| map_field context, k, v}
@@ -1,3 +1,3 @@
1
1
  module ContentfulMiddleman
2
- VERSION = "1.0.4"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -12,7 +12,7 @@ module ContentfulMiddleman
12
12
 
13
13
  def write_for_space_with_entries(space_name, entries)
14
14
  sorted_entries = entries.sort {|a, b| a.id <=> b.id}
15
- ids_and_revisions_string = sorted_entries.map {|e| "#{e.id}#{e.revision}"}.join
15
+ ids_and_revisions_string = sorted_entries.map {|e| "#{e.id}#{e.updated_at}"}.join
16
16
  entries_hash = Digest::SHA1.hexdigest( ids_and_revisions_string )
17
17
 
18
18
  File.open(hashfilename(space_name), 'w') { |file| file.write(entries_hash) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contentful_middleman
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sascha Konietzke
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-10 00:00:00.000000000 Z
12
+ date: 2015-04-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: middleman-core
@@ -86,6 +86,7 @@ files:
86
86
  - Rakefile
87
87
  - TODO
88
88
  - contentful_middleman.gemspec
89
+ - examples/mappers/backref.rb
89
90
  - lib/contentful_middleman.rb
90
91
  - lib/contentful_middleman/commands/contentful.rb
91
92
  - lib/contentful_middleman/commands/context.rb