Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
exchangeserve
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Abdulhadi Al-Sayed
exchangeserve
Commits
e8ecb5c0
Commit
e8ecb5c0
authored
4 years ago
by
Abdulhadi Al-Sayed
Browse files
Options
Downloads
Patches
Plain Diff
Refined exchangeborder
parent
e09987ec
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
country.go
+12
-9
12 additions, 9 deletions
country.go
currency.go
+16
-4
16 additions, 4 deletions
currency.go
with
28 additions
and
13 deletions
country.go
+
12
−
9
View file @
e8ecb5c0
...
...
@@ -84,15 +84,18 @@ func GetNeighbour(countryName string, limit int) (string, error) {
country
,
err
:=
gocountries
.
CountriesByName
(
countryName
)
// Extract first country
c
:=
country
[
0
]
// Extract currency code
neighbourAlpha
:=
make
([]
string
,
limit
)
neighbourAlpha
=
c
.
Borders
[
:
]
// Extract border alpha codes
neighbourAlpha
:=
c
.
Borders
[
:
]
// To avoid indexing out of neighbourAlpha's range
if
limit
>
len
(
neighbourAlpha
)
{
limit
=
len
(
neighbourAlpha
)}
// parse neighbour alpha codes and append to API call URL
for
i
,
a
:=
range
neighbourAlpha
{
if
(
i
!=
len
(
neighbourAlpha
)
-
1
)
{
// If not last element in array
borderURL
+=
a
+
";"
}
else
{
borderURL
+=
a
// Avoid appending with ';' at the end
for
i
:=
0
;
i
<
limit
;
i
++
{
if
i
>=
limit
{
// Nothing happens if index exceeds limit
}
else
if
i
==
limit
-
1
{
// If last element in array
borderURL
+=
neighbourAlpha
[
i
]
// Avoid appending with ';' at the end
}
else
if
i
<
limit
{
// If not last element in array
borderURL
+=
neighbourAlpha
[
i
]
+
";"
}
}
// Using http API for restcountriesAPI because gocountries pckg does not support searching by country code
...
...
@@ -111,7 +114,7 @@ func GetNeighbour(countryName string, limit int) (string, error) {
// Make string value of neighbour country currencies for return
currencyCodes
:=
""
for
i
,
a
:=
range
countries
{
if
(
i
!=
len
(
countries
)
-
1
)
{
// If not last element in array
if
i
!=
len
(
countries
)
-
1
{
// If not last element in array
currencyCodes
+=
a
.
Currencies
[
0
]
.
Code
+
","
}
else
{
currencyCodes
+=
a
.
Currencies
[
0
]
.
Code
// Avoid appending with ',' at the end
...
...
This diff is collapsed.
Click to expand it.
currency.go
+
16
−
4
View file @
e8ecb5c0
...
...
@@ -30,22 +30,25 @@ func GetExchangeData(startDate, endDate, currencyCode, currencyBase string) (map
if
err
!=
nil
{
// Error handling data
return
nil
,
err
}
return
Decode
(
resData
)
return
Decode
(
resData
,
"date"
)
}
else
{
// Request for history
// Insert parameters into HISTORYURL for request
resData
,
err
:=
http
.
Get
(
fmt
.
Sprintf
(
HISTORYURL
,
startDate
,
endDate
,
currencyCode
,
currencyBase
))
if
err
!=
nil
{
// Error handling data
return
nil
,
err
}
return
Decode
(
resData
)
return
Decode
(
resData
,
""
)
}
}
// TODO make this function return a struct instead of a map, no random placement
// of JSON arrays and keys like map handles it
// Decode returns a decoded map from a decoded JSON
func
Decode
(
data
*
http
.
Response
)
(
map
[
string
]
interface
{},
error
)
{
/*
Decode returns a decoded map from a decoded JSON
* Optional removal of a key in decoded map
*/
func
Decode
(
data
*
http
.
Response
,
filter
string
)
(
map
[
string
]
interface
{},
error
)
{
var
result
=
make
(
map
[
string
]
interface
{})
// Body object
defer
data
.
Body
.
Close
()
// Closing body after finishing read
...
...
@@ -58,5 +61,14 @@ func Decode(data *http.Response) (map[string]interface{}, error) {
if
err
!=
nil
{
// Error handling decoding
return
nil
,
err
}
// Optional filtering of certain key in map
if
filter
!=
""
{
// Check for filter word existence
_
,
ok
:=
result
[
filter
];
if
ok
{
delete
(
result
,
filter
);
}
}
// Return map with requested data
return
result
,
err
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment