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

Get matching postcodes implemented #17

Merged
merged 2 commits into from
Jul 15, 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 @@ -68,15 +68,17 @@ Copyright = ''
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @( '.\methods\Get-PostcodeInfo.psm1',
'.\methods\Test-Postcode.psm1',
'.\methods\Get-NearestPostcode.psm1'
'.\methods\Get-RandomPostcode.psm1'
'.\methods\Get-NearestPostcode.psm1',
'.\methods\Get-RandomPostcode.psm1',
'.\methods\Get-MatchingPostcode.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-RandomPostcode',
'Get-MatchingPostcode'
)

# 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 Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Get-Help Get-NearestPostcode -ShowWindow
## Testing
Pester is used for unit testing.
```powershell
Invoke-Pester .\tests\*.Tests.ps1
Invoke-Pester .\tests\*.Tests.ps1 -Output detailed
```

PSScriptAnalyzer is used for identifying common issues
Expand Down
38 changes: 27 additions & 11 deletions methods/Get-MatchingPostcode.psm1
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
#TODO Complete this function
$API_ROOT="https://api.postcodes.io"
<#
.SYNOPSIS
Convenience method to return an list of matching postcodes.
Submit a postcode query and receive a complete list of postcode matches and all associated postcode data.
.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.
This is essentially a postcode search which prefix matches and returns postcodes in sorted order (case insensitive)
.EXAMPLE
Test-MyTestFunction -Verbose
Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
Get-MatchingPostcode -PostcodeStart "MK3 6E" | Select postcode
Return the 9 postcodes that start "MK3 6E"
.EXAMPLE
Get-MatchingPostcode -PostcodeStart "N1 " -limit 30 | Select postcode
Return the first 30 postcodes (in alphabetical order) that have the N1 outcode
.EXAMPLE
Get-MatchingPostcode -PostcodeStart "SW11 1A" -limit 20
Return the 18 postcodes that start "SW11 1A" including all the associated details.
#>
function Get-MatchingPostcode {
[CmdletBinding()]
param(
# [parameter(ValueFromPipeline)]
# Filters random postcodes by outcode. Returns null if invalid outcode.
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$PostcodeStart,
#(not required) Limits number of postcodes matches to return. Defaults to 10. Needs to be less than 100.
[Parameter(Mandatory=$false)]
[ValidateRange(1,99)]
[int]$limit=10
)

process {
#TODO
$result=Invoke-RestMethod -Uri "$API_ROOT/postcodes?query=$PostcodeStart&limit=$limit" -Method Get -SkipHttpErrorCheck
switch ($result.status) {
200 {
$result.result
}
Default {
throw $result.error
}
}
}
}
23 changes: 23 additions & 0 deletions tests/Get-MatchingPostcode.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Pester Tests for Get-MatchingPostcode
BeforeAll {
Import-Module $PSScriptRoot/../Posh-Postcodes.io.psd1
}
Describe 'Get-MatchingPostcode' {
It 'Given a full postcode returns postcode data for that postcode' {
$result=Get-MatchingPostcode -PostcodeStart 'SW1A 1AA'
$result.count | Should -Be 1
}
It 'Given a partial postcode returns postcode data for many postcodes' {
$result=Get-MatchingPostcode -PostcodeStart 'SW1A 1A'
$result.count | Should -Be 4
}
It 'Given two valid outcode on the pipeline, it returns values for both' {
$result='GU2 7X','GU2 7Y' | Get-MatchingPostcode -limit 20
$result.count | Should -Be 24
}
It 'Given a nonsense partial it returns no data'{
$result=Get-MatchingPostcode -PostcodeStart 'banana'
$result | Should -Be $null
}

}
Loading