lib: move `examples/pnacl` to `lib/pnacl/examples`
[nit.git] / lib / pnacl / examples / converter / converter.nit
1 # This file is part of NIT ( http://www.nitlanguage.org )
2 #
3 # Copyright 2014 Johan Kayser <kayser.johan@gmail.com>
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 # Example of a currency converter using PNaCl with Nit
18 #
19 # First imports the pnacl module
20 # Then redefines the 'handle_dictionary' method
21 # Creates a converter and initializes it
22 # Finally checks for dictionaries
23
24 import pnacl
25
26 class Converter
27 super PnaclApp
28
29 # We have to redefine the method to do what we want when receiving a dictionary from JS
30 redef fun handle_dictionary(dictionary: PepperDictionary)
31 do
32 # Getting values from the dictionary
33 var from = dictionary["from"]
34 var to = dictionary["to"]
35 var value = dictionary["value"]
36
37 # If conversion to the same currency return the same value
38 if from isa String and to isa String then
39 if from == "EUR" and to == "EUR" then
40 if value isa String then
41 var rez = value.to_f
42 var d = new PepperDictionary
43 d["value"] = rez
44 post_dictionary d
45 end
46 end
47 end
48 # If conversion to the same currency return the same value
49 if from isa String and to isa String then
50 if from == "CAD" and to == "CAD" then
51 if value isa String then
52 var rez = value.to_f
53 var d = new PepperDictionary
54 d["value"] = rez
55 post_dictionary d
56 end
57 end
58 end
59 # If conversion to the same currency return the same value
60 if from isa String and to isa String then
61 if from == "USD" and to == "USD" then
62 if value isa String then
63 var rez = value.to_f
64 var d = new PepperDictionary
65 d["value"] = rez
66 post_dictionary d
67 end
68 end
69 end
70 # Converts EUR to CAD
71 if from == "EUR" and to == "CAD" then
72 if value isa String then
73 var rez = (value.to_f * 1.52) # April 30 2014
74 var d = new PepperDictionary
75 d["value"] = rez
76 post_dictionary d
77 end
78 end
79 # Converts EUR to USD
80 if from == "EUR" and to == "USD" then
81 if value isa String then
82 var rez = (value.to_f * 1.38640) # April 30 2014
83 var d = new PepperDictionary
84 d["value"] = rez
85 post_dictionary d
86 end
87 end
88 # Converts CAD to EUR
89 if from == "CAD" and to == "EUR" then
90 if value isa String then
91 var rez = (value.to_f * 0.65840) # April 30 2014
92 var d = new PepperDictionary
93 d["value"] = rez
94 post_dictionary d
95 end
96 end
97 # Converts CAD to USD
98 if from == "CAD" and to == "USD" then
99 if value isa String then
100 var rez = (value.to_f * 0.91240) # April 30 2014
101 var d = new PepperDictionary
102 d["value"] = rez
103 post_dictionary d
104 end
105 end
106 # Converts USD to EUR
107 if from == "USD" and to == "EUR" then
108 if value isa String then
109 var rez = (value.to_f * 0.721) # April 30 2014
110 var d = new PepperDictionary
111 d["value"] = rez
112 post_dictionary d
113 end
114 end
115 # Converts USD to CAD
116 if from == "USD" and to == "CAD" then
117 if value isa String then
118 var rez = (value.to_f * 1.095) # April 30 2014
119 var d = new PepperDictionary
120 d["value"] = rez
121 post_dictionary d
122 end
123 end
124 end
125 end
126
127 redef fun app do return once new Converter
128 app.initialize # Needed to correctly set up Nit control over the Pepper API
129 app.run # Launches an infinite loop in order to check for dictionaries