pickpoint 2.0.3
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/Gemfile +9 -0
- data/LICENSE +202 -0
- data/README.md +132 -0
- data/Rakefile +13 -0
- data/VERSION +1 -0
- data/lib/pickpoint/address.rb +17 -0
- data/lib/pickpoint/auth.rb +213 -0
- data/lib/pickpoint/client.rb +50 -0
- data/lib/pickpoint/config.rb +44 -0
- data/lib/pickpoint/devices.rb +103 -0
- data/lib/pickpoint/errors.rb +39 -0
- data/lib/pickpoint/geocoding.rb +123 -0
- data/lib/pickpoint/http.rb +53 -0
- data/lib/pickpoint/mint.rb +61 -0
- data/lib/pickpoint/routing.rb +40 -0
- data/lib/pickpoint/transport.rb +161 -0
- data/lib/pickpoint/version.rb +5 -0
- data/lib/pickpoint.rb +18 -0
- data/pickpoint.gemspec +38 -0
- metadata +70 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c5da5d860abc72dddb7a6618c2a352678a109e2a41049e5df08bdce4b3b09b7a
|
|
4
|
+
data.tar.gz: 1666532c7873746f27d73862b833117476276de5ed46f265cbe643b11a4554dd
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 55037a1ba536d3cf0223388c0476efe12d117096dda81ccd1a060b8b654bcf245973de5fb68cae587ad63a9ecd7785ad5bb2d5bba481786ea8cf98dd0e260e27
|
|
7
|
+
data.tar.gz: 93626ebdba3d0e21ad6ec7e04c9d9c746aed8d19fdfea2d43b1b8834d9b4d77936558651ae5c59ce924b8cb9467e92b3af02b0c3e6bd150eb061e52780e3a87e
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# pickpoint (Ruby SDK)
|
|
2
|
+
|
|
3
|
+
Official Ruby SDK for [Pickpoint](https://pickpoint.io) — geocoding, address search, routing, and device registry over HTTP.
|
|
4
|
+
|
|
5
|
+
| API | What it does |
|
|
6
|
+
|-----|----------------|
|
|
7
|
+
| **Geocoding** | Address ↔ coordinates (forward, reverse, place lookup) |
|
|
8
|
+
| **Address search** | Typeahead / autocomplete for address inputs |
|
|
9
|
+
| **Routing** | Routes, matrices, optimized multi-stop, elevation |
|
|
10
|
+
| **Devices** | Register / manage tracking devices over HTTP |
|
|
11
|
+
|
|
12
|
+
This gem covers the **public HTTP API only** (no realtime WebSocket / gRPC tracking client). Docs: [pickpoint.io/docs](https://pickpoint.io/docs).
|
|
13
|
+
|
|
14
|
+
Apache-2.0. Siblings: [`@pickpoint/sdk`](https://github.com/pickpoint/pickpoint-js), [`go-sdk`](https://github.com/pickpoint/go-sdk), [`python-sdk`](https://github.com/pickpoint/python-sdk), [`rust-sdk`](https://github.com/pickpoint/rust-sdk).
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
gem install pickpoint
|
|
18
|
+
# or in Gemfile:
|
|
19
|
+
# gem "pickpoint"
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Requires Ruby **3.1+**. Zero runtime dependencies (stdlib `Net::HTTP` + `JSON`).
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Public API
|
|
27
|
+
|
|
28
|
+
One `Client`, one auth session, whole public HTTP surface:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
require "pickpoint"
|
|
32
|
+
|
|
33
|
+
pp = Pickpoint::Client.new(
|
|
34
|
+
Pickpoint::Config.new(api_key: ENV.fetch("PICKPOINT_API_KEY"))
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
places = pp.forward("q" => "Berlin", "limit" => "5")
|
|
38
|
+
pp.reverse("lat" => "52.52", "lon" => "13.405")
|
|
39
|
+
pp.search("q" => "Alexanderplatz")
|
|
40
|
+
pp.route(
|
|
41
|
+
"locations" => [
|
|
42
|
+
{ "lat" => 52.52, "lon" => 13.40 },
|
|
43
|
+
{ "lat" => 52.53, "lon" => 13.42 }
|
|
44
|
+
],
|
|
45
|
+
"costing" => "auto"
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
devices = pp.devices.list
|
|
49
|
+
puts devices.total
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### API map
|
|
53
|
+
|
|
54
|
+
| Method | HTTP | Notes |
|
|
55
|
+
|--------|------|--------|
|
|
56
|
+
| `forward` / `geocoding.forward` | `GET /v2/geocode/forward` | Nominatim-style; returns `Array` |
|
|
57
|
+
| `reverse` / `geocoding.reverse` | `GET /v2/geocode/reverse` | `Hash` or `nil` |
|
|
58
|
+
| `lookup` / `geocoding.lookup` | `GET /v2/address/lookup` | e.g. `osm_ids` |
|
|
59
|
+
| `forward_batch` / `reverse_batch` / `lookup_batch` | same | Geocoding **only**; conveyor ≤20 in flight |
|
|
60
|
+
| `search` / `address.search` | `GET /v2/address/search` | Photon autocomplete |
|
|
61
|
+
| `route` / `optimized_route` / `matrix` / `locate` / `elevation` | `POST /v2/route…` | Valhalla JSON body |
|
|
62
|
+
| `devices.list` / `get` / `create` / `update` / `delete` | `/v2/devices` | Typed structs |
|
|
63
|
+
| `devices.command` | `POST …/command` | Payload bytes (SDK base64-encodes) |
|
|
64
|
+
| `Pickpoint.mint_client_tokens` | `POST /v2/client-tokens` | Needs secret `api_key` |
|
|
65
|
+
|
|
66
|
+
Query params for geocode/address are plain `Hash` of strings (symbol keys are fine).
|
|
67
|
+
|
|
68
|
+
### Auth
|
|
69
|
+
|
|
70
|
+
Provide **exactly one** of:
|
|
71
|
+
|
|
72
|
+
| Field | Header | Use |
|
|
73
|
+
|-------|--------|-----|
|
|
74
|
+
| `api_key` | `x-api-key` | Backends, workers, CLIs |
|
|
75
|
+
| `client_auth` | `Authorization: Bearer` | Short-lived pair; auto-refresh |
|
|
76
|
+
| `access_token` | `Authorization: Bearer` | Static token, no refresh |
|
|
77
|
+
|
|
78
|
+
Keep the secret API key on the server. For client apps mint **client-tokens** and pass `client_auth`.
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
pair = Pickpoint.mint_client_tokens(
|
|
82
|
+
Pickpoint::Config.new(api_key: ENV.fetch("PICKPOINT_API_KEY")),
|
|
83
|
+
scopes: %w[geocoding address routing devices],
|
|
84
|
+
ttl_sec: 600
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
pp = Pickpoint::Client.new(
|
|
88
|
+
Pickpoint::Config.new(
|
|
89
|
+
client_auth: Pickpoint::ClientAuth.new(
|
|
90
|
+
access_token: pair.access_token,
|
|
91
|
+
refresh_token: pair.refresh_token,
|
|
92
|
+
expires_at: pair.expires_at
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Refresh behavior (same as Go/Python/JS):
|
|
99
|
+
|
|
100
|
+
1. Proactive refresh at **~50% of access TTL** (single-flight).
|
|
101
|
+
2. On HTTP **401**, one refresh + retry.
|
|
102
|
+
3. If refresh fails → `Pickpoint::APIError` with auth code.
|
|
103
|
+
|
|
104
|
+
### Config
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
Pickpoint::Config.new(
|
|
108
|
+
api_key: "…",
|
|
109
|
+
base_url: "https://api.pickpoint.io", # default
|
|
110
|
+
timeout: 30.0,
|
|
111
|
+
max_retries: 3,
|
|
112
|
+
retry_base: 1.0,
|
|
113
|
+
concurrency: 20
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
| Constant | Value |
|
|
118
|
+
|----------|--------|
|
|
119
|
+
| `DEFAULT_BASE_URL` | `https://api.pickpoint.io` |
|
|
120
|
+
| `DEFAULT_TIMEOUT` | 30s |
|
|
121
|
+
| `DEFAULT_MAX_RETRIES` | 3 |
|
|
122
|
+
| `DEFAULT_RETRY_BASE` | 1s (`MIN_RETRY_BASE` = 0.2s) |
|
|
123
|
+
| `MAX_CONCURRENCY` | 20 |
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Development
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
bundle install
|
|
131
|
+
bundle exec rake test
|
|
132
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rake/testtask"
|
|
5
|
+
|
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
|
7
|
+
t.libs << "test"
|
|
8
|
+
t.libs << "lib"
|
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
|
10
|
+
t.warning = false
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
task default: :test
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.0.3
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Pickpoint
|
|
6
|
+
class AddressService
|
|
7
|
+
def initialize(transport)
|
|
8
|
+
@t = transport
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def search(query)
|
|
12
|
+
q = (query || {}).transform_keys(&:to_s).transform_values(&:to_s)
|
|
13
|
+
raw = @t.do(Transport::RequestOpts.new(method: "GET", path: "/v2/address/search", query: q))
|
|
14
|
+
JSON.parse(raw)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "monitor"
|
|
5
|
+
|
|
6
|
+
module Pickpoint
|
|
7
|
+
module Auth
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def resolve(cfg, base_url, http)
|
|
11
|
+
n = 0
|
|
12
|
+
n += 1 if present?(cfg.api_key)
|
|
13
|
+
n += 1 if cfg.client_auth
|
|
14
|
+
n += 1 if present?(cfg.access_token)
|
|
15
|
+
if n > 1
|
|
16
|
+
raise InvalidConfigError, "provide only one of: api_key | client_auth | access_token"
|
|
17
|
+
end
|
|
18
|
+
if n.zero?
|
|
19
|
+
raise InvalidConfigError, "auth required: api_key, client_auth, or access_token"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
if present?(cfg.api_key)
|
|
23
|
+
return State.new(api_key: cfg.api_key)
|
|
24
|
+
end
|
|
25
|
+
if cfg.client_auth
|
|
26
|
+
return State.new(session: ClientAuthSession.new(cfg.client_auth, base_url, http))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
State.new(session: StaticSession.new(cfg.access_token))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def present?(s)
|
|
33
|
+
!s.nil? && !s.to_s.empty?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class State
|
|
37
|
+
def initialize(api_key: nil, session: nil)
|
|
38
|
+
@api_key = api_key
|
|
39
|
+
@session = session
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def bearer?
|
|
43
|
+
!@session.nil?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def apply!(headers)
|
|
47
|
+
headers["Accept"] = "application/json"
|
|
48
|
+
if @api_key
|
|
49
|
+
headers["x-api-key"] = @api_key
|
|
50
|
+
return
|
|
51
|
+
end
|
|
52
|
+
headers["Authorization"] = "Bearer #{@session.token}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def refresh_after_unauthorized
|
|
56
|
+
return false if @session.nil?
|
|
57
|
+
|
|
58
|
+
@session.refresh_after_unauthorized
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
class StaticSession
|
|
63
|
+
def initialize(access_token)
|
|
64
|
+
@token = access_token
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def token
|
|
68
|
+
@token
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def refresh_after_unauthorized
|
|
72
|
+
false
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
class ClientAuthSession
|
|
77
|
+
def initialize(initial, base_url, http)
|
|
78
|
+
if blank?(initial.access_token) || blank?(initial.refresh_token) || initial.expires_at.to_i.zero?
|
|
79
|
+
raise InvalidConfigError,
|
|
80
|
+
"client_auth requires access_token, refresh_token, and expires_at (unix ms)"
|
|
81
|
+
end
|
|
82
|
+
@mutex = Monitor.new
|
|
83
|
+
@cond = @mutex.new_cond
|
|
84
|
+
@access = initial.access_token
|
|
85
|
+
@refresh = initial.refresh_token
|
|
86
|
+
@expires_at = initial.expires_at.to_i
|
|
87
|
+
@issued_at = Time.now
|
|
88
|
+
@base_url = base_url
|
|
89
|
+
@http = http
|
|
90
|
+
@refreshing = false
|
|
91
|
+
@waiters = 0
|
|
92
|
+
@last_refresh_error = nil
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def token
|
|
96
|
+
refresh if needs_proactive_refresh?
|
|
97
|
+
@mutex.synchronize { @access }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def refresh_after_unauthorized
|
|
101
|
+
refresh
|
|
102
|
+
true
|
|
103
|
+
rescue StandardError
|
|
104
|
+
false
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def refresh
|
|
108
|
+
wait = false
|
|
109
|
+
refresh_tok = nil
|
|
110
|
+
|
|
111
|
+
@mutex.synchronize do
|
|
112
|
+
if @refreshing
|
|
113
|
+
@waiters += 1
|
|
114
|
+
wait = true
|
|
115
|
+
else
|
|
116
|
+
@refreshing = true
|
|
117
|
+
@last_refresh_error = nil
|
|
118
|
+
refresh_tok = @refresh
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
if wait
|
|
123
|
+
@mutex.synchronize do
|
|
124
|
+
@cond.wait_while { @refreshing }
|
|
125
|
+
err = @last_refresh_error
|
|
126
|
+
@waiters -= 1
|
|
127
|
+
raise err if err
|
|
128
|
+
end
|
|
129
|
+
return
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
err = nil
|
|
133
|
+
begin
|
|
134
|
+
do_refresh(refresh_tok)
|
|
135
|
+
rescue StandardError => e
|
|
136
|
+
err = e
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
@mutex.synchronize do
|
|
140
|
+
@last_refresh_error = err
|
|
141
|
+
@refreshing = false
|
|
142
|
+
@cond.broadcast
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
raise err if err
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
private
|
|
149
|
+
|
|
150
|
+
def blank?(s)
|
|
151
|
+
s.nil? || s.to_s.empty?
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def needs_proactive_refresh?
|
|
155
|
+
now_ms = (Time.now.to_f * 1000).to_i
|
|
156
|
+
issued_ms = (@issued_at.to_f * 1000).to_i
|
|
157
|
+
ttl_ms = @expires_at - issued_ms
|
|
158
|
+
if ttl_ms <= 0
|
|
159
|
+
return now_ms >= (@expires_at - 30_000)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
refresh_after_s = (ttl_ms * CLIENT_AUTH_REFRESH_AT) / 1000.0
|
|
163
|
+
(Time.now - @issued_at) >= refresh_after_s
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def do_refresh(refresh_tok)
|
|
167
|
+
status, raw =
|
|
168
|
+
begin
|
|
169
|
+
@http.request(
|
|
170
|
+
"POST",
|
|
171
|
+
"#{@base_url}/v2/client-tokens/refresh",
|
|
172
|
+
headers: {
|
|
173
|
+
"Accept" => "application/json",
|
|
174
|
+
"Content-Type" => "application/json"
|
|
175
|
+
},
|
|
176
|
+
body: JSON.generate({ "refreshToken" => refresh_tok })
|
|
177
|
+
)
|
|
178
|
+
rescue StandardError => e
|
|
179
|
+
raise APIError.new(code: "REFRESH_FAILED", message: "client token refresh network error: #{e}")
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
unless status >= 200 && status < 300
|
|
183
|
+
raise APIError.new(
|
|
184
|
+
status: status,
|
|
185
|
+
code: "REFRESH_FAILED",
|
|
186
|
+
message: "client token refresh failed (#{status})",
|
|
187
|
+
body: raw
|
|
188
|
+
)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
begin
|
|
192
|
+
pair = JSON.parse(raw)
|
|
193
|
+
rescue JSON::ParserError => e
|
|
194
|
+
raise APIError.new(code: "INVALID_TOKEN", message: "refresh returned invalid JSON: #{e}", body: raw)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
access = pair["accessToken"].to_s
|
|
198
|
+
refresh = pair["refreshToken"].to_s
|
|
199
|
+
expires = pair["expiresAt"].to_i
|
|
200
|
+
if access.empty? || refresh.empty? || expires.zero?
|
|
201
|
+
raise APIError.new(code: "INVALID_TOKEN", message: "refresh returned invalid clientAuth pair", body: raw)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
@mutex.synchronize do
|
|
205
|
+
@access = access
|
|
206
|
+
@refresh = refresh
|
|
207
|
+
@expires_at = expires
|
|
208
|
+
@issued_at = Time.now
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pickpoint
|
|
4
|
+
# Unified public-api client (geocoding, address, routing, devices).
|
|
5
|
+
# This gem does not include realtime tracking.
|
|
6
|
+
class Client
|
|
7
|
+
attr_reader :geocoding, :address, :routing, :devices, :concurrency
|
|
8
|
+
|
|
9
|
+
def initialize(cfg)
|
|
10
|
+
base = Transport.trim_slash(cfg.base_url || DEFAULT_BASE_URL)
|
|
11
|
+
timeout = cfg.timeout && cfg.timeout.positive? ? cfg.timeout : DEFAULT_TIMEOUT
|
|
12
|
+
http = Http.new(timeout: timeout, adapter: cfg.http_adapter)
|
|
13
|
+
|
|
14
|
+
max_retries = cfg.max_retries && cfg.max_retries.positive? ? cfg.max_retries : DEFAULT_MAX_RETRIES
|
|
15
|
+
retry_base = cfg.retry_base && cfg.retry_base.positive? ? cfg.retry_base : DEFAULT_RETRY_BASE
|
|
16
|
+
retry_base = [retry_base, MIN_RETRY_BASE].max
|
|
17
|
+
concurrency = cfg.concurrency && cfg.concurrency.positive? ? cfg.concurrency : DEFAULT_CONCURRENCY
|
|
18
|
+
concurrency = [concurrency, MAX_CONCURRENCY].min
|
|
19
|
+
|
|
20
|
+
auth = Auth.resolve(cfg, base, http)
|
|
21
|
+
transport = Transport::Client.new(
|
|
22
|
+
base_url: base,
|
|
23
|
+
http: http,
|
|
24
|
+
auth: auth,
|
|
25
|
+
max_retries: max_retries,
|
|
26
|
+
retry_base: retry_base
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
@concurrency = concurrency
|
|
30
|
+
@geocoding = GeocodingService.new(transport, concurrency)
|
|
31
|
+
@address = AddressService.new(transport)
|
|
32
|
+
@routing = RoutingService.new(transport)
|
|
33
|
+
@devices = DevicesService.new(transport)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Flat shortcuts
|
|
37
|
+
def forward(query) = geocoding.forward(query)
|
|
38
|
+
def reverse(query) = geocoding.reverse(query)
|
|
39
|
+
def lookup(query) = geocoding.lookup(query)
|
|
40
|
+
def forward_batch(queries) = geocoding.forward_batch(queries)
|
|
41
|
+
def reverse_batch(queries) = geocoding.reverse_batch(queries)
|
|
42
|
+
def lookup_batch(queries) = geocoding.lookup_batch(queries)
|
|
43
|
+
def search(query) = address.search(query)
|
|
44
|
+
def route(body) = routing.route(body)
|
|
45
|
+
def optimized_route(body) = routing.optimized(body)
|
|
46
|
+
def matrix(body) = routing.matrix(body)
|
|
47
|
+
def locate(body) = routing.locate(body)
|
|
48
|
+
def elevation(body) = routing.elevation(body)
|
|
49
|
+
end
|
|
50
|
+
end
|