Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Finished integrating boutique
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Feb 16, 2015
1 parent c9387b6 commit 154e1ca
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"coffee-script": "~1.7.1",
"yargs": "~1.3.3",
"protagonist-experimental": "0.18.6",
"async": "~0.9.0",
"boutique": "git+ssh://[email protected]:apiaryio/boutique.git"
},
"devDependencies": {
Expand Down
104 changes: 101 additions & 3 deletions src/drafter.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,82 @@
protagonist = require 'protagonist-experimental'
boutique = require 'boutique'
options = require './options'
fs = require 'fs'
async = require 'async'

# Gather all payloads from the given parse result
#
# @param result [Object] Parse Result
gatherPayloads = (result) ->
payloads = []

for element in result.ast.content
if element.element is 'category'

for subElement in element.content
if subElement.element is 'resource'

for action in subElement.actions
attributes = null

for actionElement in action.content
attributes = actionElement if actionElement.element is 'dataStructure'

for example in action.examples
payloads.push {payload: request, actionAttributes: attributes} for request in example.requests
payloads.push {payload: response, actionAttributes: attributes} for response in example.responses

return payloads

# Generate payload body if MSON is provided and no body
#
# @param payload [Object] Payload object
# @param attributes [Object] Payload attributes object
# @param contentType [Object] Payload content type
generateBody = (payload, attributes, contentType, callback) ->
if not attributes? or not contentType? or payload.body
return callback null

boutique.represent
ast: attributes,
contentType: contentType
, (error, body) ->
if not error? and body
resolved =
element: 'resolvedAsset'
attributes:
role: 'bodyExample'
content: body

payload.content.push resolved

# For waterfall
callback null, payload, attributes, contentType

# Generate payload schema if MSON is provided and no schema and ContentType is json
#
# @param payload [Object] Payload object
# @param attributes [Object] Payload attributes object
# @param contentType [Object] Payload content type
generateSchema = (payload, attributes, contentType, callback) ->
if not attributes? or payload.schema or contentType.indexOf('json') is -1
return callback null

boutique.represent
ast: attributes,
contentType: 'application/schema+json'
, (error, body) ->
if not error? and body
resolved =
element: 'resolvedAsset'
attributes:
role: 'bodySchema'
content: body

payload.content.push resolved

# For waterfall
callback null, payload, attributes, contentType

#
# Drafter
Expand Down Expand Up @@ -48,9 +124,32 @@ class Drafter
delete result.ast.resourceGroups

@expandNode result.ast, rules, 'blueprint'
@reconstructResourceGroups result.ast
payloads = gatherPayloads result

async.each payloads, @resolvePayload, (error) =>
@reconstructResourceGroups result.ast
callback error, result

# Resolve assets of a payload
resolvePayload: ({payload, actionAttributes}, callback) ->
attributes = null
contentType = ''

callback error, result
for header in payload.headers
contentType = header.value if header.name is 'Content-Type'

for element in payload.content
attributes = element if element.element is 'dataStructure'

attributes ?= actionAttributes

async.waterfall [
(callback) ->
callback null, payload, attributes, contentType
, generateBody
, generateSchema
], (err) ->
callback null

# Expand a certain node with the given rules
#
Expand All @@ -71,7 +170,6 @@ class Drafter
when 'dataStructure'
@dataStructures[subElement.name.literal] = subElement
when 'resource'

for resourceSubElement in subElement.content
@dataStructures[resourceSubElement.name.literal] = resourceSubElement if resourceSubElement.element is 'dataStructure'

Expand Down
90 changes: 88 additions & 2 deletions test/fixtures/dataStructures.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@
"class": "memberType"
}
]
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodyExample"
},
"content": "{\"id\":\"250FF\",\"percent_off\":25,\"redeem_by\":null,\"created\":null,\"modified\":null,\"created_at\":null,\"updated_at\":null}"
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodySchema"
},
"content": "{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"},\"percent_off\":{\"type\":\"number\"},\"redeem_by\":{\"type\":\"number\",\"description\":\"Date after which the coupon can no longer be redeemed\"},\"created\":{\"type\":\"number\"},\"modified\":{\"type\":\"number\"},\"created_at\":{\"type\":\"number\"},\"updated_at\":{\"type\":\"number\"}},\"$schema\":\"http://json-schema.org/draft-04/schema#\"}"
}
]
}
Expand Down Expand Up @@ -657,7 +671,22 @@
],
"body": "",
"schema": "",
"content": []
"content": [
{
"element": "resolvedAsset",
"attributes": {
"role": "bodyExample"
},
"content": "{\"percent_off\":25,\"redeem_by\":null,\"created\":null,\"modified\":null,\"created_at\":null,\"updated_at\":null}"
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodySchema"
},
"content": "{\"type\":\"object\",\"properties\":{\"percent_off\":{\"type\":\"number\"},\"redeem_by\":{\"type\":\"number\",\"description\":\"Date after which the coupon can no longer be redeemed\"},\"created\":{\"type\":\"number\"},\"modified\":{\"type\":\"number\"},\"created_at\":{\"type\":\"number\"},\"updated_at\":{\"type\":\"number\"}},\"$schema\":\"http://json-schema.org/draft-04/schema#\"}"
}
]
}
],
"responses": [
Expand Down Expand Up @@ -845,6 +874,20 @@
"class": "memberType"
}
]
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodyExample"
},
"content": "{\"id\":\"250FF\",\"percent_off\":25,\"redeem_by\":null,\"created\":null,\"modified\":null,\"created_at\":null,\"updated_at\":null}"
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodySchema"
},
"content": "{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"},\"percent_off\":{\"type\":\"number\"},\"redeem_by\":{\"type\":\"number\",\"description\":\"Date after which the coupon can no longer be redeemed\"},\"created\":{\"type\":\"number\"},\"modified\":{\"type\":\"number\"},\"created_at\":{\"type\":\"number\"},\"updated_at\":{\"type\":\"number\"}},\"$schema\":\"http://json-schema.org/draft-04/schema#\"}"
}
]
}
Expand Down Expand Up @@ -2177,6 +2220,20 @@
"class": "memberType"
}
]
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodyExample"
},
"content": "{\"id\":\"250FF\",\"percent_off\":25,\"redeem_by\":null,\"created\":null,\"modified\":null,\"created_at\":null,\"updated_at\":null}"
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodySchema"
},
"content": "{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"},\"percent_off\":{\"type\":\"number\"},\"redeem_by\":{\"type\":\"number\",\"description\":\"Date after which the coupon can no longer be redeemed\"},\"created\":{\"type\":\"number\"},\"modified\":{\"type\":\"number\"},\"created_at\":{\"type\":\"number\"},\"updated_at\":{\"type\":\"number\"}},\"$schema\":\"http://json-schema.org/draft-04/schema#\"}"
}
]
}
Expand Down Expand Up @@ -2600,7 +2657,22 @@
],
"body": "",
"schema": "",
"content": []
"content": [
{
"element": "resolvedAsset",
"attributes": {
"role": "bodyExample"
},
"content": "{\"percent_off\":25,\"redeem_by\":null,\"created\":null,\"modified\":null,\"created_at\":null,\"updated_at\":null}"
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodySchema"
},
"content": "{\"type\":\"object\",\"properties\":{\"percent_off\":{\"type\":\"number\"},\"redeem_by\":{\"type\":\"number\",\"description\":\"Date after which the coupon can no longer be redeemed\"},\"created\":{\"type\":\"number\"},\"modified\":{\"type\":\"number\"},\"created_at\":{\"type\":\"number\"},\"updated_at\":{\"type\":\"number\"}},\"$schema\":\"http://json-schema.org/draft-04/schema#\"}"
}
]
}
],
"responses": [
Expand Down Expand Up @@ -2788,6 +2860,20 @@
"class": "memberType"
}
]
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodyExample"
},
"content": "{\"id\":\"250FF\",\"percent_off\":25,\"redeem_by\":null,\"created\":null,\"modified\":null,\"created_at\":null,\"updated_at\":null}"
},
{
"element": "resolvedAsset",
"attributes": {
"role": "bodySchema"
},
"content": "{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"},\"percent_off\":{\"type\":\"number\"},\"redeem_by\":{\"type\":\"number\",\"description\":\"Date after which the coupon can no longer be redeemed\"},\"created\":{\"type\":\"number\"},\"modified\":{\"type\":\"number\"},\"created_at\":{\"type\":\"number\"},\"updated_at\":{\"type\":\"number\"}},\"$schema\":\"http://json-schema.org/draft-04/schema#\"}"
}
]
}
Expand Down

0 comments on commit 154e1ca

Please sign in to comment.