Skip to content

Commit

Permalink
Merge pull request #20 from dsanchezcr/packages-update
Browse files Browse the repository at this point in the history
Packages update to v2.0.0
  • Loading branch information
dsanchezcr committed Nov 19, 2023
2 parents 8b60b85 + c275c00 commit f241d46
Show file tree
Hide file tree
Showing 15 changed files with 369 additions and 389 deletions.
108 changes: 75 additions & 33 deletions README.md
@@ -1,6 +1,8 @@
# Colones Exchange Rate NuGet & npm Packages
This repository contains a NuGet & npm packages to provide currency conversion from Colones (Costa Rica - CRC ₡) to Dollars (United States - USD $) and Euros (European Union - EUR €). It consumes the [API from Ministerio de Hacienda de Costa Rica](https://api.hacienda.go.cr/indicadores/tc) (The API is in Spanish).

> Note: The API usually changes the response for the Euro exchange rate, during business hours, the response contains the exchange rate in dollars and colones, but after business hours, the response only contains the exchange rate in dollars. The ColonesExchangeRate packages handles this situation and returns the correct values.
[![ColonesExchangeRate - CI/CD](https://github.com/dsanchezcr/ColonesExchangeRate/actions/workflows/workflow.yaml/badge.svg)](https://github.com/dsanchezcr/ColonesExchangeRate/actions/workflows/workflow.yaml)

![](https://raw.githubusercontent.com/dsanchezcr/ColonesExchangeRate/main/images/Icon.png)
Expand All @@ -15,63 +17,103 @@ To install ColonesExchangeRate using NuGet, run the following command in the Pac
Install-Package ColonesExchangeRate
```
### Usage
To use ColonesExchangeRate, first create an instance of the CurrencyConverter class:
To use ColonesExchangeRate, first create an instance of the class:

```csharp
var converter = new CurrencyConverter();
```
Then, you can use the following methods to perform currency conversion:
```csharp
decimal dolaresAColones = await converter.DollarsToColones(amount);
decimal colonesADolares = await converter.ColonesToDollars(amount);
decimal dolaresAEuros = await converter.DollarsToEuros(amount);
decimal eurosADolares = await converter.EurosToDollars(amount);
decimal colonesAEuros = await converter.ColonesToEuros(amount);
decimal eurosAColones = await converter.EurosToColones(amount);
// true or false to get the date as part of the exchange rate information.
(DateTime? date, decimal sale, decimal purchase) dollarExchangeRate = await converter.GetDollarExchangeRate(true);
(DateTime? date, decimal dollars, decimal colones) euroExchangeRate = await converter.GetEuroExchangeRate(true);
var converter = new ColonesExchangeRate();
var amount = 1000;

decimal dollarsToColones = await converter.DollarsToColones(amount);
decimal colonesToDollars = await converter.ColonesToDollars(amount);
decimal dollarsToEuros = await converter.DollarsToEuros(amount);
decimal eurosToDollars = await converter.EurosToDollars(amount);
decimal colonesToEuros = await converter.ColonesToEuros(amount);
decimal eurosToColones = await converter.EurosToColones(amount);
var dollarExchangeRate = await converter.GetDollarExchangeRate();
var euroExchangeRate = await converter.GetEuroExchangeRate();

Console.WriteLine($"{amount} Dollars = {dollarsToColones} Colones");
Console.WriteLine($"{amount} Colones = {colonesToDollars} Dollars");
Console.WriteLine($"{amount} Dollars = {dollarsToEuros} Euros");
Console.WriteLine($"{amount} Euros = {eurosToDollars} Dollars");
Console.WriteLine($"{amount} Colones = {colonesToEuros} Euros");
Console.WriteLine($"{amount} Euros = {eurosToColones} Colones");

Console.WriteLine($"Dollar exchange rate: {dollarExchangeRate.date} - Sale: {dollarExchangeRate.sale} - Purchase: {dollarExchangeRate.purchase}");
if (euroExchangeRate.colones != null)
Console.WriteLine($"Euro exchange rate: {euroExchangeRate.date} - Dollars: {euroExchangeRate.dollars} - Colones: {euroExchangeRate.colones}");
else
Console.WriteLine($"Euro exchange rate: {euroExchangeRate.date} - Dollars: {euroExchangeRate.dollars}");
```
The result will look similar to this:
![Console Result](https://raw.githubusercontent.com/dsanchezcr/ColonesExchangeRate/main/images/ConsoleResult.jpg)

> Note: Replace amount with the amount of currency you want to convert.
The GetDollarExchangeRate and GetEuroExchangeRate methods return a tuple with three values: the date of the exchange rate, the sale rate, and the purchase rate.
The following methods return a tuple with three values:
- GetDollarExchangeRate: the date of the exchange rate, the sale rate, and the purchase rate.
- GetEuroExchangeRate: the date of the exchange rate, the dollars rate, and the colones rate.

## npm Package

To install ColonesExchangeRate using npm, run the following command in the Package Manager Console:
To install ColonesExchangeRate using npm, run the following command in the command line of your project's root directory:

```cli
npm i @dsanchezcr/colonesexchangerate
```

### Usage

To use ColonesExchangeRate, first import the CurrencyConverter class:
To use ColonesExchangeRate, first import the colonesexchangerate module:
```javascript
import { CurrencyConverter } from '@dsanchezcr/colones-exchange-rate';
```
Then, you can use the following methods to perform currency conversion:
```javascript
const converter = new CurrencyConverter();
const dolaresAColones = await converter.DollarsToColones(amount);
const colonesADolares = await converter.ColonesToDollars(amount);
const dolaresAEuros = await converter.DollarsToEuros(amount);
const eurosADolares = await converter.EurosToDollars(amount);
const colonesAEuros = await converter.ColonesToEuros(amount);
const eurosAColones = await converter.EurosToColones(amount);
// true or false to get the date as part of the exchange rate information.
const dollarExchangeRate = await converter.GetDollarExchangeRate(true);
const euroExchangeRate = await converter.GetEuroExchangeRate(true);
import('@dsanchezcr/colonesexchangerate').then(module => {
// Create a new instance of the class
const exchangeRateClient = new module.default();
// Use the methods of the class
async function test() {
try {
const amount = 1000;
const amountInColones = await exchangeRateClient.dollarsToColones(amount);
console.log(`$${amount} is ₡${amountInColones}`);

const amountInColonesToDollars = await exchangeRateClient.colonesToDollars(amount);
console.log(`${amount} is $${amountInColonesToDollars}`);

const amountInDollarsToEuros = await exchangeRateClient.dollarsToEuros(amount);
console.log(`$${amount} is €${amountInDollarsToEuros}`);

const amountInDollarsFromEuros = await exchangeRateClient.eurosToDollars(amount);
console.log(`${amount} is $${amountInDollarsFromEuros}`);

const amountInColonesToEuros = await exchangeRateClient.colonesToEuros(amount);
console.log(`${amount} is €${amountInColonesToEuros}`);

const amountInEurosToColones = await exchangeRateClient.eurosToColones(amount);
console.log(`${amount} is ₡${amountInEurosToColones}`);

const dollarExchangeRate = await exchangeRateClient.getDollarExchangeRate();
console.log(`Dollar exchange rate: ${JSON.stringify(dollarExchangeRate)}`);

const euroExchangeRate = await exchangeRateClient.getEuroExchangeRate();
console.log(`Euro exchange rate: ${JSON.stringify(euroExchangeRate)}`);
} catch (ex) {
console.error(`Error: ${ex.message}`);
}
}
test();
});
```
The result will look similar to this:
![npm console result](https://raw.githubusercontent.com/dsanchezcr/ColonesExchangeRate/main/images/npmConsoleResult.jpg)

> Note: Replace amount with the amount of currency you want to convert.
The GetDollarExchangeRate and GetEuroExchangeRate methods return a tuple with three values: the date of the exchange rate, the sale rate, and the purchase rate.
The following methods return a tuple with three values:
- getDollarExchangeRate: the date of the exchange rate, the sale rate, and the purchase rate.
- getEuroExchangeRate: the date of the exchange rate, the dollars rate, and the colones rate.

# Contributing
Contributions are welcome! To contribute to ColonesExchangeRate, fork the repository and create a pull request with your changes.

# License
ColonesExchangeRate is licensed under the MIT License. See the [LICENSE](/LICENSE) file for details.
ColonesExchangeRate is licensed under the MIT License. See the [LICENSE](/LICENSE) file for details.
Binary file modified images/ConsoleResult.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/npmConsoleResult.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -1,11 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ColonesExchangeRate" Version="1.0.1" />
<ProjectReference Include="..\ColonesExchangeRate\ColonesExchangeRate.csproj" />
</ItemGroup>
</Project>

</Project>
24 changes: 24 additions & 0 deletions src/NuGet/ClientConsole/Program.cs
@@ -0,0 +1,24 @@
var converter = new ColonesExchangeRate();
var amount = 1000;

decimal dollarsToColones = await converter.DollarsToColones(amount);
decimal colonesToDollars = await converter.ColonesToDollars(amount);
decimal dollarsToEuros = await converter.DollarsToEuros(amount);
decimal eurosToDollars = await converter.EurosToDollars(amount);
decimal colonesToEuros = await converter.ColonesToEuros(amount);
decimal eurosToColones = await converter.EurosToColones(amount);
var dollarExchangeRate = await converter.GetDollarExchangeRate();
var euroExchangeRate = await converter.GetEuroExchangeRate();

Console.WriteLine($"{amount} Dollars = {dollarsToColones} Colones");
Console.WriteLine($"{amount} Colones = {colonesToDollars} Dollars");
Console.WriteLine($"{amount} Dollars = {dollarsToEuros} Euros");
Console.WriteLine($"{amount} Euros = {eurosToDollars} Dollars");
Console.WriteLine($"{amount} Colones = {colonesToEuros} Euros");
Console.WriteLine($"{amount} Euros = {eurosToColones} Colones");

Console.WriteLine($"Dollar exchange rate: {dollarExchangeRate.date} - Sale: {dollarExchangeRate.sale} - Purchase: {dollarExchangeRate.purchase}");
if (euroExchangeRate.colones != null)
Console.WriteLine($"Euro exchange rate: {euroExchangeRate.date} - Dollars: {euroExchangeRate.dollars} - Colones: {euroExchangeRate.colones}");
else
Console.WriteLine($"Euro exchange rate: {euroExchangeRate.date} - Dollars: {euroExchangeRate.dollars}");
171 changes: 84 additions & 87 deletions src/NuGet/ColonesExchangeRate.Tests/CurrencyConverterTests.cs
@@ -1,106 +1,103 @@
using Xunit;
namespace ColonesExchangeRate.Tests
public class CurrencyConverterTests
{
public class CurrencyConverterTests
private readonly ColonesExchangeRate _colonesExchangeRate;
public CurrencyConverterTests()
{
private readonly CurrencyConverter _converter;
public CurrencyConverterTests()
{
_converter = new CurrencyConverter();
}
[Fact]
public async Task DollarsToColones_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10;
_colonesExchangeRate = new ColonesExchangeRate();
}
[Fact]
public async Task DollarsToColones_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10;

// Act
var result = await _converter.DollarsToColones(amount);
// Act
var result = await _colonesExchangeRate.DollarsToColones(amount);

// Assert
Assert.True(result > 0);
}
[Fact]
public async Task ColonesToDollars_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10000;
// Assert
Assert.True(result > 0);
}
[Fact]
public async Task ColonesToDollars_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10000;

// Act
var result = await _converter.ColonesToDollars(amount);
// Act
var result = await _colonesExchangeRate.ColonesToDollars(amount);

// Assert
Assert.True(result > 0);
}
[Fact]
public async Task DollarsToEuros_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10;
// Assert
Assert.True(result > 0);
}
[Fact]
public async Task DollarsToEuros_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10;

// Act
var result = await _converter.DollarsToEuros(amount);
// Act
var result = await _colonesExchangeRate.DollarsToEuros(amount);

// Assert
Assert.True(result > 0);
}
[Fact]
public async Task EurosToDollars_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10;
// Assert
Assert.True(result > 0);
}
[Fact]
public async Task EurosToDollars_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10;

// Act
var result = await _converter.EurosToDollars(amount);
// Act
var result = await _colonesExchangeRate.EurosToDollars(amount);

// Assert
Assert.True(result > 0);
}
[Fact]
public async Task EurosToColones_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10;
// Assert
Assert.True(result > 0);
}
[Fact]
public async Task EurosToColones_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10;

// Act
var result = await _converter.EurosToColones(amount);
// Act
var result = await _colonesExchangeRate.EurosToColones(amount);

// Assert
Assert.True(result > 0);
}
[Fact]
public async Task ColonesToEuros_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10000;
// Assert
Assert.True(result > 0);
}
[Fact]
public async Task ColonesToEuros_ShouldReturnNonNegativeDecimal()
{
// Arrange
decimal amount = 10000;

// Act
var result = await _converter.ColonesToEuros(amount);
// Act
var result = await _colonesExchangeRate.ColonesToEuros(amount);

// Assert
Assert.True(result > 0);
}
[Fact]
public async Task GetDollarExchangeRate_ShouldReturnNonNegativeValues()
{
// Act
var (date, sale, purchase) = await _converter.GetDollarExchangeRate(true);
// Assert
Assert.True(result > 0);
}
[Fact]
public async Task GetDollarExchangeRate_ShouldReturnValues()
{
// Act
var (date, sale, purchase) = await _colonesExchangeRate.GetDollarExchangeRate();

// Assert
Assert.True(sale > 0);
Assert.True(purchase > 0);
Assert.False(date == null);
}
[Fact]
public async Task GetEuroExchangeRate_ShouldReturnNonNegativeValues()
{
// Act
var (date, dollars, colones) = await _converter.GetEuroExchangeRate(true);
// Assert
Assert.False(date == null);
Assert.True(sale > 0);
Assert.True(purchase > 0);
}
[Fact]
public async Task GetEuroExchangeRate_ShouldReturnValues()
{
// Act
var (date, dollars, colones) = await _colonesExchangeRate.GetEuroExchangeRate();

// Assert
Assert.True(dollars > 0);
Assert.True(colones > 0);
Assert.False(date == null);
}
// Assert
Assert.False(date == null);
Assert.True(dollars > 0);
Assert.True(colones > 0 || colones == null);
}
}

0 comments on commit f241d46

Please sign in to comment.