Skip to content

Commit e3ff052

Browse files
authored
Merge pull request #13 from stadub/Currency-Conv-Release-1.5
added requests caching #11 and save ApiKey prompt #12
2 parents 2a96919 + 372e316 commit e3ff052

5 files changed

Lines changed: 134 additions & 51 deletions

File tree

Currency-Conv/Currency-Conv.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# RootModule = ''
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.0.0'
15+
ModuleVersion = '1.5.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -98,7 +98,7 @@ PrivateData = @{
9898
PSData = @{
9999

100100
# Tags applied to this module. These help with module discovery in online galleries.
101-
Tags = @('currency','currency convereter','exchange','currency-exchange', 'currencyconverterapi', 'convereter', 'exchang rates' )
101+
Tags = @('currency','currency-convereter','exchange','currency-exchange', 'currencyconverterapi', 'convereter', 'exchange-rates' )
102102

103103
# A URL to the license for this module.
104104
LicenseUri = 'http://opensource.org/licenses/MIT'

Currency-Conv/Currency-Conv.psm1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
. $PSScriptRoot\Functions.ps1
44

5-
CheckPsGalleryUpdate "Currency-Conv" "1.0.0"
5+
CurrencyCheckPsGalleryUpdate "Currency-Conv" "1.5.0"
66

77
_Initalize
88

99
Export-ModuleMember -Function Get-Currencies
10-
Export-ModuleMember -Function GetImpo-ExchangeRate
10+
Export-ModuleMember -Function Get-ExchangeRate
1111
Export-ModuleMember -Function Get-Countries
12+
Export-ModuleMember -Function Remove-CurrencyApiKey
1213
. $PSScriptRoot\Aliases.ps1

Currency-Conv/Functions.ps1

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,59 @@
44

55
function _Initalize() {
66

7+
$script:currenciesCache = [System.Runtime.Caching.MemoryCache]::Default
8+
79
$script:license=$null
810

911
$script:apiKey = $null
1012
$script:baseUrl = $null
1113

12-
$script:apiKeyFile = Get-ProfileDataFile currency-conv ".cur_api_key"
13-
14-
$script:baseUrlFile = Get-ProfileDataFile currency-conv ".path"
14+
$script:apiKeyFile = CurrencyGet-ProfileDataFile currency-conv ".cur_api_key"
1515

16+
$script:baseUrlFile = CurrencyGet-ProfileDataFile currency-conv ".path"
17+
$store = $false
1618

1719
if( !(Test-Path $script:apiKeyFile)){
1820

19-
Write-Console "Api key didn't found on the computer. "
20-
Write-Console "To use the CurrencyConverter api you need to enter your apiKey or receive free api key."
21+
CurrencyWrite-Console "Api key didn't found on the computer. "
22+
CurrencyWrite-Console "To use the CurrencyConverter api you need to enter your apiKey or receive free api key."
2123

22-
while ( Test-Empty $script:baseUrl ){
24+
while ( CurrencyTest-Empty $script:baseUrl ){
2325
LicenseTypePrompt
2426
}
25-
$script:baseUrl | Out-File -FilePath $baseUrlFile
27+
2628

2729
if( $script:license -ne 'fr' ){
28-
Read-Host -Prompt "Enter the received api key here: " | Out-File -FilePath $apiKeyFile
30+
$script:apiKey = Read-Host -Prompt "Enter the received api key here: "
2931
}else{
3032

31-
Read-Host -Prompt "Please open in the browser link 'https://free.currencyconverterapi.com/free-api-key' and follow the site instructions.`
32-
Afterwards enter the received api key here: " | Out-File -FilePath $apiKeyFile
33-
Write-Console "Important: Do not forget to verify your email address."
33+
$script:apiKey = Read-Host -Prompt "Please open in the browser link 'https://free.currencyconverterapi.com/free-api-key' and follow the site instructions.`
34+
Afterwards enter the received api key here: "
35+
CurrencyWrite-Console "Important: Do not forget to verify your email address."
3436
}
37+
38+
$store = currencyShow-ConfirmPrompt -Question "Do you want to store the ApiKey on disk for the future usage?"
39+
40+
}
41+
else{
42+
Get-Content -Path $apiKeyFile | ForEach-Object{ $script:apiKey= $_}
43+
Get-Content -Path $baseUrlFile | ForEach-Object{ $script:baseUrl= $_}
44+
}
45+
46+
47+
if ($store) {
48+
49+
$script:apiKey | Out-File -FilePath $apiKeyFile
50+
$script:baseUrl | Out-File -FilePath $baseUrlFile
51+
52+
#to doublecheck stored data
53+
Get-Content -Path $apiKeyFile | ForEach-Object{ $script:apiKey= $_}
54+
Get-Content -Path $baseUrlFile | ForEach-Object{ $script:baseUrl= $_}
3555
}
3656

37-
Get-Content -Path $apiKeyFile | ForEach-Object{ $script:apiKey= $_}
38-
Get-Content -Path $baseUrlFile | ForEach-Object{ $script:baseUrl= $_}
57+
$global:CurrencyConvExchangeRateCahcheLifetime = $([System.DateTimeOffset]::Now.AddSeconds(60.0))
58+
$global:CurrencyConvCountriesCahcheLifetime = $([System.DateTimeOffset]::Now.AddHours(15.0))
59+
$global:CurrencyConvCurrenciesCahcheLifetime = $([System.DateTimeOffset]::Now.AddHours(15.0))
3960
}
4061

4162
function LicenseTypePrompt {
@@ -136,11 +157,11 @@ function Get-ExchangeRate {
136157

137158

138159
#"${from}_${to}%2C${to}_${from}"
139-
$result = PerformWebRequest "convert" "${From}_${To}"
160+
$result = PerformCahcedWebRequest "convert"-cacheExpirity $global:CurrencyConvExchangeRateCahcheLifetime -arg "${From}_${To}"
140161

141-
$from = $result.results | Select-Object -ExpandProperty "${from}_${to}" | ForEach-Object{$_.fr}
142-
$to = $result.results | Select-Object -ExpandProperty "${from}_${to}" | ForEach-Object{$_.to}
143-
$val = $result.results | Select-Object -ExpandProperty "${from}_${to}" | ForEach-Object{$_.val}
162+
$from = $result.results | Select-Object -ExpandProperty "${from}_${to}" | CurrencyLast | ForEach-Object{$_.fr}
163+
$to = $result.results | Select-Object -ExpandProperty "${from}_${to}" | CurrencyLast | ForEach-Object{$_.to}
164+
$val = $result.results | Select-Object -ExpandProperty "${from}_${to}" | CurrencyLast | ForEach-Object{$_.val}
144165
#$from= $result.results."${from}_${to}"
145166
#$to= $result.results."${to}_${from}"
146167

@@ -179,8 +200,9 @@ Get-Countries
179200
#>
180201

181202
function Get-Countries {
182-
183-
$result = PerformWebRequest "countries"
203+
204+
$result = PerformCahcedWebRequest "countries" $global:CurrencyConvCountriesCahcheLifetime
205+
184206
return $result.results
185207
}
186208

@@ -197,12 +219,32 @@ Supported currencies list
197219
#>
198220

199221
function Get-Currencies {
200-
201-
$result = PerformWebRequest "currencies"
222+
223+
$result = PerformCahcedWebRequest "currencies" $global:CurrencyConvCurrenciesCahcheLifetime
224+
202225
$result.results.PSObject.Properties | ForEach-Object {
203226
$_.Value
204227
}
205-
#return $result.results
228+
}
229+
230+
231+
function PerformCahcedWebRequest {
232+
param (
233+
[string]$func,
234+
[System.DateTimeOffset] $cacheExpirity,
235+
[string]$arg='a'
236+
)
237+
238+
if( -not $script:currenciesCache.Contains($func+$arg)){
239+
240+
$result = PerformWebRequest $func $arg
241+
242+
$script:currenciesCache.Add($func+$arg, $result, $cacheExpirity)
243+
}
244+
245+
246+
return $script:currenciesCache.Get( $func+$arg)
247+
206248
}
207249

208250
#Get-Currencies | Where-Object {$_.id -eq "BYN"}
@@ -224,9 +266,15 @@ Remove currconv.com api key fromthe system
224266
Remove currconv.com api key fromthe system:
225267
226268
#>
227-
function Remove-CurrencyApi-Key {
269+
function Remove-CurrencyApiKey {
270+
271+
Remove-Item $script:apiKeyFile
272+
273+
Remove-Item $script:baseUrlFile
274+
275+
$script:apiKey = $null
276+
$script:baseUrl = $null
277+
$script:license=$null
278+
}
228279

229-
rm $script:apiKeyFile
230280

231-
rm $script:baseUrlFile
232-
}

Currency-Conv/README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,27 @@ Get-Countries -Countries list with currencies
4747
4848
Get-Currencies - Supported currencies list
4949
50-
Remove-CurrencyApi-Key- Clean key
50+
Remove-CurrencyApiKey- Clean key
5151
```
5252

5353
## Aliases
5454

55-
| Cmdlet | Alias |
56-
| -----------------|:--------:|
57-
| Get-Currencies | gxc |
58-
| Get-ExchangeRate | xe |
55+
| Cmdlet | Alias |
56+
| -----------------|:-----:|
57+
| Get-Currencies | gxc |
58+
| Get-ExchangeRate | xe |
59+
60+
## Cache Timeouts
61+
62+
Default cache timeouts are stored in global variables so it can be changed to meet your needs.
63+
64+
Default values:
65+
66+
```powershell
67+
$global:CurrencyConvExchangeRateCahcheLifetime = $([System.DateTimeOffset]::Now.AddSeconds(60.0))
68+
$global:CurrencyConvCountriesCahcheLifetime = $([System.DateTimeOffset]::Now.AddHours(15.0))
69+
$global:CurrencyConvCurrenciesCahcheLifetime = $([System.DateTimeOffset]::Now.AddHours(15.0))
70+
```
5971

6072
## Usage
6173

@@ -96,11 +108,18 @@ List supported currencies:
96108
Remove currconv.com api key fromthe system:
97109

98110
```powershell
99-
/> Remove-CurrencyApi-Key
111+
/> Remove-CurrencyApiKey
100112
```
101113

102114
## Changelog
103115

116+
### [v1.5.0](https://github.com/stadub/PowershellScripts/releases/tag/v0.9.5) Spet 9, 2019
117+
118+
* Add Request cache
119+
* Add ability to change cache timeount
120+
121+
* Rename `Remove-CurrencyApi-Key` to `Remove-CurrencyApiKey` to be ps1 cmdlet name restrictions compliant
122+
104123
### [v1.0.2](https://github.com/stadub/PowershellScripts/releases/tag/v0.9.0) Spet 1, 2019
105124

106125
* Add functions:
@@ -120,7 +139,6 @@ Remove-CurrencyApi-Key- Clean key
120139

121140
* Create tests
122141

123-
124142
## Motivation
125143

126144
The modules are created and actively maintained during evenings and weekends for my own needs.

Currency-Conv/Shared-Functions.ps1

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
function CreateFolderIfNotExist {
3+
function CurrencyCreateFolderIfNotExist {
44
param ([string]$Folder)
55
if( Test-Path $Folder -PathType Leaf){
66
Write-Error "The destanation path ${Folder} is file."
@@ -12,31 +12,31 @@ function CreateFolderIfNotExist {
1212
}
1313

1414

15-
function Test-Empty {
15+
function CurrencyTest-Empty {
1616
param (
1717
[Parameter(Position = 0)]
1818
[string]$string
1919
)
2020
return [string]::IsNullOrWhitespace($string)
2121
}
2222

23-
function Get-ProfileDataFile {
23+
function CurrencyGet-ProfileDataFile {
2424
param (
2525
[string]$file,
2626
[string]$moduleName = $null
2727
)
28-
return Join-Path (Get-ProfileDir $moduleName) $file
28+
return Join-Path (CurrencyGet-ProfileDir $moduleName) $file
2929

3030
}
31-
function Get-ProfileDir {
31+
function CurrencyGet-ProfileDir {
3232
param (
3333
[string]$moduleName = $null,
3434
[string]$profileFolder = $null
3535
)
3636

3737
$profileDir = $ENV:AppData
3838

39-
if( Test-Empty $moduleName ){
39+
if( CurrencyTest-Empty $moduleName ){
4040

4141
if ( $script:MyInvocation.MyCommand.Name.EndsWith('.psm1') ){
4242
$moduleName = $script:MyInvocation.MyCommand.Name
@@ -48,14 +48,14 @@ function Get-ProfileDir {
4848
}
4949
}
5050

51-
if( Test-Empty $moduleName ){
51+
if( CurrencyTest-Empty $moduleName ){
5252
throw "Unable to read module name."
5353
}
5454

55-
$scriptProfile = Combine-Path $profileDir '.ps1' 'ScriptData' $moduleName
55+
$scriptProfile = CurrencyCombine-Path $profileDir '.ps1' 'ScriptData' $moduleName
5656

57-
if( Test-Empty $profileFolder){
58-
$scriptProfile = Combine-Path $profileDir '.ps1' 'ScriptData' $moduleName $profileFolder
57+
if( CurrencyTest-Empty $profileFolder){
58+
$scriptProfile = CurrencyCombine-Path $profileDir '.ps1' 'ScriptData' $moduleName $profileFolder
5959

6060
}
6161
if ( ! (Test-Path $scriptProfile -PathType Container )) {
@@ -66,7 +66,7 @@ function Get-ProfileDir {
6666
}
6767

6868

69-
function Combine-Path {
69+
function CurrencyCombine-Path {
7070
param (
7171
[string]$baseDir,
7272
[string]$path
@@ -76,7 +76,7 @@ function Combine-Path {
7676
[IO.Path]::Combine([string[]]$allArgs)
7777
}
7878

79-
filter Last {
79+
filter CurrencyLast {
8080
BEGIN
8181
{
8282
$current=$null
@@ -93,7 +93,7 @@ filter Last {
9393

9494

9595

96-
function CheckPsGalleryUpdate {
96+
function CurrencyCheckPsGalleryUpdate {
9797
param (
9898
[string] $moduleName,
9999
[string] $currentVersion
@@ -119,7 +119,7 @@ function CheckPsGalleryUpdate {
119119
}
120120
}
121121

122-
function Write-Console {
122+
function CurrencyWrite-Console {
123123
param (
124124
[string]$text,
125125
[String[]]$arg=$null
@@ -131,4 +131,20 @@ function Write-Console {
131131
[Console]::WriteLine($text, $arg)
132132
}
133133

134+
}
135+
136+
function currencyShow-ConfirmPrompt {
137+
param (
138+
[Parameter(Position = 0, ParameterSetName = 'Positional', ValueFromPipeline = $True)]
139+
[Alias("Question", "Description")]
140+
$text ="Would you like to continue?"
141+
)
142+
$reply = Read-Host -Prompt "$text`
143+
[Y] Yes [N] No [S] Suspend(default is ""Yes""):"
144+
145+
if ( $reply -match "[yY]" -and $null -ne $reply ) {
146+
return $true
147+
}
148+
if ( $reply -match "[Ss]" ) { throw "Execution aborted" }
149+
return $false
134150
}

0 commit comments

Comments
 (0)