Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single conversion format e.g "$400". & Add a more relevant API error message #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note that `in` and `to` are interchangable. The default currency is GBP (can be

> c 30USD

> c $30

Another shorthand is:

> c 30$ to €
Expand Down
37 changes: 24 additions & 13 deletions src/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ function getRow($currency_from, $currency_to, $amount){
$data_json = file_get_contents($url, false, $context);
$data = json_decode($data_json);



if($data->result == "success" && getHttpCode($http_response_header) == 200){
if(getHttpCode($http_response_header) == 200 && $data->result == "success"){
$exchange_rate = $data->conversion_rate;
$destination_amount = number_format($exchange_rate * $amount, 2);

Expand All @@ -26,6 +24,15 @@ function getRow($currency_from, $currency_to, $amount){
]
]);

} elseif(getHttpCode($http_response_header) == 429) {
echo json_encode([
"items" => [
[
"title" => "Error: Too many requests",
"subtitle" => "Try again later",
]
]
]);
} else {
echo json_encode([
"items" => [
Expand Down Expand Up @@ -61,8 +68,6 @@ function getWaitingRow(){

function getResult($query, $default_currency){



// Replace symbols
$query = str_replace(
array("€", "$", "£"),
Expand All @@ -81,19 +86,25 @@ function getResult($query, $default_currency){
$query = trim($query);


// parse query
preg_match("/^([0-9]+[.]?[0-9]*?)\s?([A-Z]{3})\s?([A-Z]{3})?$/", $query, $matches);
// If a query contains a quantity with currency after the amount, and/or with a "to" currency specified
preg_match("/^([0-9]+[.]?[0-9]*?)\s?([A-Z]{3})\s?([A-Z]{3})?$/", $query, $match_a_f_t);

// If a query contains only one quantity with the currency before the amount. E.g "$200" or "USD 200"
preg_match("/^([A-Z]{3})\s?([0-9]+[.]?[0-9]*?)$/", $query, $match_f_a);

if(!empty($matches)){
if(!empty($match_a_f_t)){

$amount = $matches[1];
$currency_from = $matches[2];
$currency_to = isset($matches[3]) ? $matches[3] : $default_currency;
$amount = $match_a_f_t[1];
$currency_from = $match_a_f_t[2];
$currency_to = isset($match_a_f_t[3]) ? $match_a_f_t[3] : $default_currency;

// Initial row
getRow($currency_from, $currency_to, $amount);
} elseif(!empty($match_f_a)){
$currency_from = $match_f_a[1];
$amount = $match_f_a[2];

}else{
getRow($currency_from, $default_currency, $amount);
} else {
getWaitingRow();
}
}
Expand Down