owmo 2.1.1 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/examples +43 -0
- data/lib/owmo/version.rb +1 -1
- data/lib/owmo/weather.rb +8 -6
- data/lib/owmo/weather_response.rb +29 -0
- metadata +4 -3
- data/lib/core_extensions/net/http_response/weather_response.rb +0 -65
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,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'owmo/weather_response'
|
4
4
|
|
5
5
|
require 'json'
|
6
6
|
require 'logger'
|
@@ -19,8 +19,8 @@ module OWMO
|
|
19
19
|
# rdoc
|
20
20
|
# Weather response error to handle errors received from OpenWeatherMap.orgs API
|
21
21
|
class WeatherResponseError < StandardError
|
22
|
-
def initialize(
|
23
|
-
super("ERROR #{
|
22
|
+
def initialize(weather_response)
|
23
|
+
super("ERROR #{weather_response.code}: #{weather_response.weather}")
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -84,11 +84,13 @@ module OWMO
|
|
84
84
|
uri = format_uri(OWMO::URL, PATHS[path], query)
|
85
85
|
|
86
86
|
# Get the weather data
|
87
|
-
|
87
|
+
http_response = http_get(uri)
|
88
88
|
|
89
|
-
|
89
|
+
weather_response = WeatherResponse.new(http_response)
|
90
90
|
|
91
|
-
|
91
|
+
raise(WeatherResponseError, weather_response) if weather_response.error?
|
92
|
+
|
93
|
+
weather_response.weather
|
92
94
|
end
|
93
95
|
|
94
96
|
private
|
@@ -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,65 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'json'
|
4
|
-
require 'net/http'
|
5
|
-
|
6
|
-
module CoreExtensions
|
7
|
-
module Net
|
8
|
-
module HTTPResponse
|
9
|
-
module WeatherResponse
|
10
|
-
# rdoc
|
11
|
-
# Returns the weather
|
12
|
-
def weather
|
13
|
-
parse_weather
|
14
|
-
@weather
|
15
|
-
end
|
16
|
-
|
17
|
-
# rdoc
|
18
|
-
# Returns the response code
|
19
|
-
def weather_code
|
20
|
-
parse_weather
|
21
|
-
|
22
|
-
return (weather['cod'] || '500').to_i if weather.is_a? Hash
|
23
|
-
|
24
|
-
200
|
25
|
-
end
|
26
|
-
|
27
|
-
# rdoc
|
28
|
-
# Returns the response message
|
29
|
-
def weather_message
|
30
|
-
parse_weather
|
31
|
-
return weather['message'] if weather.is_a? Hash
|
32
|
-
|
33
|
-
'None'
|
34
|
-
end
|
35
|
-
|
36
|
-
# rdoc
|
37
|
-
# Returns boolean if the response contains an error or not.
|
38
|
-
def error?
|
39
|
-
weather_code != 200
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
# rdoc
|
45
|
-
# Sets the weather variable
|
46
|
-
def weather=(weather)
|
47
|
-
@weather = weather if @weather.nil?
|
48
|
-
end
|
49
|
-
|
50
|
-
# rdoc
|
51
|
-
# Attempts to parse the body to JSON. This is so we don't have to continually
|
52
|
-
# parse the raw JSON.
|
53
|
-
def parse_weather
|
54
|
-
# Try to parse the response and return a hash
|
55
|
-
@weather = JSON.parse(body)
|
56
|
-
rescue StandardError
|
57
|
-
# Return the body string if parsing fails (used for html and xml responses)
|
58
|
-
@weather = body
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
Net::HTTPResponse.include CoreExtensions::Net::HTTPResponse::WeatherResponse
|