open_weather_client 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +1 -1
- data/.rubocop.yml +1 -1
- data/CHANGES.md +3 -0
- data/README.md +25 -6
- data/bin/console +2 -3
- data/lib/open_weather_client/caching/memory.rb +2 -2
- data/lib/open_weather_client/configuration.rb +3 -0
- data/lib/open_weather_client/request.rb +13 -1
- data/lib/open_weather_client/version.rb +1 -1
- data/lib/open_weather_client.rb +4 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6d4db65e59ab7bde82a64f2b3da01fbd80cc1e5f4acf7b5084d9dc7ec7db89e
|
4
|
+
data.tar.gz: 43b2bb7361f68e91b23e022703cf2ba436f8cea7734dd7495d9f6a31a6cc8215
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 880007ce334e68cacac086112242a304c081a5535e593589e6797f48d1d3061247cc51f8663deb750e20816978e3b5947d44a466c85d3fe5145c999e5e83676a
|
7
|
+
data.tar.gz: f2fdabce1f5d502b41e0cc2c97c8f7efc0d0e121dd8f38fe9465008f3799436efd19bbe81a7c87dadc066e025832149923dd2b563c7a37c7e48eb41e29cd4dc2
|
data/.github/workflows/ruby.yml
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
![RSpec](https://github.com/qurasoft/open_weather_client/actions/workflows/ruby.yml/badge.svg)
|
5
5
|
|
6
6
|
Welcome to Open Weather Client.
|
7
|
-
This gem allows you to easily request weather information from
|
7
|
+
This gem allows you to easily request weather information from Open Weather.
|
8
8
|
It integrates in your rails project, when you are using bundler or even in plain ruby projects.
|
9
9
|
|
10
10
|
## Installation
|
@@ -24,7 +24,7 @@ Or install it yourself as:
|
|
24
24
|
$ gem install open_weather_client
|
25
25
|
|
26
26
|
## Usage
|
27
|
-
During configuration the
|
27
|
+
During configuration the Open Weather API key must be set. Afterwards it is as simple as calling `OpenWeatherClient.current(lat:, lon:)` to get the current weather at a location.
|
28
28
|
|
29
29
|
```ruby
|
30
30
|
# OpenWeatherClient initializer
|
@@ -36,13 +36,32 @@ end
|
|
36
36
|
OpenWeatherClient::Weather.current(lat: 50.3569, lon: 7.5890)
|
37
37
|
```
|
38
38
|
|
39
|
+
### Used Open Weather API version
|
40
|
+
Open Weather provides One Call API 3.0 and API 2.5, which is expected to be deprecated in June 2024.
|
41
|
+
Until API 2.5 is deprecated it is the default. With Open Weather Client version 1.0 the default will change to One Call API 3.0.
|
42
|
+
|
43
|
+
The used API version is configurable through `OpenWeatherClient.configuration.api_version`.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# OpenWeatherClient initializer
|
47
|
+
OpenWeatherClient.configure do |config|
|
48
|
+
config.api_version = :v25 # (default) also supports :v30
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
39
52
|
### Exceptions during requests
|
40
53
|
When an error occurs during a request, an exception is raised.
|
41
|
-
|
42
|
-
|
54
|
+
|
55
|
+
If the configured API version is not supported by the client, `OpenWeatherClient::APIVersionNotSupportedError` is raised.
|
56
|
+
If the request is not authorized, `OpenWeatherClient::AutheniticationError` is raised.
|
57
|
+
If attributes like latitude or longitude are outside of the expected range, `RangeError` is raised.
|
58
|
+
If the time is longer in the past than one hour, `OpenWeatherClient::NotCurrentError` is raised.
|
43
59
|
|
44
60
|
### Secure Configuration
|
45
|
-
|
61
|
+
Rails provides the credentials functionality for [environmental security](https://edgeguides.rubyonrails.org/security.html#environmental-security).
|
62
|
+
This mechanism can be used by OpenWeatherClient to load the API key from an encrypted file.
|
63
|
+
This also allows easy separation of production and development api key configuration.
|
64
|
+
|
46
65
|
All settings are defined under the top-level entry `open_weather_client`.
|
47
66
|
```yaml
|
48
67
|
# $ bin/rails credentials:edit
|
@@ -50,7 +69,7 @@ open_weather_client:
|
|
50
69
|
appid: "<INSERT OPENWEATHERMAP API KEY HERE>"
|
51
70
|
```
|
52
71
|
|
53
|
-
After
|
72
|
+
After configuring the credentials, they can be loaded in the initializer with `#load_from_rails_configuration`.
|
54
73
|
|
55
74
|
```ruby
|
56
75
|
# OpenWeatherClient initializer
|
data/bin/console
CHANGED
@@ -13,11 +13,10 @@ require 'open_weather_client'
|
|
13
13
|
|
14
14
|
OpenWeatherClient.configure do |config|
|
15
15
|
if File.exist?('config.yml')
|
16
|
-
# NOTE(Keune): The
|
17
|
-
# mapping of channel identifiers to webhook URLs. The channel identifier is loaded as a symbol.
|
16
|
+
# NOTE(Keune): The console automatically loads the appid from the config.yml file in the same directory
|
18
17
|
#
|
19
18
|
# Example: APPID: "<INSERT YOUR WEBHOOK URL HERE>"
|
20
|
-
puts 'Load
|
19
|
+
puts 'Load appid specified by file'
|
21
20
|
|
22
21
|
require 'yaml'
|
23
22
|
settings = YAML.load_file 'config.yml'
|
@@ -9,9 +9,9 @@ module OpenWeatherClient
|
|
9
9
|
# When the limit is reached the least recently used entry is evicted.
|
10
10
|
class Memory < OpenWeatherClient::Caching
|
11
11
|
# Memory cache to store a hash of keys and request data
|
12
|
-
|
12
|
+
attr_reader :memory_cache
|
13
13
|
# Key registry of the memory cache. The first entry is the key of the least recently accessed data
|
14
|
-
|
14
|
+
attr_reader :memory_keys
|
15
15
|
|
16
16
|
##
|
17
17
|
# Initialize an empty cache
|
@@ -4,6 +4,8 @@ module OpenWeatherClient
|
|
4
4
|
##
|
5
5
|
# Configuratin of OpenWeatherClient
|
6
6
|
class Configuration
|
7
|
+
# [String] Used api version. One of :v25, :v30. Default :v25
|
8
|
+
attr_accessor :api_version
|
7
9
|
# [String] API key to access OpenWeatherMap
|
8
10
|
attr_accessor :appid
|
9
11
|
# Caching method. One of :none, :memory
|
@@ -24,6 +26,7 @@ module OpenWeatherClient
|
|
24
26
|
##
|
25
27
|
# Initialize a new Configuration with the default settings
|
26
28
|
def initialize
|
29
|
+
@api_version = :v25
|
27
30
|
@caching = OpenWeatherClient::Caching.new
|
28
31
|
@lang = 'de'
|
29
32
|
@max_memory_entries = 10_000
|
@@ -13,6 +13,7 @@ module OpenWeatherClient
|
|
13
13
|
# @param lon[Float] longitude of the requests location
|
14
14
|
# @param time[Time] time of the request
|
15
15
|
#
|
16
|
+
# @raise [APIVersionNotSupportedError] if the configured api version is not supported
|
16
17
|
# @raise [AuthenticationError] if the request is not authorized, e.g in case the API key is not correct
|
17
18
|
# @raise [NotCurrentError] if the requested time is older than 1 hour
|
18
19
|
#
|
@@ -21,7 +22,7 @@ module OpenWeatherClient
|
|
21
22
|
raise OpenWeatherClient::NotCurrentError if time < Time.now - 1 * 60 * 60
|
22
23
|
|
23
24
|
begin
|
24
|
-
response = connection(lat, lon).get(
|
25
|
+
response = connection(lat, lon).get(path)
|
25
26
|
OpenWeatherClient.cache.store(lat: lat, lon: lon, data: response.body, time: time)
|
26
27
|
rescue Faraday::UnauthorizedError
|
27
28
|
raise OpenWeatherClient::AuthenticationError
|
@@ -54,5 +55,16 @@ module OpenWeatherClient
|
|
54
55
|
units: OpenWeatherClient.configuration.units
|
55
56
|
}
|
56
57
|
end
|
58
|
+
|
59
|
+
def self.path
|
60
|
+
case OpenWeatherClient.configuration.api_version
|
61
|
+
when :v25
|
62
|
+
'2.5/weather'
|
63
|
+
when :v30
|
64
|
+
'3.0/onecall'
|
65
|
+
else
|
66
|
+
raise OpenWeatherClient::APIVersionNotSupportedError
|
67
|
+
end
|
68
|
+
end
|
57
69
|
end
|
58
70
|
end
|
data/lib/open_weather_client.rb
CHANGED
@@ -9,10 +9,13 @@ require 'open_weather_client/weather'
|
|
9
9
|
##
|
10
10
|
# Get weather data from OpenWeatherMap
|
11
11
|
module OpenWeatherClient
|
12
|
+
# The configured Open Weather API Version is not supported
|
13
|
+
class APIVersionNotSupportedError < StandardError; end
|
14
|
+
|
12
15
|
# Error during authentication with the OpenWeatherMap servers
|
13
16
|
class AuthenticationError < StandardError; end
|
14
17
|
|
15
|
-
# The
|
18
|
+
# The requested time is not current enough for getting weather data from the OpenWeatherMap server
|
16
19
|
class NotCurrentError < StandardError; end
|
17
20
|
|
18
21
|
class << self
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_weather_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Keune
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|