Skip to content

Commit

Permalink
Merge 6aa2827 into e0bb039
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Kliment committed Jun 18, 2014
2 parents e0bb039 + 6aa2827 commit 6c3538e
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 119 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dependencies": {
"gavel": "~0.3.0",
"winston": "~0.7.2",
"protagonist": "~0.8.0",
"protagonist": "~0.11.0",
"uri-template": "~0.4.1",
"async": "~0.2.9",
"optimist": "~0.6.0",
Expand Down
15 changes: 9 additions & 6 deletions src/blueprint-ast-to-runtime.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
inheritHeaders = require './inherit-headers'
inheritParameters = require './inherit-parameters'
expandUriTemplateWithParameters = require './expand-uri-template-with-parameters'
exampleToHttpPayloadPair = require './example-to-http-payload-pair'
convertAstMetadata = require './convert-ast-metadata'

blueprintAstToRuntime = (blueprintAst) ->
runtime =
transactions: []
Expand All @@ -19,10 +20,12 @@ blueprintAstToRuntime = (blueprintAst) ->
for action in resource['actions']
origin['actionName'] = action['name']

action['headers'] = inheritHeaders action['headers'], resource['headers']
action['parameters'] = inheritParameters action['parameters'], resource['parameters']

uriResult = expandUriTemplateWithParameters resource['uriTemplate'], action['parameters']
actionParameters = convertAstMetadata action['parameters']
resourceParameters = convertAstMetadata resource['parameters']

parameters = inheritParameters actionParameters, resourceParameters

uriResult = expandUriTemplateWithParameters resource['uriTemplate'], parameters

