mapricot-api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +77 -0
- data/lib/mapricot/api.rb +132 -0
- metadata +45 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 31d656521ae328f37c56e40ebc8be88f9dcfb2157c530df0af8506239f0926bc
|
|
4
|
+
data.tar.gz: 947d9768c032efb16d615f0d662636006376dc0935164d0f357fbebbc61d7f18
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b797cf979be35da5c8bcdcf14262e8b989b85e61718c2154bee7e20d1e1644bf14f03da1c8ded58821c457f1983c53df7911517f0c2664902171418bc9f1aca8
|
|
7
|
+
data.tar.gz: 2687accc178bff6578f19d3f45abeb42749c69c84d623f18a84eae2050c342297b60bb3a32bfeafdd741c7240718de4a9112462b7dc226e9e4f660a3992ce4fb
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mapricot
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Mapricot for Ruby
|
|
2
|
+
|
|
3
|
+
The official Ruby client for the [Mapricot API](https://mapricot.com/): address and place search, reverse geocoding, nearby places, routing, and IP geolocation, with 250,000 free requests every month.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
gem install mapricot-api
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
It needs Ruby 3.1 or later and has no dependencies outside the standard library.
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
Create a server key in the [dashboard](https://app.mapricot.com/) and set it as `MAPRICOT_KEY`:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
export MAPRICOT_KEY="mapricot_sk_…"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
require "mapricot/api"
|
|
21
|
+
|
|
22
|
+
client = Mapricot::Client.new
|
|
23
|
+
|
|
24
|
+
data = client.search("128 SW 3rd Ave, Portland", limit: 1)
|
|
25
|
+
place = data["results"][0]
|
|
26
|
+
puts place["label"], place["lat"], place["lon"]
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Pass the key yourself with `Mapricot::Client.new(api_key: "mapricot_sk_…")`.
|
|
30
|
+
|
|
31
|
+
## Methods
|
|
32
|
+
|
|
33
|
+
Each method returns the API's JSON as a Hash with string keys, with the same field names as the [API reference](https://mapricot.com/api-reference/).
|
|
34
|
+
|
|
35
|
+
| Method | Endpoint |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| `client.search(q, focus:, country:, limit:, lang:)` | [`GET /v1/search`](https://mapricot.com/docs/geocoding/#find-an-address) |
|
|
38
|
+
| `client.autocomplete(q, focus:, country:, limit:, lang:)` | [`GET /v1/autocomplete`](https://mapricot.com/docs/places/#suggest-as-people-type) |
|
|
39
|
+
| `client.reverse(lat, lon, limit:, lang:)` | [`GET /v1/reverse`](https://mapricot.com/docs/geocoding/#find-the-address-at-a-point) |
|
|
40
|
+
| `client.nearby(lat, lon, category:, radius:, limit:, lang:)` | [`GET /v1/nearby`](https://mapricot.com/docs/places/#list-places-around-a-point) |
|
|
41
|
+
| `client.place(id, lang:)` | [`GET /v1/places/{id}`](https://mapricot.com/docs/places/#look-up-a-saved-place) |
|
|
42
|
+
| `client.route(waypoints, mode:)` | [`POST /v1/route`](https://mapricot.com/docs/routing/#get-a-route) |
|
|
43
|
+
| `client.ip(address)` | [`GET /v1/ip/{ip}`](https://mapricot.com/docs/ip-geolocation/#look-up-an-address) |
|
|
44
|
+
| `client.account` | `GET /v1/account`, which checks a key and is never billed |
|
|
45
|
+
|
|
46
|
+
Every keyword argument is optional. `focus` and each waypoint are `{ lat:, lon: }`. `country` and `category` take an Array or a comma-separated String.
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
nearby = client.nearby(45.52, -122.68, category: ["food:coffee_shop"], radius: 1000)
|
|
50
|
+
route = client.route([{ lat: 45.5189, lon: -122.6793 }, { lat: 45.5214, lon: -122.6828 }], mode: "walk")
|
|
51
|
+
puts route["routes"][0]["durationSeconds"]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
A route's `polyline` is encoded with six decimal places. Decode it with precision 6, as [decoding a route polyline](https://mapricot.com/docs/polyline/) shows.
|
|
55
|
+
|
|
56
|
+
If your app shows results, show the credits in each response's `attribution`.
|
|
57
|
+
|
|
58
|
+
## Errors and retries
|
|
59
|
+
|
|
60
|
+
An error response raises `Mapricot::Error`, with the HTTP `status`, the API's error `code`, a `message`, and the `request_id`:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
begin
|
|
64
|
+
client.search("")
|
|
65
|
+
rescue Mapricot::Error => e
|
|
66
|
+
puts e.status, e.code, e.message # 400, invalid_request, The service rejected the request.
|
|
67
|
+
end
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The client retries `503` responses and `429 concurrency_exceeded`, up to twice, waiting longer each time. Errors aren't billed. Pass `max_retries:` and `timeout:` (seconds) to `Mapricot::Client.new` to change this. The [API reference](https://mapricot.com/api-reference/#errors) lists every status.
|
|
71
|
+
|
|
72
|
+
## Links
|
|
73
|
+
|
|
74
|
+
- [Mapricot](https://mapricot.com/)
|
|
75
|
+
- [Ruby docs](https://mapricot.com/docs/ruby/)
|
|
76
|
+
- [API reference](https://mapricot.com/api-reference/)
|
|
77
|
+
- [Pricing](https://mapricot.com/pricing/)
|
data/lib/mapricot/api.rb
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
|
|
6
|
+
# The official client for the Mapricot API: address and place search, reverse geocoding, nearby places, routing,
|
|
7
|
+
# and IP geolocation. See https://mapricot.com/docs/ruby/.
|
|
8
|
+
module Mapricot
|
|
9
|
+
VERSION = "0.1.0"
|
|
10
|
+
|
|
11
|
+
# An error response from the API.
|
|
12
|
+
class Error < StandardError
|
|
13
|
+
attr_reader :status, :code, :request_id
|
|
14
|
+
|
|
15
|
+
def initialize(status, code, message, request_id)
|
|
16
|
+
super(message)
|
|
17
|
+
@status = status
|
|
18
|
+
@code = code
|
|
19
|
+
@request_id = request_id
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Each method returns the API's JSON as a Hash with string keys, as in https://mapricot.com/api-reference/.
|
|
24
|
+
class Client
|
|
25
|
+
def initialize(api_key: ENV["MAPRICOT_KEY"], base_url: "https://api.mapricot.com", timeout: 30, max_retries: 2)
|
|
26
|
+
raise ArgumentError, "Pass api_key: or set MAPRICOT_KEY." if api_key.nil? || api_key.empty?
|
|
27
|
+
|
|
28
|
+
@api_key = api_key
|
|
29
|
+
@base_url = base_url
|
|
30
|
+
@timeout = timeout
|
|
31
|
+
@max_retries = max_retries
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Find addresses and places matching free text. focus is {lat:, lon:}; country is an Array or a String.
|
|
35
|
+
def search(q, focus: nil, country: nil, limit: nil, lang: nil)
|
|
36
|
+
get("/v1/search", q: q, focus: focus, country: country, limit: limit, lang: lang)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Suggest addresses and places as someone types.
|
|
40
|
+
def autocomplete(q, focus: nil, country: nil, limit: nil, lang: nil)
|
|
41
|
+
get("/v1/autocomplete", q: q, focus: focus, country: country, limit: limit, lang: lang)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Find the address nearest a coordinate, or the area around it.
|
|
45
|
+
def reverse(lat, lon, limit: nil, lang: nil)
|
|
46
|
+
get("/v1/reverse", lat: lat, lon: lon, limit: limit, lang: lang)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# List places around a coordinate, optionally by category, an Array or a String.
|
|
50
|
+
def nearby(lat, lon, category: nil, radius: nil, limit: nil, lang: nil)
|
|
51
|
+
get("/v1/nearby", lat: lat, lon: lon, category: category, radius: radius, limit: limit, lang: lang)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Look up one place by its Mapricot ID.
|
|
55
|
+
def place(id, lang: nil)
|
|
56
|
+
get("/v1/places/#{escape(id)}", lang: lang)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Find a route through 2 to 20 waypoints, each {lat:, lon:}. mode is "drive" (the default), "walk", or "bicycle".
|
|
60
|
+
def route(waypoints, mode: nil)
|
|
61
|
+
body = { waypoints: waypoints, mode: mode }.compact
|
|
62
|
+
request(Net::HTTP::Post, URI("#{@base_url}/v1/route"), JSON.generate(body))
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Approximate location of a public IP address.
|
|
66
|
+
def ip(address)
|
|
67
|
+
get("/v1/ip/#{escape(address)}")
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The key's account. Not billed, so it checks a key for free.
|
|
71
|
+
def account
|
|
72
|
+
get("/v1/account")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
def get(path, **params)
|
|
78
|
+
uri = URI(@base_url + path)
|
|
79
|
+
query = params.compact.transform_values { |value| query_value(value) }
|
|
80
|
+
uri.query = URI.encode_www_form(query) unless query.empty?
|
|
81
|
+
request(Net::HTTP::Get, uri)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def request(method, uri, body = nil)
|
|
85
|
+
attempt = 0
|
|
86
|
+
loop do
|
|
87
|
+
req = method.new(uri)
|
|
88
|
+
req["Authorization"] = "Bearer #{@api_key}"
|
|
89
|
+
req["User-Agent"] = "mapricot-ruby/#{VERSION}"
|
|
90
|
+
if body
|
|
91
|
+
req["Content-Type"] = "application/json"
|
|
92
|
+
req.body = body
|
|
93
|
+
end
|
|
94
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
|
|
95
|
+
open_timeout: @timeout, read_timeout: @timeout) do |http|
|
|
96
|
+
http.request(req)
|
|
97
|
+
end
|
|
98
|
+
return JSON.parse(response.body) if response.is_a?(Net::HTTPOK)
|
|
99
|
+
|
|
100
|
+
error = error_from(response)
|
|
101
|
+
raise error if attempt >= @max_retries || !retryable?(error)
|
|
102
|
+
|
|
103
|
+
sleep(0.5 * 2**attempt)
|
|
104
|
+
attempt += 1
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def query_value(value)
|
|
109
|
+
case value
|
|
110
|
+
when Hash then "#{value.fetch(:lat)},#{value.fetch(:lon)}"
|
|
111
|
+
when Array then value.join(",")
|
|
112
|
+
else value.to_s
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def escape(segment)
|
|
117
|
+
URI.encode_www_form_component(segment).gsub("+", "%20")
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def error_from(response)
|
|
121
|
+
request_id = response["X-Request-Id"]
|
|
122
|
+
error = JSON.parse(response.body).fetch("error")
|
|
123
|
+
Error.new(response.code.to_i, error.fetch("code"), error.fetch("message"), request_id)
|
|
124
|
+
rescue JSON::ParserError, KeyError, TypeError
|
|
125
|
+
Error.new(response.code.to_i, "unknown", "HTTP #{response.code}", request_id)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def retryable?(error)
|
|
129
|
+
error.status == 503 || error.code == "concurrency_exceeded"
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mapricot-api
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mapricot
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
executables: []
|
|
13
|
+
extensions: []
|
|
14
|
+
extra_rdoc_files: []
|
|
15
|
+
files:
|
|
16
|
+
- LICENSE
|
|
17
|
+
- README.md
|
|
18
|
+
- lib/mapricot/api.rb
|
|
19
|
+
homepage: https://mapricot.com/
|
|
20
|
+
licenses:
|
|
21
|
+
- MIT
|
|
22
|
+
metadata:
|
|
23
|
+
documentation_uri: https://mapricot.com/docs/ruby/
|
|
24
|
+
source_code_uri: https://github.com/mapricothq/mapricot-ruby
|
|
25
|
+
bug_tracker_uri: https://github.com/mapricothq/mapricot-ruby/issues
|
|
26
|
+
rubygems_mfa_required: 'true'
|
|
27
|
+
rdoc_options: []
|
|
28
|
+
require_paths:
|
|
29
|
+
- lib
|
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '3.1'
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubygems_version: 3.6.9
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: 'Official client for the Mapricot API: geocoding, places, routing, and IP
|
|
44
|
+
geolocation.'
|
|
45
|
+
test_files: []
|