-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
245 lines (220 loc) · 6.74 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
sample ALPS server implementation
2013-07
show using ALPS document drive
mapping of internal data model to ALPS "magic strings"
and creating a representation in collection+json/
*/
var http = require('http');
var port = process.env.PORT||1337;
var base = "http://locahost:"+port;
// handle requests
function handler(req, res) {
base = "http://"+req.headers.host;
applyMap();
res.writeHead(200, {"content-type":"application/json"});
res.end(JSON.stringify(template));
res.end("{}");
}
// use internal semantic map
function applyMap() {
var i, x, item, v, p, desc, j, y;
// apply data elements
template.collection.items = [];
for(i=0, x=data.people.length; i<x; i++) {
item = {};
item.href = base + '/contacts/' + data.people[i].id;
item.data = [];
for(p in data.people[i]) {
v = matchMap(p);
if(v!==null) {
item.data.push({"name" : v, "value" : data.people[i][p]});
}
}
template.collection.items.push(item);
}
// apply transition elements
template.collection.queries = [];
for(i=0, x=alps.alps.descriptor.length; i<x; i++) {
desc = alps.alps.descriptor[i];
if(desc.type==='safe') {
item = {};
item.rt = desc.rt;
item.rel = desc.id;
item.href = base+'/search';
item.data = [];
for(j=0, y=desc.descriptor.length; j<y; j++) {
item.data.push({"name" : desc.descriptor[j].id, "value" : ""});
}
template.collection.queries.push(item);
}
if(desc.type==='unsafe') {
// todo
}
if(desc.type==='idempotent') {
// todo
}
}
}
function matchMap(p) {
var i, x, rtn;
rtn = null;
for(i=0, x=map.length; i<x; i++) {
if(map[i].local===p) {
rtn = map[i].shared;
break;
}
}
return rtn;
}
// start listening
http.createServer(handler).listen(port);
// ********************************************************
// DATA
// ********************************************************
// stored data model
// usually at some external location
var data = { "people" :
[
{
"id" : "q1w2e3r4",
"firstName" : "Mike",
"lastName" : "Amundsen",
"primaryEmail" : "[email protected]",
"voicePhone" : "123-456-7890"
},
{
"id" : "p0o9i8u7",
"firstName" : "Mark",
"lastName" : "Gunderson",
"primaryEmail" : "[email protected]",
"voicePhone" : "234-567-8901"
}
]
};
// response representation (Cj)
var template = { "collection" :
{
"links" : [
{
"href" : "https://rawgit.com/alps-io/alps-contacts/master/contact-alps.js",
"rel" : "profile"
},
{
"href" : "https://rawgit.com/alps-io/alps-contacts/master/contact-doc.html",
"rel" : "help"
}
],
"items" : [],
"queries" : [
{
"rt" : "contacts",
"rel" : "search",
"href" : base+"/search",
"data" : [
{
"name" : "name",
"value" : "",
"prompt" : "Search"
}
]
}
]
}
};
// map info
// might be in a config
var map = [
{"local" : "firstName", "shared" : "givenName"},
{"local" : "lastName", "shared" : "familyName"},
{"local" : "primaryEmail", "shared" : "email"},
{"local" : "voicePhone", "shared" : "telephone"}
];
// alps model
// usually read from external location
var alps = { "alps" :
{
"version" : "1.0",
"doc" : {
"format" : "text",
"value" : "List of contacts w/ search"
},
"descriptor" : [
{
"id" : "search",
"type" : "safe",
"rt" : "contacts",
"doc" : {
"format" : "text",
"value" : "search for contacts in the list"
},
"descriptor" : [
{
"id" : "name",
"type" : "semantic",
"doc" : {
"format" : "text",
"value" : "input for searching"
}
}
]
},
{
"id" : "contacts",
"type" : "semantic",
"doc" : {
"format" : "text",
"value" : "contact item"
},
"descriptor" : [
{
"id" : "item",
"type" : "safe",
"doc" : {
"format" : "text",
"value" : "link to contact"
}
},
{
"id" : "givenName",
"type" : "semantic",
"href" : "http://schema.org/givenName"
},
{
"id" : "familyName",
"type" : "semantic",
"href" : "http://schema.org/familyName"
},
{
"id" : "email",
"type" : "semantic",
"href" : "http://schema.org/email"
},
{
"id" : "telephone",
"type" : "semantic",
"href" : "http://schema.org/telephone"
}
]
}
]
}
};
// ******************************************
// DEAD CODE
// ******************************************
// hand-roll the mapping this time
function mapTheData() {
var i,x, item;
template.collection.items = [];
for(i=0, x=data.people.length; i<x;i++) {
item = {};
item.href = base+'/contacts/'+data.people[i].id;
item.data = [];
item.data.push({"name" : "givenName", "value" : data.people[i].firstName});
item.data.push({"name" : "familyName", "value" : data.people[i].lastName});
item.data.push({"name" : "email", "value" : data.people[i].primaryEmail});
item.data.push({"name" : "telephone", "value" : data.people[i].voicePhone});
template.collection.items.push(item);
}
}