1 | lazorse = require '../'
|
2 | lazorse ->
|
3 | greetingLookup = english: "Hi", french: "Salut"
|
4 |
|
5 |
|
6 | @resource "/greeting/{language}":
|
7 | description: "Per-language greetings"
|
8 | shortName: 'localGreeting'
|
9 | GET: -> @ok greeting: greetingLookup[@language], language: @language
|
10 | POST: -> greetingLookup[@language] = @req.body; @ok()
|
11 | examples: [
|
12 | {method: 'GET', vars: {language: 'english'}}
|
13 | {method: 'POST', vars: {language: 'english'}, body: "howdy"}
|
14 | ]
|
15 |
|
16 |
|
17 |
|
18 | @coerce "language", """
|
19 | A language to use for localized greetings.
|
20 | Valid values: #{Object.keys(greetingLookup).join(', ')}.
|
21 | """, (language, next) ->
|
22 | language = language.toLowerCase()
|
23 | unless greetingLookup[language]
|
24 | errName = @req.method is 'GET' and 'NotFound' or 'InvalidParameter'
|
25 | @error errName, 'language', language
|
26 | else
|
27 | next null, language
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | cons = require 'consolidate'
|
33 | @render 'text/html', (req, res, next) ->
|
34 | res.setHeader 'Content-Type', 'text/html'
|
35 | engine = req.resource.view?.engine or 'swig'
|
36 | path = req.resource.view?.path or req.resource.shortName or 'fallback.html'
|
37 | cons[engine] path, res.data, (err, html) ->
|
38 | if err? then next err else res.end html
|
39 |
|
40 |
|
41 |
|
42 | class TeapotError
|
43 | constructor: ->
|
44 |
|
45 |
|
46 | @error TeapotError, (err, req, res, next) ->
|
47 | res.statusCode = 418
|
48 | res.end """
|
49 | I'm a little teapot, short and stout.
|
50 | Here is my handle, here is my spout.
|
51 | When I get all steamed up, hear me shout!
|
52 | Tip! me over and pour me out!
|
53 | """
|
54 |
|
55 | <<<<<<< HEAD
|
56 | @resource '/teapot': GET: -> @error 'TeapotError'
|
57 | =======
|
58 | @resource '/teapot': GET: -> @error new TeapotError
|
59 | >>>>>>> b3a39c4... Update example app
|