Skip to content

Commit d756f49

Browse files
authored
Merge pull request #1 from Ziptastic/initial-version
Initial version
2 parents ce10200 + 2a10a6e commit d756f49

File tree

15 files changed

+565
-0
lines changed

15 files changed

+565
-0
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
dist
2+
dist-*
3+
cabal-dev
4+
*.o
5+
*.hi
6+
*.chi
7+
*.chs.h
8+
*.dyn_o
9+
*.dyn_hi
10+
.hpc
11+
.hsenv
12+
.cabal-sandbox/
13+
cabal.sandbox.config
14+
*.prof
15+
*.aux
16+
*.hp
17+
*.eventlog
18+
.stack-work/
19+
cabal.project.local

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refer to LICENSE of each project.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### Official Ziptastic Haskell libraries
2+
3+
[![Haskell Programming Language](https://img.shields.io/badge/language-Haskell-blue.svg)](http://www.haskell.org)
4+
[![BSD3 License](http://img.shields.io/badge/license-BSD3-brightgreen.svg)](https://tldrlegal.com/license/bsd-3-clause-license-%28revised%29)
5+
6+
This repository contains the official Haskell binding libraries for the [Ziptastic](https://www.getziptastic.com) API providing forward and reverse geocoding via country, zip code (postal code), latitude, and longitude.
7+
8+
* [ziptastic-core](http://hackage.haskell.org/package/ziptastic-core) provides a complete and type-safe [Servant](http://haskell-servant.readthedocs.io/) API specification for the Ziptastic API.
9+
* [ziptastic-client](http://hackage.haskell.org/package/ziptastic-client) provides a simple Haskell interface to the API.
10+
11+
This repository is maintained by [Grafted-In](https://www.graftedin.io/).

stack.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resolver: lts-8.5
2+
packages:
3+
- 'ziptastic-core'
4+
- 'ziptastic-client'

ziptastic-client/LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright (c) 2017 Elliot Cameron and Ziptastic and Grafted-In LLC
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Elliot Cameron nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

ziptastic-client/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ziptastic-client
2+
3+
[![Haskell Programming Language](https://img.shields.io/badge/language-Haskell-blue.svg)](http://www.haskell.org)
4+
[![BSD3 License](http://img.shields.io/badge/license-BSD3-brightgreen.svg)](https://tldrlegal.com/license/bsd-3-clause-license-%28revised%29)
5+
6+
A convenient and type-safe client library for the [Ziptastic](https://www.getziptastic.com/) API providing forward and reverse geocoding via country, zip code (postal code), latitude, and longitude.
7+
8+
This package is maintained by [Grafted-In](https://www.graftedin.io/).

ziptastic-client/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{-# LANGUAGE
2+
DataKinds
3+
, TypeOperators
4+
, OverloadedStrings
5+
#-}
6+
7+
-- | This module provides a simple interface to Ziptastic's forward and reverse geocoding API
8+
-- (<https://www.getziptastic.com/>).
9+
--
10+
-- > {-# LANGUAGE OverloadedStrings #-}
11+
-- > import Data.ISO3166_CountryCodes (CountryCode(US))
12+
-- > import Network.HTTP.Client (newManager)
13+
-- > import Network.HTTP.Client.TLS (tlsManagerSettings)
14+
-- > import Ziptastic.Client
15+
-- >
16+
-- > apiKey :: ApiKey
17+
-- > apiKey = "abcdefghijklmnopqrstuvwxyz"
18+
-- >
19+
-- > main :: IO ()
20+
-- > main = do
21+
-- > manager <- newManager tlsManagerSettings
22+
-- > print =<< forwardGeocode apiKey manager US "48867"
23+
-- > print =<< reverseGeocode apiKey manager 42.9934 (-84.1595)
24+
module Ziptastic.Client
25+
( ApiKey(..)
26+
, LocaleInfo(..)
27+
, Core.LocaleCoords(..)
28+
, forwardGeocode
29+
, reverseGeocode
30+
, reverseGeocodeWithRadius
31+
) where
32+
33+
import Data.ISO3166_CountryCodes (CountryCode)
34+
import Data.Proxy (Proxy(..))
35+
import Data.Text (Text)
36+
import Network.HTTP.Client (Manager)
37+
import Servant.API ((:<|>)(..))
38+
import Servant.Client
39+
( BaseUrl(..), ClientEnv(..), ClientM, Scheme(..)
40+
, client, runClientM
41+
)
42+
43+
import Ziptastic.Core (ApiKey, ForApi(..), LocaleInfo)
44+
import qualified Ziptastic.Core as Core
45+
46+
forwardGeocode :: ApiKey
47+
-> Manager -- ^ HTTP connection manager
48+
-> CountryCode -- ^ country
49+
-> Text -- ^ postal code
50+
-> IO (Either String [LocaleInfo])
51+
forwardGeocode apiKey manager countryCode postalCode = strLeft <$> runClientM func (ClientEnv manager baseUrl)
52+
where func = forwardGeocode' (apiClient apiKey) (ForApi countryCode) postalCode
53+
54+
-- | Performs a reverse geocode lookup at the given coordinates using a default radius of 5000 meters.
55+
reverseGeocode :: ApiKey
56+
-> Manager -- ^ HTTP connection manager
57+
-> Double -- ^ latitude
58+
-> Double -- ^ longitude
59+
-> IO (Either String [LocaleInfo])
60+
reverseGeocode apiKey manager lat long = reverseGeocodeWithRadius apiKey manager lat long 5000
61+
62+
-- | Performs a reverse geocode lookup at the given coordinates using a specified radius in meters.
63+
reverseGeocodeWithRadius :: ApiKey
64+
-> Manager -- ^ HTTP connection manager
65+
-> Double -- ^ latitude
66+
-> Double -- ^ longitude
67+
-> Int -- ^ radius (in meters)
68+
-> IO (Either String [LocaleInfo])
69+
reverseGeocodeWithRadius apiKey manager lat long radius = strLeft <$> runClientM func (ClientEnv manager baseUrl)
70+
where func = reverseGeocodeWithRadius' (mkReverseGeocode (apiClient apiKey) lat long) radius
71+
72+
strLeft :: Show e => Either e a -> Either String a
73+
strLeft (Left e) = Left (show e)
74+
strLeft (Right a) = Right a
75+
76+
data ApiClient = ApiClient
77+
{ forwardGeocode' :: ForApi CountryCode -> Text -> ClientM [LocaleInfo]
78+
, mkReverseGeocode :: Double -> Double -> ReverseGeocodeApiClient
79+
}
80+
data ReverseGeocodeApiClient = ReverseGeocodeApiClient
81+
{ reverseGeocodeWithRadius' :: Int -> ClientM [LocaleInfo]
82+
, reverseGeocode' :: ClientM [LocaleInfo]
83+
}
84+
85+
-- TODO: This boilerplate can be removed with servant-client 0.10 with Servant.Client.Generic.
86+
apiClient :: ApiKey -> ApiClient
87+
apiClient apiKey = ApiClient
88+
{ forwardGeocode' = forwardGeocodeApi
89+
, mkReverseGeocode = mkReversGeocodeEndpoints
90+
}
91+
where
92+
forwardGeocodeApi :<|> reverseGeocodeApi = client (Proxy :: Proxy Core.Api) (Just apiKey)
93+
mkReversGeocodeEndpoints lat long = ReverseGeocodeApiClient
94+
{ reverseGeocodeWithRadius' = withRadius
95+
, reverseGeocode' = withDefaultRadius
96+
}
97+
where
98+
withRadius :<|> withDefaultRadius = reverseGeocodeApi lat long
99+
100+
baseUrl :: BaseUrl
101+
baseUrl = BaseUrl
102+
{ baseUrlScheme = if Core.baseUrlIsHttps then Https else Http
103+
, baseUrlHost = Core.baseUrlHost
104+
, baseUrlPort = Core.baseUrlPort
105+
, baseUrlPath = Core.baseUrlPath
106+
}
107+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: ziptastic-client
2+
version: 0.1.0.0
3+
synopsis:
4+
Core Servant specification for the Ziptastic API (https://www.getziptastic.com) for doing forward and reverse geocoding.
5+
description:
6+
This package provides a type-safe Servant specification for the Ziptastic
7+
(https://www.getziptastic.com) API for doing forward and reverse geocoding
8+
via zip/postal code, latitude, and longitude.
9+
.
10+
This package is maintained by Grafted-In (https://www.graftedin.io/).
11+
homepage: https://github.com/Ziptastic/ziptastic-haskell#readme
12+
license: BSD3
13+
license-file: LICENSE
14+
author: Elliot Cameron
15+
maintainer: elliot@graftedin.io
16+
copyright: 2017 Elliot Cameron and Ziptastic and Grafted-In LLC
17+
category: Web
18+
build-type: Simple
19+
extra-source-files: README.md
20+
cabal-version: >=1.10
21+
tested-with: GHC==8.0.2
22+
23+
library
24+
hs-source-dirs: src
25+
exposed-modules: Ziptastic.Client
26+
build-depends:
27+
base >= 4.7 && < 5
28+
, bytestring
29+
, aeson
30+
, http-api-data
31+
, http-client
32+
, http-client-tls
33+
, iso3166-country-codes
34+
, servant
35+
, servant-client
36+
, text
37+
, tz
38+
, ziptastic-core
39+
default-language: Haskell2010
40+
extensions:
41+
DataKinds
42+
TypeOperators
43+
OverloadedStrings
44+
ghc-options: -Wall
45+
46+
source-repository head
47+
type: git
48+
location: https://github.com/Ziptastic/ziptastic-haskell

ziptastic-core/LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright (c) 2017 Elliot Cameron and Ziptastic and Grafted-In LLC
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Elliot Cameron nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

ziptastic-core/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ziptastic-core
2+
3+
[![Haskell Programming Language](https://img.shields.io/badge/language-Haskell-blue.svg)](http://www.haskell.org)
4+
[![BSD3 License](http://img.shields.io/badge/license-BSD3-brightgreen.svg)](https://tldrlegal.com/license/bsd-3-clause-license-%28revised%29)
5+
6+
Core [Servant](http://haskell-servant.readthedocs.io/en/stable/) specification for the [Ziptastic](https://www.getziptastic.com/) API providing forward and reverse geocoding via country, zip code (postal code), latitude, and longitude.
7+
8+
This package is maintained by [Grafted-In](https://www.graftedin.io/).
9+
10+
If you want to use the Ziptastic API in your application, try the [ziptastic-client](http://hackage.haskell.org/package/ziptastic-client) package.

ziptastic-core/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

0 commit comments

Comments
 (0)