-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package exchangerate | ||
|
||
import ( | ||
"companion" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func New(targetCurrency string) (companion.CurrencyConverter, error) { | ||
rates, err := getRates(targetCurrency) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &converter{ | ||
rates: *rates, | ||
}, nil | ||
} | ||
|
||
type converter struct { | ||
rates ConversionRates | ||
} | ||
|
||
func (c *converter) Convert(fromAmount int, fromCurrency string, toCurrency string) int { | ||
|
||
fromCurrency = strings.ToLower(fromCurrency) | ||
|
||
return int(c.rates[fromCurrency].InverseRate * float64(fromAmount)) | ||
|
||
} | ||
|
||
func getRates(toCurrency string) (*ConversionRates, error) { | ||
|
||
log.Println("download exchange rates for", toCurrency) | ||
resp, err := http.Get(fmt.Sprintf("http://www.floatrates.com/daily/%v.json", strings.ToLower(toCurrency))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
rates := &ConversionRates{} | ||
json.NewDecoder(resp.Body).Decode(rates) | ||
|
||
return rates, nil | ||
} | ||
|
||
type ConversionRates map[string]struct { | ||
Code string `json:"code"` | ||
AlphaCode string `json:"alphaCode"` | ||
NumericCode string `json:"numericCode"` | ||
Name string `json:"name"` | ||
Rate float64 `json:"rate"` | ||
Date string `json:"date"` | ||
InverseRate float64 `json:"inverseRate"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package companion | ||
|
||
type CurrencyConverter interface { | ||
Convert(fromAmount int, fromCurrency string, toCurrency string) int | ||
} |