-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from carrot/implement-view-helper
Implement view helper
- Loading branch information
Showing
22 changed files
with
304 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.DS_Store | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- "0.10" | ||
after_script: | ||
- npm run coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,92 @@ | ||
RootsUtil = require 'roots-util' | ||
path = require 'path' | ||
_ = require 'lodash' | ||
W = require 'when' | ||
S = require 'string' | ||
contentful = require 'contentful' | ||
pluralize = require 'pluralize' | ||
|
||
# This is meant to serve as an example... | ||
errors = | ||
no_token: 'Missing required options for roots-contentful. Please ensure | ||
`access_token` and `space_id` are present.' | ||
no_type_id: 'One or more of your content types is missing an `id` value' | ||
sys_conflict:'One of your content types has `sys` as a field. This is reserved | ||
for storing Contentful system metadata, please rename this field to a | ||
different value.' | ||
|
||
# Full Roots Extension API documentation located: | ||
# http://roots.readthedocs.org/en/latest/extensions.html | ||
module.exports = (opts) -> | ||
# throw error if missing required config | ||
if not (opts.access_token && opts.space_id) | ||
throw new Error errors.no_token | ||
|
||
module.exports = -> | ||
class RootsContentful | ||
# setup contentful api client | ||
client = contentful.createClient | ||
accessToken: opts.access_token | ||
space: opts.space_id | ||
|
||
class RootsContentful | ||
constructor: (@roots) -> | ||
# console.log @roots | ||
@roots.config.locals ||= {} | ||
|
||
setup: -> | ||
configure_content(opts.content_types) | ||
.then(get_all_content) | ||
.then (res) => | ||
@roots.config.locals.contentful = res | ||
|
||
###* | ||
* Configures content types set in app.coffee. Sets default values if | ||
* optional config options are missing. | ||
* @param {Array} types - content_types set in app.coffee extension config | ||
* @return {Promise} - returns an array of configured content types | ||
### | ||
|
||
configure_content = (types) -> | ||
W.map types, (t) -> | ||
if not t.id then return W.reject(errors.no_type_id) | ||
if t.name then return W.resolve(t) | ||
t.filters ?= {} | ||
W client.contentType(t.id).then (res) -> | ||
t.name = pluralize(S(res.name).toLowerCase().underscore().s) | ||
return t | ||
|
||
fs: -> | ||
# category: 'foo' | ||
# extract: true | ||
# detect: (f) => | ||
# path.extname(f.relative) == 'js' | ||
###* | ||
* Fetches data from Contentful API, formats the raw data, and constructs | ||
* the locals object | ||
* @param {Array} types - configured content_type objects | ||
* @return {Promise} - returns formatted locals object with all content | ||
### | ||
|
||
compile_hooks: -> | ||
# category: 'foo' | ||
get_all_content = (types) -> | ||
W.reduce types, (m, t) -> | ||
fetch_content(t) | ||
.then(format_content) | ||
.then((c) -> m[t.name] = c) | ||
.yield(m) | ||
, {} | ||
|
||
# before_file: (ctx) => | ||
# ctx.content = ctx.content.toUpperCase() | ||
###* | ||
* Fetch entries for a single content type object | ||
* @param {Object} type - content type object | ||
* @return {Promise} - returns response from Contentful API | ||
### | ||
|
||
# after_file: (ctx) => | ||
# ctx.content = ctx.content.toUpperCase() | ||
fetch_content = (type) -> | ||
W client.entries(_.merge(type.filters, content_type: type.id)) | ||
|
||
# before_pass: (ctx) => | ||
# ctx.content = ctx.content.toUpperCase() | ||
###* | ||
* Formats raw response from Contentful | ||
* @param {Object} content - entries API response for a content type | ||
* @return {Promise} - returns formatted content type entries object | ||
### | ||
|
||
# after_pass: (ctx) => | ||
# ctx.content = ctx.content.toUpperCase() | ||
format_content = (content) -> | ||
W.map(content, format_entry) | ||
|
||
# write: -> | ||
# false | ||
###* | ||
* Formats a single entry object from Contentful API response | ||
* @param {Object} e - single entry object from API response | ||
* @return {Promise} - returns formatted entry object | ||
### | ||
|
||
category_hooks: -> | ||
# after: (ctx) => | ||
# output = path.join(ctx.roots.config.output_path(), 'build.js') | ||
# nodefn.call(fs.writeFile, output, @contents) | ||
format_entry = (e) -> | ||
if _.has(e.fields, 'sys') then return W.reject(errors.sys_conflict) | ||
_.assign(_.omit(e, 'fields'), e.fields) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1 wow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
roots_contentful = require '../../..' | ||
contentful = require '../../..' | ||
|
||
module.exports = | ||
ignores: ["**/_*", "**/.DS_Store"] | ||
extensions: [new roots_contentful] | ||
extensions: [ | ||
contentful( | ||
access_token: 'YOUR_ACCESS_TOKEN' | ||
space_id: 'aqzq2qya2jm4' | ||
content_types: [ | ||
{ | ||
id: '6BYT1gNiIEyIw8Og8aQAO6' | ||
} | ||
{ | ||
id: '7CDlVsacqQc88cmIEGYWMa' | ||
} | ||
] | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
p hello world | ||
ul | ||
- for p in contentful.blog_posts | ||
li | ||
h1= p.title | ||
p= p.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1 wow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
contentful = require '../../..' | ||
|
||
module.exports = | ||
ignores: ["**/_*", "**/.DS_Store"] | ||
extensions: [ | ||
contentful( | ||
access_token: 'YOUR_ACCESS_TOKEN' | ||
space_id: 'aqzq2qya2jm4' | ||
content_types: [ | ||
{ | ||
id: '6BYT1gNiIEyIw8Og8aQAO6' | ||
name: 'press_links' | ||
}, | ||
{ | ||
id: '7CDlVsacqQc88cmIEGYWMa' | ||
} | ||
] | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ul | ||
- for p in contentful.press_links | ||
li | ||
h1= p.title | ||
p= p.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "test", | ||
"dependencies": { | ||
"jade": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
contentful = require '../../..' | ||
|
||
module.exports = | ||
ignores: ["**/_*", "**/.DS_Store"] | ||
extensions: [ | ||
contentful( | ||
access_token: 'YOUR_ACCESS_TOKEN' | ||
space_id: 'aqzq2qya2jm4' | ||
content_types: [ | ||
{ | ||
name: 'test' | ||
} | ||
{ | ||
id: '7CDlVsacqQc88cmIEGYWMa' | ||
} | ||
] | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ul | ||
- for p in contentful.blog_posts | ||
li | ||
h1= p.title | ||
p= p.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "test", | ||
"dependencies": { | ||
"jade": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
contentful = require '../../..' | ||
|
||
module.exports = | ||
ignores: ["**/_*", "**/.DS_Store"] | ||
extensions: [ | ||
contentful( | ||
content_types: [ | ||
{ | ||
name: 'test' | ||
} | ||
{ | ||
id: '7CDlVsacqQc88cmIEGYWMa' | ||
} | ||
] | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1 wow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "test", | ||
"dependencies": { | ||
"jade": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--reporter spec | ||
--compilers coffee:coffee-script/register | ||
--require test/support/helpers | ||
--timeout 30000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var chai = require('chai'), | ||
chai_promise = require('chai-as-promised'), | ||
mockery = require('mockery'), | ||
path = require('path'), | ||
_path = path.join(__dirname, '../fixtures'), | ||
RootsUtil = require('roots-util'), | ||
h = new RootsUtil.Helpers({ base: _path }), | ||
roots_contentful = require('../../lib'); | ||
|
||
var should = chai.should(); | ||
chai.use(chai_promise); | ||
|
||
global.should = should; | ||
global.mockery = mockery; | ||
global._path = _path; | ||
global.h = h; | ||
global.roots_contentful = roots_contentful; |
Oops, something went wrong.