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 # Finally creates a converter and initializes it
28 # We have to redefine the method to do what we want when receiving a dictionary from JS
29 redef fun handle_dictionary
(dictionary
: PepperDictionary)
31 # Getting values from the dictionary
32 var from
= dictionary
["from"]
33 var to
= dictionary
["to"]
34 var value
= dictionary
["value"]
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
41 var d
= new PepperDictionary
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
52 var d
= new PepperDictionary
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
63 var d
= new PepperDictionary
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
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
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
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
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
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
126 var converter
= new Converter
127 converter
.initialize
# Needed to correctly set up Nit control over the Pepper API