sz

package module
v0.0.0-...-1931a01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2019 License: MIT Imports: 13 Imported by: 0

README

Swedish Zipcode

This package provides validation for Swedish zip codes (or postal codes) based on the provided CSV. The CSV was generated at 2019-01-19 by traversing the Bring API described below between 10000 and 99999.

HTTPS fallback

Since there're constantly changes in zip codes in Sweden a lot of them are missing in the CSV submodule. This package supports the possibiity to fallback to an HTTPS API from Bring.

If you call Store() on the ZipCodes type you can store all newly found zip codes in the existing CSV file for faster and offline execution in the future.

Multiple results

A few zip codes are listed as multiple matches which means they're shared between multiple locations. Instead of choosing one of them none is chosen so to see which these are you can use grep ',$' sweden-zipcode.csv.

Example

set ex (grep  ',$' sweden-zipcode.csv | string sub --length 5 | head -1); \
    curl -sL \
    "https://api.bring.com/shippingguide/api/postalCode.json?clientUrl=ex&country=SE&pnr=$ex" \
    | jq .multipleMatches

[
  "Södertälje",
  "Enhörna"
]

Examples

// One time validation
sz.Valid("12010") // true
sz.Valid(12010)   // true

// Multiple validations - create a cache
httpFallback := true
zc := sz.NewZipCodes(httpFallback)
zc.ClientURL("https://my.url.se")

zc.Valid("12010") // true
zc.Valid(12010)   // true
zc.Valid(99999)   // false

zc.Store()        // Updates the CSV file with new zip codes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Valid

func Valid(in interface{}) bool

Valid will do a one time check if a zip code/postal code is a valid Swedish one.

Types

type ZipCodes

type ZipCodes struct {
	// contains filtered or unexported fields
}

ZipCodes is a type that can be used to only parse the CSV once or use the HTTPS API from Bring.

func NewZipCodes

func NewZipCodes(httpFallback bool) *ZipCodes

NewZipCodes returns a cache that implements the valid function so it can be re-used without parsing the CSV.

func (*ZipCodes) ClientURL

func (z *ZipCodes) ClientURL(url string)

ClientURL sets the querying client URL according to the Bring API documentation.

func (*ZipCodes) Read

func (z *ZipCodes) Read()

func (*ZipCodes) Store

func (z *ZipCodes) Store() error

Store will store all newly found zip codes via external APIs and update the CSV file so it's always expanding.

func (*ZipCodes) Valid

func (z *ZipCodes) Valid(in interface{}) bool

Valid will check if a zip code/postal code is a valid Swedish one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL