1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Actions for the Web interface of Benitlux
18 module benitlux_controller
21 private import json
::serialization
27 abstract class BenitluxAction
30 # Path to the database
31 var db_path
= "benitlux_sherbrooke.db"
33 # Path to the storage of the last email sent
34 var sample_email_path
= "benitlux_sherbrooke.email"
37 # Web interface to subscribe to the mailing list
38 class BenitluxSubscriptionAction
41 redef fun answer
(request
, turi
)
43 var template
= new BenitluxDocument
45 var sub
= request
.post_args
.keys
.has
("sub")
46 var unsub
= request
.all_args
.keys
.has
("unsub")
49 if request
.all_args
.keys
.has
("email") then email
= request
.all_args
["email"].trim
52 if email
.is_empty
or not email
.chars
.has
('@') or not email
.chars
.has
('.') then
53 template
.message_level
= "danger"
54 template
.message_content
= "Invalid email."
55 else if sub
and request
.post_args
.keys
.has
("email") then
56 template
.message_level
= "success"
57 template
.message_content
= "Subscription successful!"
59 var db
= new DB.open
(db_path
)
63 template
.message_level
= "warning"
64 template
.message_content
= "You've been unsubscribed."
66 var db
= new DB.open
(db_path
)
72 if sample_email_path
.file_exists
then
73 var f
= new FileReader.open
(sample_email_path
)
74 var lines
= new Array[String]
75 for line
in f
.read_all
.split_with
("\n") do if not line
.is_empty
then lines
.add line
77 template
.sample_email_lines
= lines
80 var response
= new HttpResponse(200)
81 response
.body
= template
.write_to_string
86 # RESTful interface to compare beer offer between given dates
88 # Expects request in the format of `since/2014-07-24`, will replay with a
89 # `BeerEvents` serialized to Json with the necessary meta-data to be deserialized.
90 class BenitluxRESTAction
93 redef fun answer
(request
, turi
)
95 var words
= turi
.split
("/")
96 if not words
.is_empty
and words
.first
.is_empty
then words
.shift
98 if words
.length
>= 2 and words
[0] == "since" then
101 var db
= new DB.open
(db_path
)
102 var events
= db
.beer_events_since
(since
.to_sql_string
)
105 if events
== null then
106 var response
= new HttpResponse(400)
107 response
.body
= "Bad request"
111 var stream
= new StringWriter
112 var serializer
= new JsonSerializer(stream
)
113 serializer
.serialize events
114 var serialized
= stream
.to_s
116 var response
= new HttpResponse(200)
117 response
.body
= serialized
121 var response
= new HttpResponse(400)
122 response
.body
= "Bad request"