1 # This file is part of NIT ( http://www.nitlanguage.org )
3 # Copyright 2014 Johan Kayser <kayser.johan@gmail.com>
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 # Example of a currency converter using PNaCl with Nit
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
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)
32 # Getting values from the dictionary
33 var from
= dictionary
["from"]
34 var to
= dictionary
["to"]
35 var value
= dictionary
["value"]
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
42 var d
= new PepperDictionary
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
53 var d
= new PepperDictionary
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
64 var d
= new PepperDictionary
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
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
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
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
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
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
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