owmo 2.1.0 → 2.1.2
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/bin/examples +43 -0
- data/lib/owmo/version.rb +1 -1
- data/lib/owmo/weather.rb +11 -7
- data/lib/owmo/weather_response.rb +29 -0
- metadata +4 -3
- data/lib/core_extensions/net/http_response/weather_response.rb +0 -61
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edf9511c6a44ae70b3f4823aef686e12f5b7f13e530526364a1f6dc9c476b96b
|
4
|
+
data.tar.gz: 0f10c539d774fc03f682d1c2977c12518f9e028288d9a7584d220e76dd2e55fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37250290ebe64c1bee4757b68dcfb79058765ab0c25776e8d556730899098a454ea8afa23fc6f7f5ec06a658789024d5460e8259e5117ba318e727e36838f415
|
7
|
+
data.tar.gz: 29d07afd68aecc136cbc7a6e4b60c302bd005fb0bd762721edc183c1a7ea40b4df0170c0b9d6c7f40a0228ed102b03e02ee6bfe020cc8ae8ac60318bbfeeb80d
|
data/bin/examples
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'owmo'
|
6
|
+
|
7
|
+
# Create a file at the root of the repo with your api key and call it api_key.txt
|
8
|
+
api_key = File.read('api_key.txt')
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
examples = [
|
13
|
+
{ description: 'current_box', path: :box, query: { bbox: [12, 32, 15, 37, 10].join(',') } },
|
14
|
+
{ description: 'current_circle', path: :circle, query: { lat: 55.5, lon: 37.5, cnt: 10 } },
|
15
|
+
#{ description: 'current_group', path: :group, query: { id: [4_850_751, 4_887_398, 2_643_743, 4_164_138, 5_368_361].join(',') } },
|
16
|
+
{ description: 'current', path: :current, query: { city_name: 'London,UK' } },
|
17
|
+
{ description: 'forecast16', path: :forecast16, query: { city_name: 'London,UK' } },
|
18
|
+
{ description: 'forecast5', path: :forecast5, query: { city_name: 'London,UK' } },
|
19
|
+
{ description: 'query_all', path: :current, query: { city_name: 'London,UK', mode: 'json', units: 'imperial', lang: 'fr' } },
|
20
|
+
{ description: 'query_geocode', path: :current, query: { city_id: 5_328_041 } },
|
21
|
+
{ description: 'query_geocode', path: :current, query: { city_name: 'Beverly Hills' } },
|
22
|
+
{ description: 'query_geocode', path: :current, query: { id: 5_328_041 } },
|
23
|
+
{ description: 'query_geocode', path: :current, query: { lon: -118.41, lat: 34.09 } },
|
24
|
+
{ description: 'query_geocode', path: :current, query: { q: 'Beverly Hills' } },
|
25
|
+
{ description: 'query_geocode', path: :current, query: { zip: 90_210 } },
|
26
|
+
{ description: 'query_geocode', path: :current, query: { zip_code: 90_210 } },
|
27
|
+
{ description: 'query_mode', path: :current, query: { city_name: 'London,UK' } },
|
28
|
+
{ description: 'query_mode', path: :current, query: { city_name: 'London,UK', mode: :html } },
|
29
|
+
{ description: 'query_mode', path: :current, query: { city_name: 'London,UK', mode: :json } },
|
30
|
+
{ description: 'query_mode', path: :current, query: { city_name: 'London,UK', mode: :xml } },
|
31
|
+
{ description: 'query_units', path: :current, query: { city_name: 'London,UK' } },
|
32
|
+
{ description: 'query_units', path: :current, query: { city_name: 'London,UK', units: :imperial } },
|
33
|
+
{ description: 'query_units', path: :current, query: { city_name: 'London,UK', units: :metric } }
|
34
|
+
]
|
35
|
+
|
36
|
+
puts "OWMO Version #{OWMO::VERSION}"
|
37
|
+
|
38
|
+
OWMO.weather(api_key) do |weather|
|
39
|
+
examples.each do |example|
|
40
|
+
puts '', '/********************', example[:description], '********************/'
|
41
|
+
pp weather.get example[:path], **example[:query]
|
42
|
+
end
|
43
|
+
end
|
data/lib/owmo/version.rb
CHANGED
data/lib/owmo/weather.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'owmo/weather_response'
|
4
|
+
|
5
|
+
require 'json'
|
3
6
|
require 'logger'
|
4
7
|
require 'net/http'
|
5
8
|
|
6
|
-
|
7
9
|
module OWMO
|
8
10
|
# rdoc
|
9
11
|
# A weather class for retrieving current and forecasted weather conditions.
|
@@ -17,8 +19,8 @@ module OWMO
|
|
17
19
|
# rdoc
|
18
20
|
# Weather response error to handle errors received from OpenWeatherMap.orgs API
|
19
21
|
class WeatherResponseError < StandardError
|
20
|
-
def initialize(
|
21
|
-
super("ERROR #{
|
22
|
+
def initialize(weather_response)
|
23
|
+
super("ERROR #{weather_response.code}: #{weather_response.weather}")
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
@@ -82,11 +84,13 @@ module OWMO
|
|
82
84
|
uri = format_uri(OWMO::URL, PATHS[path], query)
|
83
85
|
|
84
86
|
# Get the weather data
|
85
|
-
|
87
|
+
http_response = http_get(uri)
|
88
|
+
|
89
|
+
weather_response = WeatherResponse.new(http_response)
|
86
90
|
|
87
|
-
raise
|
91
|
+
raise(WeatherResponseError, weather_response) if weather_response.error?
|
88
92
|
|
89
|
-
weather
|
93
|
+
weather_response.weather
|
90
94
|
end
|
91
95
|
|
92
96
|
private
|
@@ -121,7 +125,7 @@ module OWMO
|
|
121
125
|
http.request(Net::HTTP::Get.new(uri))
|
122
126
|
end
|
123
127
|
|
124
|
-
|
128
|
+
response
|
125
129
|
end
|
126
130
|
end
|
127
131
|
# :nocov:
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module OWMO
|
6
|
+
|
7
|
+
class WeatherResponse
|
8
|
+
|
9
|
+
attr_reader :weather, :code
|
10
|
+
|
11
|
+
def initialize http_response
|
12
|
+
begin
|
13
|
+
# JSON
|
14
|
+
@weather = JSON.parse(http_response.body)
|
15
|
+
@code = self.weather["cod"].to_i
|
16
|
+
rescue JSON::ParserError, TypeError
|
17
|
+
# Assume XML or HTML
|
18
|
+
@weather = http_response.body
|
19
|
+
@code = self.weather.length > 10 ? 200 : 500
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def error?
|
24
|
+
self.code != 200
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: owmo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robb Randall
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-01-
|
11
|
+
date: 2023-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- Rakefile
|
72
72
|
- _config.yml
|
73
73
|
- bin/console
|
74
|
+
- bin/examples
|
74
75
|
- bin/rspec
|
75
76
|
- bin/setup
|
76
77
|
- examples/current.rb
|
@@ -84,10 +85,10 @@ files:
|
|
84
85
|
- examples/query_mode.rb
|
85
86
|
- examples/query_units.rb
|
86
87
|
- examples/sinatra_example.rb
|
87
|
-
- lib/core_extensions/net/http_response/weather_response.rb
|
88
88
|
- lib/owmo.rb
|
89
89
|
- lib/owmo/version.rb
|
90
90
|
- lib/owmo/weather.rb
|
91
|
+
- lib/owmo/weather_response.rb
|
91
92
|
- owmo.gemspec
|
92
93
|
homepage: https://github.com/robb-randall/owmo
|
93
94
|
licenses:
|
@@ -1,61 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
module CoreExtensions
|
6
|
-
module Net
|
7
|
-
module HTTPResponse
|
8
|
-
module WeatherResponse
|
9
|
-
# rdoc
|
10
|
-
# Returns the weather
|
11
|
-
def weather
|
12
|
-
parse_weather
|
13
|
-
@weather
|
14
|
-
end
|
15
|
-
|
16
|
-
# rdoc
|
17
|
-
# Returns the response code
|
18
|
-
def weather_code
|
19
|
-
parse_weather
|
20
|
-
return (weather['cod'] || '200').to_i if weather.is_a? Hash
|
21
|
-
|
22
|
-
200
|
23
|
-
end
|
24
|
-
|
25
|
-
# rdoc
|
26
|
-
# Returns the response message
|
27
|
-
def weather_message
|
28
|
-
parse_weather
|
29
|
-
return weather['message'] if weather.is_a? Hash
|
30
|
-
|
31
|
-
'None'
|
32
|
-
end
|
33
|
-
|
34
|
-
# rdoc
|
35
|
-
# Returns boolean if the response contains an error or not.
|
36
|
-
def error?
|
37
|
-
weather_code != 200
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
|
42
|
-
# rdoc
|
43
|
-
# Sets the weather variable
|
44
|
-
def weather=(weather)
|
45
|
-
@weather = weather if @weather.nil?
|
46
|
-
end
|
47
|
-
|
48
|
-
# rdoc
|
49
|
-
# Attempts to parse the body to JSON. This is so we don't have to continually
|
50
|
-
# parse the raw JSON.
|
51
|
-
def parse_weather
|
52
|
-
# Try to parse the response and return a hash
|
53
|
-
@weather = JSON.parse(body)
|
54
|
-
rescue StandardError
|
55
|
-
# Return the body string if parsing fails (used for html and xml responses)
|
56
|
-
@weather = body
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|