openweathermap 0.2.2 → 0.2.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 +4 -4
- data/lib/openweathermap.rb +92 -2
- data/lib/openweathermap/api.rb +30 -16
- data/lib/openweathermap/classes.rb +60 -4
- data/lib/openweathermap/current-weather.rb +18 -2
- data/lib/openweathermap/forecast.rb +17 -1
- metadata +1 -3
- data/lib/openweathermap/data/constants.rb +0 -31
- data/lib/openweathermap/data/exceptions.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24880aa7d52ef082b5c90c142860c91bf01b048778d323120d0d5d025b93b635
|
4
|
+
data.tar.gz: 4f8898e0f90e45c4559dc173ec7ac9be559fb2f150ecfb974b080764ae4b28f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61c8986000ca55f3da322a837f483e30360e06ce87f0286f7d2755eb91a3b5ac9ae1eefa0dac7fb212700450dfb3068a06bd2b62f1799ccc4c7541ee38484b46
|
7
|
+
data.tar.gz: 8b5d1e5a0c06d59874e604d192c7b234e279bcad94a3c54dc85bedc2c04fb31ed43eac9621350b014179a894e5921c23b8ba5a215b287525d3fa040a7432d8fc
|
data/lib/openweathermap.rb
CHANGED
@@ -1,9 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'net/http'
|
2
4
|
require 'json'
|
3
5
|
|
4
|
-
require 'openweathermap/data/constants'
|
5
|
-
require 'openweathermap/data/exceptions'
|
6
6
|
require 'openweathermap/classes'
|
7
7
|
require 'openweathermap/current-weather'
|
8
8
|
require 'openweathermap/forecast'
|
9
9
|
require 'openweathermap/api'
|
10
|
+
|
11
|
+
module OpenWeatherMap
|
12
|
+
|
13
|
+
##
|
14
|
+
# All the constants needed for the library
|
15
|
+
|
16
|
+
module Constants
|
17
|
+
|
18
|
+
##
|
19
|
+
# URL of the OpenWeatherMap API
|
20
|
+
|
21
|
+
API_URL = 'https://api.openweathermap.org'
|
22
|
+
|
23
|
+
##
|
24
|
+
# Accepted types of unit
|
25
|
+
|
26
|
+
UNITS = %w(default metric imperial)
|
27
|
+
|
28
|
+
##
|
29
|
+
# Accepted locales
|
30
|
+
|
31
|
+
LANGS = %w(ar bg ca cz de el fa fi fr gl hr hu it ja kr la lt mk nl pl pt ro ru se sk sl es tr ua vi zh_cn zh_tw en)
|
32
|
+
|
33
|
+
##
|
34
|
+
# The different URLs
|
35
|
+
|
36
|
+
URLS = {
|
37
|
+
current: '/data/2.5/weather',
|
38
|
+
forecast: '/data/2.5/forecast'
|
39
|
+
}
|
40
|
+
|
41
|
+
##
|
42
|
+
# All condition codes associated with emojis
|
43
|
+
|
44
|
+
CONDITION_CODE = {
|
45
|
+
'01d' => '☀',
|
46
|
+
'02d' => '⛅',
|
47
|
+
'03d' => '☁',
|
48
|
+
'04d' => '☁☁',
|
49
|
+
'09d' => '🌧',
|
50
|
+
'10d' => '🌦',
|
51
|
+
'11d' => '🌩',
|
52
|
+
'13d' => '🌨',
|
53
|
+
'50d' => '🌫',
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Base exception for the OpenWeatherMap library
|
59
|
+
|
60
|
+
class Exception < StandardError
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Exceptions that can be thrown by the library
|
65
|
+
|
66
|
+
module Exceptions
|
67
|
+
|
68
|
+
##
|
69
|
+
# Exception to handle unknown lang
|
70
|
+
|
71
|
+
class UnknownLang < OpenWeatherMap::Exception
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# Exception to handle unknown units
|
76
|
+
|
77
|
+
class UnknownUnits < OpenWeatherMap::Exception
|
78
|
+
end
|
79
|
+
|
80
|
+
##
|
81
|
+
# Exception to handle unknown location
|
82
|
+
|
83
|
+
class UnknownLocation < OpenWeatherMap::Exception
|
84
|
+
end
|
85
|
+
|
86
|
+
##
|
87
|
+
# Exception to tell that the API key isn't authorized
|
88
|
+
|
89
|
+
class Unauthorized < OpenWeatherMap::Exception
|
90
|
+
end
|
91
|
+
|
92
|
+
##
|
93
|
+
# Exception to handle data error
|
94
|
+
|
95
|
+
class DataError < OpenWeatherMap::Exception
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
data/lib/openweathermap/api.rb
CHANGED
@@ -1,12 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module OpenWeatherMap
|
4
|
+
##
|
2
5
|
# The main API class.
|
6
|
+
|
3
7
|
class API
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
##
|
9
|
+
# The default lang to use across the program
|
10
|
+
# @return [String]
|
11
|
+
|
12
|
+
attr_reader :lang
|
9
13
|
|
14
|
+
##
|
15
|
+
# The default unit system to use across the program
|
16
|
+
# @return [String]
|
17
|
+
|
18
|
+
attr_reader :units
|
19
|
+
|
20
|
+
##
|
10
21
|
# Initialize the API object
|
11
22
|
#
|
12
23
|
# @param api_key [String] your OpenWeatherMap's API key
|
@@ -20,29 +31,33 @@ module OpenWeatherMap
|
|
20
31
|
# - imperial (temperatures in Fahrenheit)
|
21
32
|
# @raise [OpenWeatherMap::Exceptions::UnknownLang] if the selected lang is not unknown
|
22
33
|
# @raise [OpenWeatherMap::Exceptions::UnknownUnits] if the selected units is not unknown
|
23
|
-
|
34
|
+
|
35
|
+
def initialize(api_key, lang = 'en', units = 'default')
|
24
36
|
@api_key = api_key
|
25
37
|
|
26
|
-
raise OpenWeatherMap::Exceptions::UnknownLang, "
|
38
|
+
raise OpenWeatherMap::Exceptions::UnknownLang, "unknown lang #{lang}" unless OpenWeatherMap::Constants::LANGS.include? lang
|
27
39
|
@lang = lang
|
28
40
|
|
29
|
-
raise OpenWeatherMap::Exceptions::UnknownUnits, "
|
41
|
+
raise OpenWeatherMap::Exceptions::UnknownUnits, "unknown units #{units}" unless OpenWeatherMap::Constants::UNITS.include? units
|
30
42
|
@units = units
|
31
43
|
end
|
32
44
|
|
45
|
+
##
|
33
46
|
# Get current weather at a specific location.
|
34
47
|
#
|
35
48
|
# @param location [String, Integer, Array] the location
|
36
49
|
# Can be one of this type :
|
37
50
|
# - String : search by city name
|
38
|
-
# - Integer : search by city ID (refer to bulk.openweathermap.org/sample/city.list.json.gz)
|
51
|
+
# - Integer : search by city ID (refer to http://bulk.openweathermap.org/sample/city.list.json.gz)
|
39
52
|
# - Array : search by coordinates (format : [lon, lat])
|
40
53
|
# @return [OpenWeatherMap::CurrentWeather] requested data
|
54
|
+
|
41
55
|
def current(location)
|
42
56
|
data = make_request(OpenWeatherMap::Constants::URLS[:current], location)
|
43
57
|
OpenWeatherMap::CurrentWeather.new(data)
|
44
58
|
end
|
45
59
|
|
60
|
+
##
|
46
61
|
# Get weather forecast for a specific location.
|
47
62
|
#
|
48
63
|
# @param location [String, Integer, Array] the location
|
@@ -51,6 +66,7 @@ module OpenWeatherMap
|
|
51
66
|
# - Integer : search by city ID (refer to bulk.openweathermap.org/sample/city.list.json.gz)
|
52
67
|
# - Array : search by coordinates (format : [lon, lat])
|
53
68
|
# @return [OpenWeatherMap::Forecast] requested data
|
69
|
+
|
54
70
|
def forecast(location)
|
55
71
|
data = make_request(OpenWeatherMap::Constants::URLS[:forecast], location)
|
56
72
|
OpenWeatherMap::Forecast.new(data)
|
@@ -58,11 +74,13 @@ module OpenWeatherMap
|
|
58
74
|
|
59
75
|
private
|
60
76
|
|
77
|
+
##
|
61
78
|
# Make a request to the OpenWeatherMap API.
|
62
79
|
#
|
63
80
|
# @param url [String] The endpoint to reach
|
64
81
|
# @param options [Hash] mixed options
|
65
82
|
# @return [String] request's body
|
83
|
+
|
66
84
|
def make_request(url, location)
|
67
85
|
options = {}
|
68
86
|
options[:q] = location if location.is_a? String
|
@@ -79,16 +97,12 @@ module OpenWeatherMap
|
|
79
97
|
}
|
80
98
|
params.merge! options
|
81
99
|
|
82
|
-
url = "#{OpenWeatherMap::Constants::API_URL}/#{url}
|
83
|
-
|
84
|
-
params.each do |key, value|
|
85
|
-
url += "#{key}=#{value}&"
|
86
|
-
end
|
100
|
+
url = "#{OpenWeatherMap::Constants::API_URL}/#{url}?#{URI.encode_www_form(params)}"
|
87
101
|
|
88
102
|
response = Net::HTTP.get_response(URI(url))
|
89
103
|
case response.code.to_i
|
90
|
-
when 401 then raise OpenWeatherMap::Exceptions::Unauthorized, "
|
91
|
-
when 404 then raise OpenWeatherMap::Exceptions::UnknownLocation, "
|
104
|
+
when 401 then raise OpenWeatherMap::Exceptions::Unauthorized, "unauthorized key. API message : #{response.message}"
|
105
|
+
when 404 then raise OpenWeatherMap::Exceptions::UnknownLocation, "unknown location. API message : #{location}"
|
92
106
|
else response.body
|
93
107
|
end
|
94
108
|
end
|
@@ -1,21 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module OpenWeatherMap
|
2
|
-
|
4
|
+
|
5
|
+
##
|
6
|
+
# Class representing a city
|
7
|
+
|
3
8
|
class City
|
9
|
+
|
10
|
+
##
|
4
11
|
# @return [String] City's name
|
12
|
+
|
5
13
|
attr_reader :name
|
6
14
|
|
15
|
+
##
|
7
16
|
# @return [Coordinates] City's coordinates
|
17
|
+
|
8
18
|
attr_reader :coordinates
|
9
19
|
|
20
|
+
##
|
10
21
|
# @return [String] Country in which the city is
|
22
|
+
|
11
23
|
attr_reader :country
|
12
24
|
|
25
|
+
##
|
13
26
|
# Create a new City object
|
14
27
|
#
|
15
28
|
# @param name [String] City's name
|
16
29
|
# @param lon [Float] Longitude of the city
|
17
30
|
# @param lat [Float] Latitude of the city
|
18
31
|
# @param country [String] Country in which the city is
|
32
|
+
|
19
33
|
def initialize(name, lon, lat, country)
|
20
34
|
@name = name
|
21
35
|
@coordinates = OpenWeatherMap::Coordinates.new(lon, lat)
|
@@ -23,85 +37,127 @@ module OpenWeatherMap
|
|
23
37
|
end
|
24
38
|
end
|
25
39
|
|
40
|
+
##
|
26
41
|
# Represents a location's coordinates
|
42
|
+
|
27
43
|
class Coordinates
|
44
|
+
|
45
|
+
##
|
28
46
|
# @return [Float] Longitude of the location
|
47
|
+
|
29
48
|
attr_reader :lon
|
30
|
-
|
49
|
+
|
50
|
+
##
|
31
51
|
# @return [Float] Latitude of the location
|
52
|
+
|
32
53
|
attr_reader :lat
|
33
54
|
|
55
|
+
##
|
34
56
|
# Create a new Coordinates object
|
35
57
|
#
|
36
58
|
# @param lon [Float] Longitude of the location
|
37
59
|
# @param lat [Float] Latitude of the location
|
60
|
+
|
38
61
|
def initialize(lon, lat)
|
39
62
|
@lon = lon
|
40
63
|
@lat = lat
|
41
64
|
end
|
42
65
|
end
|
43
66
|
|
67
|
+
##
|
44
68
|
# Represents the weather conditions
|
69
|
+
|
45
70
|
class WeatherConditions
|
71
|
+
|
72
|
+
##
|
46
73
|
# @return [Time] time of the condition
|
74
|
+
|
47
75
|
attr_reader :time
|
48
76
|
|
49
|
-
|
77
|
+
##
|
78
|
+
# @return [String] Main weather conditions at the moment
|
79
|
+
|
50
80
|
attr_reader :main
|
51
81
|
|
82
|
+
##
|
52
83
|
# @return [String] Details of weather conditions
|
84
|
+
|
53
85
|
attr_reader :description
|
54
86
|
|
87
|
+
##
|
55
88
|
# @return [String] URL to conditions icon illustration
|
89
|
+
|
56
90
|
attr_reader :icon
|
57
91
|
|
92
|
+
##
|
58
93
|
# @return [String] Conditions illustrated by an emoji
|
94
|
+
|
59
95
|
attr_reader :emoji
|
60
96
|
|
97
|
+
##
|
61
98
|
# @return [Float] Temperature
|
99
|
+
|
62
100
|
attr_reader :temperature
|
63
101
|
|
102
|
+
##
|
64
103
|
# @return [Float] Minimum temperature at the moment (for large areas)
|
104
|
+
|
65
105
|
attr_reader :temp_min
|
66
106
|
|
107
|
+
##
|
67
108
|
# @return [Float] Maximum temperature at the moment (for large areas)
|
109
|
+
|
68
110
|
attr_reader :temp_max
|
69
111
|
|
112
|
+
##
|
70
113
|
# @return [Float] Atmospheric pressure in hPa
|
114
|
+
|
71
115
|
attr_reader :pressure
|
72
116
|
|
117
|
+
##
|
73
118
|
# @return [Float] Humidity percentage
|
119
|
+
|
74
120
|
attr_reader :humidity
|
75
121
|
|
122
|
+
##
|
76
123
|
# @return [Hash] Wind data. Keys : (symbols)
|
77
124
|
# - speed : Wind speed (m/s or miles/hour)
|
78
125
|
# - direction : Wind direction (meteorological degrees)
|
126
|
+
|
79
127
|
attr_reader :wind
|
80
128
|
|
129
|
+
##
|
81
130
|
# @return [Float] Clouds percentage
|
131
|
+
|
82
132
|
attr_reader :clouds
|
83
133
|
|
134
|
+
##
|
84
135
|
# @return [Hash, nil] Rain volume. Keys : (symbols)
|
85
136
|
# - one_hour : Rain volume for the last 1 hour (mm)
|
86
137
|
# - three_hours : Rain volume for the last 3 hours (mm)
|
87
138
|
# Can be nil if there is no rain
|
139
|
+
|
88
140
|
attr_reader :rain
|
89
141
|
|
142
|
+
##
|
90
143
|
# @return [Hash, nil] Snow volume. Keys : (symbols)
|
91
144
|
# - one_hour : Snow volume for the last 1 hour (mm)
|
92
145
|
# - three_hours : Snow volume for the last 3 hours (mm)
|
93
146
|
# Can be nil if there is no snow
|
147
|
+
|
94
148
|
attr_reader :snow
|
95
149
|
|
150
|
+
##
|
96
151
|
# Create a new WeatherConditions object.
|
97
152
|
#
|
98
153
|
# @param data [Hash] all the received data
|
154
|
+
|
99
155
|
def initialize(data)
|
100
156
|
@time = Time.at(data['dt'])
|
101
157
|
@main = data['weather'][0]['main']
|
102
158
|
@description = data['weather'][0]['description']
|
103
159
|
@icon = "https://openweathermap.org/img/w/#{data['weather'][0]['icon']}.png"
|
104
|
-
@emoji = OpenWeatherMap::Constants::CONDITION_CODE[data['weather'][0]['icon'].
|
160
|
+
@emoji = OpenWeatherMap::Constants::CONDITION_CODE[data['weather'][0]['icon'].tr('n', 'd')]
|
105
161
|
@temperature = data['main']['temp']
|
106
162
|
@temp_min = data['main']['temp_min'].to_f
|
107
163
|
@temp_max = data['main']['temp_max'].to_f
|
@@ -1,17 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module OpenWeatherMap
|
4
|
+
|
5
|
+
##
|
2
6
|
# Represents the current weather at a location
|
7
|
+
|
3
8
|
class CurrentWeather
|
9
|
+
|
10
|
+
##
|
4
11
|
# @return [OpenWeatherMap::WeatherConditions] Conditions at the moment
|
12
|
+
|
5
13
|
attr_reader :weather_conditions
|
6
14
|
|
15
|
+
##
|
7
16
|
# @return [OpenWeatherMap::City] Requested city's data
|
17
|
+
|
8
18
|
attr_reader :city
|
9
|
-
|
19
|
+
|
20
|
+
##
|
10
21
|
# Create a new CurrentWeather object
|
11
22
|
#
|
12
23
|
# @param data [Hash] mixed data from the request
|
24
|
+
|
13
25
|
def initialize(data)
|
14
|
-
|
26
|
+
begin
|
27
|
+
data = JSON.parse(data)
|
28
|
+
rescue JSON::JSONError => e
|
29
|
+
raise OpenWeatherMap::Exceptions::DataError, "error while parsing data : #{e}"
|
30
|
+
end
|
15
31
|
@city = OpenWeatherMap::City.new(data['name'], data['coord']['lon'], data['coord']['lat'], data['sys']['country'])
|
16
32
|
@weather_conditions = OpenWeatherMap::WeatherConditions.new(data)
|
17
33
|
end
|
@@ -1,17 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module OpenWeatherMap
|
4
|
+
|
5
|
+
##
|
2
6
|
# Represents the forecast for a specific location
|
7
|
+
|
3
8
|
class Forecast
|
9
|
+
|
10
|
+
##
|
4
11
|
# @return [OpenWeatherMap::City] Requested city's data
|
12
|
+
|
5
13
|
attr_reader :city
|
6
14
|
|
15
|
+
##
|
7
16
|
# @return [Array<OpenWeatherMap::WeatherConditions>] Forecast for many days and hours
|
17
|
+
|
8
18
|
attr_reader :forecast
|
9
19
|
|
20
|
+
##
|
10
21
|
# Create a new Forecast object
|
11
22
|
#
|
12
23
|
# @param data [Hash] mixed data from the request
|
24
|
+
|
13
25
|
def initialize(data)
|
14
|
-
|
26
|
+
begin
|
27
|
+
data = JSON.parse(data)
|
28
|
+
rescue JSON::JSONError => e
|
29
|
+
raise OpenWeatherMap::Exceptions::DataError, "error while parsing data : #{e}"
|
30
|
+
end
|
15
31
|
@city = OpenWeatherMap::City.new(data['city']['name'], data['city']['coord']['lon'], data['city']['coord']['lat'], data['city']['country'])
|
16
32
|
@forecast = []
|
17
33
|
data['list'].each do |element|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openweathermap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Exybore
|
@@ -20,8 +20,6 @@ files:
|
|
20
20
|
- lib/openweathermap/api.rb
|
21
21
|
- lib/openweathermap/classes.rb
|
22
22
|
- lib/openweathermap/current-weather.rb
|
23
|
-
- lib/openweathermap/data/constants.rb
|
24
|
-
- lib/openweathermap/data/exceptions.rb
|
25
23
|
- lib/openweathermap/forecast.rb
|
26
24
|
homepage: https://github.com/becauseofprog/openweathermap-ruby
|
27
25
|
licenses:
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module OpenWeatherMap
|
2
|
-
module Constants
|
3
|
-
# URL of the OpenWeatherMap API
|
4
|
-
API_URL = 'https://api.openweathermap.org'
|
5
|
-
|
6
|
-
# Accepted types of unit
|
7
|
-
UNITS = [nil, 'metric', 'imperial']
|
8
|
-
|
9
|
-
# Accepted locales
|
10
|
-
LANGS = %w(ar bg ca cz de el fa fi fr gl hr hu it ja kr la lt mk nl pl pt ro ru se sk sl es tr ua vi zh_cn zh_tw en)
|
11
|
-
|
12
|
-
# The different URLs
|
13
|
-
URLS = {
|
14
|
-
current: '/data/2.5/weather',
|
15
|
-
forecast: '/data/2.5/forecast'
|
16
|
-
}
|
17
|
-
|
18
|
-
# All condition codes associated with emojis
|
19
|
-
CONDITION_CODE = {
|
20
|
-
'01d' => '☀',
|
21
|
-
'02d' => '⛅',
|
22
|
-
'03d' => '☁',
|
23
|
-
'04d' => '☁☁',
|
24
|
-
'09d' => '🌧',
|
25
|
-
'10d' => '🌦',
|
26
|
-
'11d' => '🌩',
|
27
|
-
'13d' => '🌨',
|
28
|
-
'50d' => '🌫',
|
29
|
-
}
|
30
|
-
end
|
31
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module OpenWeatherMap
|
2
|
-
module Exceptions
|
3
|
-
# Exception to handle unknown lang
|
4
|
-
class UnknownLang < StandardError
|
5
|
-
end
|
6
|
-
|
7
|
-
# Exception to handle unknown units
|
8
|
-
class UnknownUnits < StandardError
|
9
|
-
end
|
10
|
-
|
11
|
-
# Exception to handle unknown location
|
12
|
-
class UnknownLocation < StandardError
|
13
|
-
end
|
14
|
-
|
15
|
-
# Exception to tell that the API key isn't authorized
|
16
|
-
class Unauthorized < StandardError
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|