Skip to content

Commit a45010d

Browse files
committed
firt version
1 parent 2d6030d commit a45010d

File tree

8 files changed

+123
-16
lines changed

8 files changed

+123
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/Manifest.toml
22
/docs/Manifest.toml
33
/docs/build/
4+
*~

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ uuid = "41b83a75-29e0-460a-8ec7-27bd549dfa6e"
33
authors = ["Thomas Poulsen <ta.poulsen@gmail.com> and contributors"]
44
version = "1.0.0-DEV"
55

6+
[deps]
7+
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
8+
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
9+
610
[compat]
711
julia = "1"
812

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,68 @@
33
[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://tp2750.github.io/ExchangeRates.jl/stable/)
44
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://tp2750.github.io/ExchangeRates.jl/dev/)
55
[![Build Status](https://github.com/tp2750/ExchangeRates.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/tp2750/ExchangeRates.jl/actions/workflows/CI.yml?query=branch%3Amain)
6+
7+
## Purpose
8+
9+
The purpose of this package is to provide a convenient interface to working with exchange rates.
10+
11+
## Data
12+
13+
The data is downloaded from the European Centtral Bank, when the package is loaded.
14+
15+
It uses this link: <https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml>.
16+
17+
## Barewords
18+
19+
The simples interface looks like this:
20+
21+
``` julia
22+
julia> 100EUR/DKK
23+
745.08
24+
```
25+
26+
This shows that you need 745.08 DKK to have 100 EUR.
27+
28+
This is done by defining and exporting variables `EUR`, `USD`, `DKK` etc (using the cool metraprogramming tools of julia).
29+
30+
Each variable is the exchange rate of `EUR`, so `USD` is the value of 1 USD in EUR. This means that `100USD` is the value of 100 USD in EUR.
31+
32+
Note that this uses the juxtaposition syntax of julia, so the can not be a whitespace between the number and the currency without an explicit multiplication:
33+
34+
``` julia
35+
julia> 100 DKK
36+
ERROR: syntax: extra token "DKK" after end of expression
37+
38+
julia> 100 * DKK
39+
13.421377570193805
40+
41+
```
42+
43+
## Function interface
44+
45+
The package exports a single function `fromto`.
46+
It converts from the first argument to the second argument.
47+
48+
``` julia
49+
julia> fromto(:EUR,:DKK)*100
50+
745.08
51+
```
52+
53+
## Unicode
54+
55+
The package knows a few unicode currencies:
56+
57+
* `` (EUR)
58+
* `£` (GBP)
59+
* `¥` (JPY)
60+
61+
but NOT `$`, as `$` has syntactic meaning in julia (string interpolation).
62+
63+
``` julia
64+
julia> 100/£
65+
86.706
66+
67+
julia> fromto(:EUR,:GBP)*100
68+
86.706
69+
```
70+

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ makedocs(;
1515
assets=String[],
1616
),
1717
pages=[
18-
"Home" => "index.md",
18+
"README" => "index.md",
1919
],
2020
)
2121

docs/src/index.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../README.md

src/ExchangeRates.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
module ExchangeRates
22

3-
# Write your package code here.
3+
using EzXML
4+
using HTTP
5+
6+
include("fromto.jl")
7+
export exchage_rates, fromto
8+
9+
include("consts.jl")
10+
# exports EUR, USD, DKK etc
11+
412

513
end

src/consts.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Set variables for rates:
2+
global EUR = 1.0
3+
eval(Meta.parse(join(["global $(x[1]) = EUR/$(x[2]) " for x in pairs(exchange_rates)], ";")));
4+
5+
# 100EUR/DKK == 745.08
6+
# 100NOK/DKK == 66.65891299485574
7+
8+
# export
9+
eval(Meta.parse(join(["export $x " for x in keys(exchange_rates)], ";")));

src/fromto.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using EzXML
2+
using HTTP
3+
4+
function get_xml(; url="https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
5+
@info "Fetching rates from $url"
6+
r1 = HTTP.get(url, ["Accept" => "application/xml"];status_exception=false)
7+
r2 = String(r1.body)
8+
r2
9+
end
10+
11+
function get_rates_dict(; url="https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
12+
s1 = get_xml(url=url)
13+
x1 = EzXML.parsexml(s1)
14+
d1 = x1.root
15+
d = Dict("EUR" => 1.0)
16+
for e in elements(elements(elements(d1)[3])[1])
17+
d[e["currency"]] = parse(Float64,e["rate"])
18+
end
19+
d[""] = d["EUR"]
20+
d["£"] = d["GBP"]
21+
d["¥"] = d["JYP"]
22+
d
23+
end
24+
25+
global exchange_rates = get_rates_dict()
26+
27+
function fromto(from, to; exchange_rates = exchange_rates, url="https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
28+
if !(typeof(exchange_rates) <: Dict)
29+
exchange_rates = get_rates_dict(url=url)
30+
end
31+
exchange_rates[string(to)] / exchange_rates[string(from)]
32+
end
33+

0 commit comments

Comments
 (0)