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

implemented get-nearestoutwardcode with tests #18

Merged
merged 1 commit into from
Jul 18, 2024
Merged
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
8 changes: 5 additions & 3 deletions Posh-Postcodes.io.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ 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.
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.
Expand All @@ -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 = @()
Expand Down
63 changes: 52 additions & 11 deletions methods/Get-NearestOutwardCode.psm1
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
}



17 changes: 17 additions & 0 deletions tests/Get-NearestOutwardcode.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading