From 6c8c8d4f84c16ba6cd3c3e53fad29166ca0d5f2e Mon Sep 17 00:00:00 2001 From: isjwuk <{ID}+{username}@users.noreply.github.com> Date: Thu, 18 Jul 2024 10:43:08 +0100 Subject: [PATCH] implemented get-nearestoutwardcode with tests --- Posh-Postcodes.io.psd1 | 8 ++-- methods/Get-NearestOutwardCode.psm1 | 63 +++++++++++++++++++++----- tests/Get-NearestOutwardcode.Tests.ps1 | 17 +++++++ 3 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 tests/Get-NearestOutwardcode.Tests.ps1 diff --git a/Posh-Postcodes.io.psd1 b/Posh-Postcodes.io.psd1 index 0407e7a..af8c17d 100644 --- a/Posh-Postcodes.io.psd1 +++ b/Posh-Postcodes.io.psd1 @@ -70,7 +70,8 @@ NestedModules = @( '.\methods\Get-PostcodeInfo.psm1', '.\methods\Test-Postcode.psm1', '.\methods\Get-NearestPostcode.psm1', '.\methods\Get-RandomPostcode.psm1', - '.\methods\Get-MatchingPostcode.psm1' + '.\methods\Get-MatchingPostcode.psm1', + '.\methods\Get-NearestOutwardCode.psm1' ) # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. @@ -78,7 +79,8 @@ FunctionsToExport = @('Get-PostcodeInfo', 'Test-Postcode', 'Get-NearestPostcode', 'Get-RandomPostcode', - 'Get-MatchingPostcode' + 'Get-MatchingPostcode', + 'Get-NearestOutwardCode' ) # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. @@ -88,7 +90,7 @@ CmdletsToExport = ('') VariablesToExport = ('') # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = ('') +AliasesToExport = @('Get-NearestOutCode') # DSC resources to export from this module # DscResourcesToExport = @() diff --git a/methods/Get-NearestOutwardCode.psm1 b/methods/Get-NearestOutwardCode.psm1 index 9f91778..950d6a8 100644 --- a/methods/Get-NearestOutwardCode.psm1 +++ b/methods/Get-NearestOutwardCode.psm1 @@ -1,23 +1,64 @@ -#TODO Complete this function + $API_ROOT="https://api.postcodes.io" <# .SYNOPSIS Returns nearest outcodes for a given longitude and latitude or a given Outcode .DESCRIPTION - A longer description of the function, its purpose, common use cases, etc. -.NOTES - Information or caveats about the function e.g. 'This function is not supported in Linux' -.LINK - Specify a URI to a help page, this will show when Get-Help -Online is used. + Returns nearest outward codes for a given longitude and latitude or a given Outward code .EXAMPLE - Test-MyTestFunction -Verbose - Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines + Get-NearestOutwardCode -outcode "EC4M" | Select-Object outcode + Return the nearest outward codes to the given outcode. +.EXAMPLE + Get-NearestOutwardCode -Latitude 51.50460 -Longitude -0.13217 -limit 1 + Return all the data associated with the nearest outcode for the selected Latitude and Longitude. #> function Get-NearestOutwardCode { + [alias("Get-NearestOutCode")] [CmdletBinding()] param( - # [parameter(ValueFromPipeline)] + #Return nearest results for this Outward Code + [Parameter(Mandatory, ParameterSetName="Outcode")] + [string]$outcode, + #Longitude pf Geolocation to return postcodes for + [Parameter(Mandatory, ParameterSetName="GeoLocation")] + [float]$Latitude, + #Latitude pf Geolocation to return postcodes for + [Parameter(Mandatory, ParameterSetName="GeoLocation")] + [float]$Longitude, + #(not required) Limits number of postcodes matches to return. Defaults to 10. Needs to be less than 100. + [Parameter(Mandatory=$false, ParameterSetName="GeoLocation")] + [Parameter(Mandatory=$false, ParameterSetName="Outcode")] + [ValidateRange(1,99)] + [int]$limit=10, + #(not required) Limits number of postcodes matches to return. Defaults to 5000m. Needs to be less than 25,000m. + [Parameter(Mandatory=$false, ParameterSetName="GeoLocation")] + [Parameter(Mandatory=$false, ParameterSetName="Outcode")] + [ValidateRange(1,24999)] + [int]$radius=5000 ) + process { - #TODO + #Decide which API to use based on the Parameter set used when calling this function + switch ($PSCmdlet.ParameterSetName) { + 'GeoLocation' { + $URI="$API_ROOT/outcodes/?lon=$longitude&lat=$latitude&limit=$limit&radius=$radius" + $result=Invoke-RestMethod -Uri $URI -Method Get -SkipHttpErrorCheck + } + 'Outcode' { + $URI="$API_ROOT/outcodes/$outcode/nearest?limit=$limit&radius=$radius" + $result=Invoke-RestMethod -Uri $URI -Method Get -SkipHttpErrorCheck + } + } + #Return the results + switch ($result.status) { + 200 { + $result.result + } + Default { + throw $result.error + } + } } - } \ No newline at end of file + } + + + diff --git a/tests/Get-NearestOutwardcode.Tests.ps1 b/tests/Get-NearestOutwardcode.Tests.ps1 new file mode 100644 index 0000000..a5fc10a --- /dev/null +++ b/tests/Get-NearestOutwardcode.Tests.ps1 @@ -0,0 +1,17 @@ +# Pester Tests for Get-NearestPostcode +BeforeAll { + Import-Module $PSScriptRoot/../Posh-Postcodes.io.psd1 +} +Describe 'Get-NearestOutwardcode' { + It 'Given a valid geolocation it returns postcode data' { + $result=Get-NearestOutwardCode -Latitude 51.50460 -Longitude -0.13217 -limit 1 + $result.Outcode | Should -Be 'SW1A' + } + It 'Given a non-postcoded location return multiple postcodes' { + $result=Get-NearestOutwardCode -outcode "EC4M" | Select-Object outcode + $result.count | Should -Be 10 + } + It 'Given a non-postcoded location return the postcodes using the range parameter' { + { Get-NearestOutCode -outcode "banana"} | Should -Throw -ExpectedMessage "Outcode not found" + } +} \ No newline at end of file