69966bd02472fcb350ea594ec689aff2f3559387
[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) do
31 # Getting values from the dictionary
32 var from = dictionary["from"]
33 var to = dictionary["to"]
34 var value = dictionary["value"]
35
36 # If conversion to the same currency return the same value
37 if from isa String and to isa String then
38 if from == "EUR" and to == "EUR" then
39 if value isa String then
40 var rez = value.to_f
41 var d = new PepperDictionary
42 d["value"] = rez
43 post_dictionary d
44 end
45 end
46 end
47 # If conversion to the same currency return the same value
48 if from isa String and to isa String then
49 if from == "CAD" and to == "CAD" then
50 if value isa String then
51 var rez = value.to_f
52 var d = new PepperDictionary
53 d["value"] = rez
54 post_dictionary d
55 end
56 end
57 end
58 # If conversion to the same currency return the same value
59 if from isa String and to isa String then
60 if from == "USD" and to == "USD" then
61 if value isa String then
62 var rez = value.to_f
63 var d = new PepperDictionary
64 d["value"] = rez
65 post_dictionary d
66 end
67 end
68 end
69 # Converts EUR to CAD
70 if from == "EUR" and to == "CAD" then
71 if value isa String then
72 var rez = (value.to_f * 1.52) # April 30 2014
73 var d = new PepperDictionary
74 d["value"] = rez
75 post_dictionary d
76 end
77 end
78 # Converts EUR to USD
79 if from == "EUR" and to == "USD" then
80 if value isa String then
81 var rez = (value.to_f * 1.38640) # April 30 2014
82 var d = new PepperDictionary
83 d["value"] = rez
84 post_dictionary d
85 end
86 end
87 # Converts CAD to EUR
88 if from == "CAD" and to == "EUR" then
89 if value isa String then
90 var rez = (value.to_f * 0.65840) # April 30 2014
91 var d = new PepperDictionary
92 d["value"] = rez
93 post_dictionary d
94 end
95 end
96 # Converts CAD to USD
97 if from == "CAD" and to == "USD" then
98 if value isa String then
99 var rez = (value.to_f * 0.91240) # April 30 2014
100 var d = new PepperDictionary
101 d["value"] = rez
102 post_dictionary d
103 end
104 end
105 # Converts USD to EUR
106 if from == "USD" and to == "EUR" then
107 if value isa String then
108 var rez = (value.to_f * 0.721) # April 30 2014
109 var d = new PepperDictionary
110 d["value"] = rez
111 post_dictionary d
112 end
113 end
114 # Converts USD to CAD
115 if from == "USD" and to == "CAD" then
116 if value isa String then
117 var rez = (value.to_f * 1.095) # April 30 2014
118 var d = new PepperDictionary
119 d["value"] = rez
120 post_dictionary d
121 end
122 end
123 end
124 end
125
126 redef fun app do return once new Converter
127 app.initialize # Needed to correctly set up Nit control over the Pepper API
128 app.run # Launches an infinite loop in order to check for dictionaries