for message in uriResult['warnings']
runtime['warnings'].push {
Expand All @@ -41,7 +44,7 @@ blueprintAstToRuntime = (blueprintAst) ->
for example in action['examples']
origin['exampleName'] = example['name']

result = exampleToHttpPayloadPair example, action['headers']
result = exampleToHttpPayloadPair example

for message in result['warnings']
runtime['warnings'].push {
Expand Down
12 changes: 12 additions & 0 deletions src/convert-ast-metadata.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
convertAstMetadata = (metadata) ->
result = {}
if Array.isArray metadata
for item in metadata
name = item['name']
delete item['name']

result[name] = item

result

module.exports = convertAstMetadata
8 changes: 4 additions & 4 deletions src/example-to-http-payload-pair.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
inheritHeaders = require './inherit-headers'
convertAstMetadata = require './convert-ast-metadata'

# Transforms API Blueprint example to an array of Expected
# HTTP Request and Response body and headers
exampleToHttpPayloadPair = (example, inheritingHeaders = {}) ->
exampleToHttpPayloadPair = (example) ->

result =
warnings: []
Expand Down Expand Up @@ -33,10 +33,10 @@ exampleToHttpPayloadPair = (example, inheritingHeaders = {}) ->
headers: {}

request['body'] = selectedRequest['body']
request['headers'] = inheritHeaders selectedRequest['headers'], inheritingHeaders
request['headers'] = convertAstMetadata selectedRequest['headers']

response['body'] = selectedResponse['body']
response['headers'] = inheritHeaders selectedResponse['headers'], inheritingHeaders
response['headers'] = convertAstMetadata selectedResponse['headers']
response['status'] = selectedResponse['name']
if selectedResponse['schema'] != ""
response['schema'] = selectedResponse['schema']
Expand Down
8 changes: 0 additions & 8 deletions src/inherit-headers.coffee

This file was deleted.

126 changes: 87 additions & 39 deletions test/fixtures/blueprint-ast.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"_version": "1.0",
"metadata": {
"FORMAT": {
"_version": "2.0",
"metadata": [
{
"name": "FORMAT",
"value": "X-1A"
}
},
],
"name": "Machines API",
"description": "",
"resourceGroups": [
Expand All @@ -17,15 +18,13 @@
"description": "",
"uriTemplate": "/machines",
"model": {},
"parameters": {},
"headers": {},
"parameters": [],
"actions": [
{
"name": "Create a Machine",
"description": "",
"method": "POST",
"parameters": {},
"headers": {},
"parameters": [],
"examples": [
{
"name": "",
Expand All @@ -34,25 +33,54 @@
{
"name": "",
"description": "",
"headers": {
"Content-Type": {
"headers": [
{
"name": "Content-Type",
"value": "application/json"
}
},
"body": "{\n \"type\": \"bulldozer\",\n \"name\": \"willy\",\n}\n",
],
"body": "{\n \"type\": \"bulldozer\",\n \"name\": \"willy\"\n}\n",
"schema": ""
}
],
"responses": [
{
"name": "202",
"description": "",
"headers": {
"Content-Type": {
"headers": [
{
"name": "Content-Type",
"value": "application/json"
}
],
"body": "{\n \"mesage\": \"Accepted\"\n}\n",
"schema": ""
}
]
}
]
},
{
"name": "Retreive all Machines",
"description": "",
"method": "GET",
"parameters": [],
"examples": [
{
"name": "",
"description": "",
"requests": [],
"responses": [
{
"name": "200",
"description": "",
"headers": [
{
"name": "Content-Type",
"value": "application/json"
}
},
"body": "{\n \"type\": \"bulldozer\",\n \"name\": \"willy\",\n \"id\": \"5229c6e8e4b0bd7dbb07e29c\"\n}\n",
],
"body": "[{\n \"_id\": \"52341870ed55224b15ff07ef\",\n \"type\": \"bulldozer\",\n \"name\": \"willy\"\n}]\n",
"schema": ""
}
]
Expand All @@ -66,24 +94,23 @@
"description": "",
"uriTemplate": "/machines/{name}",
"model": {},
"parameters": {
"name": {
"parameters": [
{
"name": "name",
"description": "",
"type": "",
"required": false,
"required": true,
"default": "",
"example": "willy",
"values": []
}
},
"headers": {},
],
"actions": [
{
"name": "Update a Machine",
"description": "",
"method": "PUT",
"parameters": {},
"headers": {},
"parameters": [],
"examples": [
{
"name": "",
Expand All @@ -92,11 +119,12 @@
{
"name": "",
"description": "",
"headers": {
"Content-Type": {
"headers": [
{
"name": "Content-Type",
"value": "application/json"
}
},
],
"body": "{\n \"name\": \"waldo\"\n}\n",
"schema": ""
}
Expand All @@ -105,12 +133,13 @@
{
"name": "200",
"description": "",
"headers": {
"Content-Type": {
"headers": [
{
"name": "Content-Type",
"value": "application/json"
}
},
"body": "{\n \"type\": \"bulldozer\",\n \"name\": \"waldo\",\n \"id\": \"5229c6e8e4b0bd7dbb07e29c\"\n}\n",
],
"body": "{\n \"type\": \"bulldozer\",\n \"name\": \"waldo\",\n \"_id\": \"5229c6e8e4b0bd7dbb07e29c\"\n}\n",
"schema": ""
}
]
Expand All @@ -121,8 +150,17 @@
"name": "Retrieve a Machine",
"description": "",
"method": "GET",
"parameters": {},
"headers": {},
"parameters": [
{
"name": "name",
"description": "",
"type": "",
"required": true,
"default": "",
"example": "waldo",
"values": []
}
],
"examples": [
{
"name": "",
Expand All @@ -132,12 +170,13 @@
{
"name": "200",
"description": "",
"headers": {
"Content-Type": {
"headers": [
{
"name": "Content-Type",
"value": "text/plain"
}
},
"body": "{\n \"type\": \"bulldozer\",\n \"name\": \"waldo\",\n \"id\": \"5229c6e8e4b0bd7dbb07e29c\"\n}\n",
],
"body": "{\n \"type\": \"bulldozer\",\n \"name\": \"waldo\",\n \"_id\": \"5229c6e8e4b0bd7dbb07e29c\"\n}\n",
"schema": ""
}
]
Expand All @@ -148,8 +187,17 @@
"name": "Delete Message",
"description": "",
"method": "DELETE",
"parameters": {},
"headers": {},
"parameters": [
{
"name": "name",
"description": "",
"type": "",
"required": true,
"default": "",
"example": "waldo",
"values": []
}
],
"examples": [
{
"name": "",
Expand All @@ -159,7 +207,7 @@
{
"name": "204",
"description": "",
"headers": {},
"headers": [],
"body": "",
"schema": ""
}
Expand Down
46 changes: 46 additions & 0 deletions test/unit/convert-ast-metadata-test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{assert} = require 'chai'

convertAstMetadata = require '../../src/convert-ast-metadata'

describe 'convertAstMetadata()', () ->
metadata = [
{
name: 'name'
description: ''
type: ''
required: true
default: ''
example: 'willy'
values: ''
}
,
{
name: 'limit'
description: ''
type: ''
required: true
default: ''
example: 1
values: ''
}
]

data = null

describe 'its return', () ->
before () ->
data = convertAstMetadata metadata

it 'should return an object', () ->
assert.isObject data

it 'should contain keys with names limit and name', () ->
assert.include Object.keys(data), 'limit'
assert.include Object.keys(data), 'name'

it 'values should not contain "name" key', () ->
for key, values of data
assert.notInclude Object.keys(values), 'name'

it 'should return empty hash if input is undefined', () ->
assert.deepEqual {}, convertAstMetadata undefined

0 comments on commit 6c3538e

Please sign in to comment.