Skip to content

Commit 5bd8b93

Browse files
authored
Merge pull request #54 from dblock/add-geocoding-api
Add support for the Geocoding API
2 parents 5fa540d + f432341 commit 5bd8b93

14 files changed

Lines changed: 316 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
### 0.7.1 (Next)
1+
### 0.8.0 (Next)
22

3+
* [#54](https://github.com/dblock/open-weather-ruby-client/pull/54): Add support for the Geocoding API (`geo_direct`, `geo_reverse`, `geo_zip`) - [@dblock](https://github.com/dblock).
34
* [#50](https://github.com/dblock/open-weather-ruby-client/pull/50): Fixes `Danger Comment` workflow failing with a `contents: none` permissions error - [@dblock](https://github.com/dblock).
45
* [#51](https://github.com/dblock/open-weather-ruby-client/pull/51): Adds test coverage reporting with [coveralls.io](https://coveralls.io) - [@dblock](https://github.com/dblock).
56
* [#53](https://github.com/dblock/open-weather-ruby-client/pull/53): Fixed coverage reporting to Coveralls not running on pull requests by switching from `coveralls_reborn`/`COVERALLS_REPO_TOKEN` to `coverallsapp/github-action`/`GITHUB_TOKEN` - [@dblock](https://github.com/dblock).

‎README.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Unlike other clients, including [open-weather](https://github.com/coderhs/ruby_o
3232
- [Delete Station](#delete-station)
3333
- [Create Measurements](#create-measurements)
3434
- [Get Measurements](#get-measurements)
35+
- [Geocoding](#geocoding)
36+
- [Direct Geocoding](#direct-geocoding)
37+
- [Reverse Geocoding](#reverse-geocoding)
38+
- [Zip Code Geocoding](#zip-code-geocoding)
3539
- [Configuration](#configuration)
3640
- [Units](#units)
3741
- [Converting Temperature](#converting-temperature)
@@ -380,6 +384,43 @@ client.get_measurements(
380384
) # => Array[OpenWeather::Models::Stations::Measurement]
381385
```
382386

387+
### Geocoding
388+
389+
The [Geocoding API](https://openweathermap.org/api/geocoding-api) converts city names and zip/post codes to geographical coordinates, and vice versa.
390+
391+
#### Direct Geocoding
392+
393+
To get geographical coordinates for a city name, call the client method:
394+
```ruby
395+
client.geo_direct('London', nil, 'GB') # => Array[OpenWeather::Models::GeoLocation]
396+
```
397+
Or pass a hash of options.
398+
```ruby
399+
client.geo_direct(city: 'London', state: nil, country: 'GB', limit: 5) # => Array[OpenWeather::Models::GeoLocation]
400+
```
401+
402+
#### Reverse Geocoding
403+
404+
To get a location name for geographical coordinates, call the client method:
405+
```ruby
406+
client.geo_reverse(51.5074456, -0.1277653) # => Array[OpenWeather::Models::GeoLocation]
407+
```
408+
Or pass a hash of options.
409+
```ruby
410+
client.geo_reverse(lat: 51.5074456, lon: -0.1277653, limit: 5) # => Array[OpenWeather::Models::GeoLocation]
411+
```
412+
413+
#### Zip Code Geocoding
414+
415+
To get geographical coordinates for a zip/post code, call the client method:
416+
```ruby
417+
client.geo_zip('94040', 'US') # => OpenWeather::Models::GeoLocationZip
418+
```
419+
Or pass a hash of options.
420+
```ruby
421+
client.geo_zip(zip: '94040', country: 'US') # => OpenWeather::Models::GeoLocationZip
422+
```
423+
383424
## Configuration
384425

385426
You can configure client options, globally.

‎lib/open_weather/client.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Client
1010
include Endpoints::FiveDayForecast
1111
include Endpoints::OneCall
1212
include Endpoints::Stations
13+
include Endpoints::Geocoding
1314

1415
attr_accessor(*Config::ATTRIBUTES)
1516

‎lib/open_weather/config.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Config
77
ATTRIBUTES = %i[
88
endpoint
99
pro_endpoint
10+
geo_endpoint
1011
api_key
1112
proxy
1213
user_agent
@@ -24,6 +25,7 @@ module Config
2425
def reset
2526
self.endpoint = 'https://api.openweathermap.org/data'
2627
self.pro_endpoint = 'https://pro.openweathermap.org/data'
28+
self.geo_endpoint = 'https://api.openweathermap.org/geo'
2729
self.api_key = nil
2830
self.user_agent = "OpenWeather Ruby Client/#{OpenWeather::VERSION}"
2931
self.ca_path = nil

‎lib/open_weather/endpoints.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
require_relative 'endpoints/hourly'
77
require_relative 'endpoints/thirty_day_forecast'
88
require_relative 'endpoints/five_day_forecast'
9+
require_relative 'endpoints/geocoding'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module OpenWeather
4+
module Endpoints
5+
module Geocoding
6+
def geo_direct(name, state = nil, country = nil, options = {})
7+
options = name.is_a?(Hash) ? options.merge(name) : options.merge(city: name, state: state, country: country)
8+
options[:q] = [
9+
options.delete(:city),
10+
options.delete(:state),
11+
options.delete(:country)
12+
].compact.join(',')
13+
options[:endpoint] ||= geo_endpoint
14+
15+
get('1.0/direct', options).map { |data| OpenWeather::Models::GeoLocation.new(data) }
16+
end
17+
18+
def geo_reverse(lat, lon = nil, options = {})
19+
options = lat.is_a?(Hash) ? options.merge(lat) : options.merge(lat: lat, lon: lon)
20+
options[:endpoint] ||= geo_endpoint
21+
22+
get('1.0/reverse', options).map { |data| OpenWeather::Models::GeoLocation.new(data) }
23+
end
24+
25+
def geo_zip(code, country = nil, options = {})
26+
options = code.is_a?(Hash) ? options.merge(code) : options.merge(zip: code, country: country)
27+
options[:zip] = [
28+
options.delete(:zip),
29+
options.delete(:country)
30+
].compact.join(',')
31+
options[:endpoint] ||= geo_endpoint
32+
33+
OpenWeather::Models::GeoLocationZip.new(get('1.0/zip', options))
34+
end
35+
end
36+
end
37+
end

‎lib/open_weather/models.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
require_relative 'models/one_call'
1616
require_relative 'models/forecast'
1717
require_relative 'models/station'
18+
require_relative 'models/geo_location'
19+
require_relative 'models/geo_location_zip'
1820
require_relative 'models/stations/measurement'
1921
require_relative 'models/stations/temp'
2022
require_relative 'models/stations/humidity'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module OpenWeather
4+
module Models
5+
class GeoLocation < Model
6+
property 'name' # name of the found location
7+
property 'local_names' # name of the found location in different languages
8+
property 'lat' # geo location, latitude
9+
property 'lon' # geo location, longitude
10+
property 'country' # country of the found location
11+
property 'state' # state of the found location
12+
end
13+
end
14+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module OpenWeather
4+
module Models
5+
class GeoLocationZip < Model
6+
property 'zip' # zip/post code
7+
property 'name' # name of the found location
8+
property 'lat' # geo location, latitude
9+
property 'lon' # geo location, longitude
10+
property 'country' # country of the found location
11+
end
12+
end
13+
end

‎lib/open_weather/version.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module OpenWeather
4-
VERSION = '0.7.1'
4+
VERSION = '0.8.0'
55
end

0 commit comments

Comments
 (0)