lib/standard/stream: Renamed streams for more explicit denomination
[nit.git] / contrib / benitlux / src / benitlux_controller.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Actions for the Web interface of Benitlux
18 module benitlux_controller
19
20 import nitcorn
21 import json_serialization
22
23 import benitlux_model
24 import benitlux_db
25 import benitlux_view
26
27 abstract class BenitluxAction
28 super Action
29
30 # Path to the database
31 var db_path = "benitlux_sherbrooke.db"
32
33 # Path to the storage of the last email sent
34 var sample_email_path = "benitlux_sherbrooke.email"
35 end
36
37 # Web interface to subscribe to the mailing list
38 class BenitluxSubscriptionAction
39 super BenitluxAction
40
41 redef fun answer(request, turi)
42 do
43 var template = new BenitluxDocument
44
45 var sub = request.post_args.keys.has("sub")
46 var unsub = request.all_args.keys.has("unsub")
47
48 var email = null
49 if request.all_args.keys.has("email") then email = request.all_args["email"].trim
50
51 if email != null then
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!"
58
59 var db = new DB.open(db_path)
60 db.subscribe email
61 db.close
62 else if unsub then
63 template.message_level = "warning"
64 template.message_content = "You've been unsubscribed."
65
66 var db = new DB.open(db_path)
67 db.unsubscribe email
68 db.close
69 end
70 end
71
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
76 f.close
77 template.sample_email_lines = lines
78 end
79
80 var response = new HttpResponse(200)
81 response.body = template.write_to_string
82 return response
83 end
84 end
85
86 # RESTful interface to compare beer offer between given dates
87 #
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
91 super BenitluxAction
92
93 redef fun answer(request, turi)
94 do
95 var words = turi.split("/")
96 if not words.is_empty and words.first.is_empty then words.shift
97
98 if words.length >= 2 and words[0] == "since" then
99 var since = words[1]
100
101 var db = new DB.open(db_path)
102 var events = db.beer_events_since(since.to_sql_string)
103 db.close
104
105 if events == null then
106 var response = new HttpResponse(400)
107 response.body = "Bad request"
108 return response
109 end
110
111 var stream = new StringWriter
112 var serializer = new JsonSerializer(stream)
113 serializer.serialize events
114 var serialized = stream.to_s
115
116 var response = new HttpResponse(200)
117 response.body = serialized
118 return response
119 end
120
121 var response = new HttpResponse(400)
122 response.body = "Bad request"
123 return response
124 end
125 end