forked from ringcentral/slate
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfields_template.php
107 lines (85 loc) · 3.16 KB
/
fields_template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
function template($country, $fields) {
switch ($country) {
case 'us':
$address = '1109 N Highland St, Arlington VA';
$coordinate = '38.886672,-77.094735';
break;
case 'ca':
$address = '300 King St, Sturgeon Falls, ON P2B 3A1, Canada';
$coordinate = '46.225866,-79.36316';
break;
case 'ca-alt':
$address = '203 Laycoe Crescent, Saskatoon, SK, Canada';
$coordinate = '52.155106,-106.589896';
break;
default:
throw new \Exception('Unsupported country: ' . $country);
}
$addressUrlEncoded = urlencode($address);
$coordinateWithSpace = str_replace(',', ', ', $coordinate);
$fieldsWithBackTicks = array_map(function ($f) { return '`' . $f . '`'; }, $fields);
$fieldAppendsDescription = count($fieldsWithBackTicks) === 2
? implode(' and ', $fieldsWithBackTicks)
: implode(', ', $fieldsWithBackTicks);
$commaSeparated = implode(',', $fields);
$spaceSeparated = implode(' ', $fields);
$doubleQuotedCommaSeparated = implode(', ', array_map(function ($f) { return '"' . $f . '"'; }, $fields));
$singleQuotedCommaSeparated = implode(', ', array_map(function ($f) { return "'" . $f . "'"; }, $fields));
$doubleQuotedSpaceSeparated = implode(' ', array_map(function ($f) { return '"' . $f . '"'; }, $fields));
return <<<TEMPLATE
> To get $fieldAppendsDescription field appends for an address or a coordinate:
```shell
curl "https://api.geocod.io/v1.7/geocode?q=$addressUrlEncoded&fields=$commaSeparated&api_key=YOUR_API_KEY"
curl "https://api.geocod.io/v1.7/reverse?q=$coordinate&fields=$commaSeparated&api_key=YOUR_API_KEY"
```
```ruby
require 'geocodio/gem'
geocodio = Geocodio::Gem.new('YOUR_API_KEY')
location = geocodio.geocode(['$address'], [$singleQuotedCommaSeparated])
location = geocodio.reverse(['$coordinate'], [$singleQuotedCommaSeparated])
```
```python
from geocodio import GeocodioClient
client = GeocodioClient(YOUR_API_KEY)
location = client.geocode("$address", fields=[$doubleQuotedCommaSeparated])
location = client.reverse(($coordinateWithSpace), fields=[$doubleQuotedCommaSeparated])
```
```php
<?php
\$response = \$geocoder->geocode('$address', [$singleQuotedCommaSeparated]);
\$response = \$geocoder->reverse('$coordinate', [$singleQuotedCommaSeparated]);
```
```javascript
const Geocodio = require('geocodio-library-node');
const geocodio = new Geocodio('YOUR_API_KEY');
geocoder.geocode('$address', [$singleQuotedCommaSeparated])
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
}
);
geocoder.reverse('$coordinate', [$singleQuotedCommaSeparated])
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
}
);
```
```clojure
(ns my.ns
(:require [rodeo.core :refer :all]))
(single "$address" :api_key "YOUR_API_KEY" :fields [$doubleQuotedSpaceSeparated])
(single-reverse "$coordinate" :api_key "YOUR_API_KEY" :fields [$doubleQuotedSpaceSeparated])
```
TEMPLATE;
}
echo preg_replace_callback('/<!--FIELD:([A-Z-]+):([A-Z0-9-,]+)-->/i', function($match) {
list(,$country, $fields) = $match;
$fields = explode(',', $fields);
return template($country, $fields);
}, file_get_contents('source.html.md'));