owmo 2.0.0 → 2.0.1
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/core_extensions/net/http_response/weather_response.rb +20 -10
- data/lib/owmo.rb +1 -1
- data/lib/owmo/version.rb +1 -1
- data/lib/owmo/weather.rb +30 -53
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4256e7133f1d73d8d3c4c4c542547eb3be368eee
|
4
|
+
data.tar.gz: 8184cb85272239ffacc56931eec0c91f8698a135
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6be96efc7ca3e8d335e8161c29478b02d839741b0ede3c350551368a13bed0588272c7cbfcf141fe1623ebb081eb911bf29e129571a2f35dc2df7100044d17d
|
7
|
+
data.tar.gz: 1907e7585125a51c66a96abfdc173318e7d5e0878c6e3b6a1afd8dc73000a4c294152abe937edd13e50cb2777d9e189e02983db5ae8188f0d034e7dd61045641
|
@@ -18,7 +18,7 @@ Returns the response code
|
|
18
18
|
=end
|
19
19
|
def weather_code
|
20
20
|
parse_weather
|
21
|
-
return (
|
21
|
+
return (weather['cod'] || "200").to_i if weather.is_a? Hash
|
22
22
|
200
|
23
23
|
end
|
24
24
|
|
@@ -27,7 +27,7 @@ Returns the response message
|
|
27
27
|
=end
|
28
28
|
def weather_message
|
29
29
|
parse_weather
|
30
|
-
return
|
30
|
+
return weather['message'] if weather.is_a? Hash
|
31
31
|
"None"
|
32
32
|
end
|
33
33
|
|
@@ -38,18 +38,28 @@ Returns boolean if the response contains an error or not.
|
|
38
38
|
weather_code != 200
|
39
39
|
end
|
40
40
|
|
41
|
+
private
|
42
|
+
|
43
|
+
=begin rdoc
|
44
|
+
Sets the weather variable
|
45
|
+
=end
|
46
|
+
def weather=(weather)
|
47
|
+
@weather = weather if @weather.nil?
|
48
|
+
end
|
49
|
+
|
41
50
|
=begin rdoc
|
42
51
|
Attempts to parse the body to JSON. This is so we don't have to continually
|
43
52
|
parse the raw JSON.
|
44
53
|
=end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
54
|
+
def parse_weather
|
55
|
+
begin
|
56
|
+
# Try to parse the response and return a hash
|
57
|
+
@weather = JSON.parse(self.body)
|
58
|
+
rescue
|
59
|
+
# Return the body string if parsing fails (used for html and xml responses)
|
60
|
+
@weather = self.body
|
61
|
+
end
|
62
|
+
end
|
53
63
|
|
54
64
|
end
|
55
65
|
end
|
data/lib/owmo.rb
CHANGED
data/lib/owmo/version.rb
CHANGED
data/lib/owmo/weather.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
+
require 'logger'
|
1
2
|
require 'net/http'
|
2
|
-
|
3
|
+
|
3
4
|
require 'core_extensions/net/http_response/weather_response'
|
4
5
|
|
5
6
|
|
@@ -57,7 +58,8 @@ Geocode aliases
|
|
57
58
|
city_id: :id,
|
58
59
|
zip_code: :zip,
|
59
60
|
latitude: :lat,
|
60
|
-
longitude: :lon
|
61
|
+
longitude: :lon,
|
62
|
+
box: :bbox
|
61
63
|
}
|
62
64
|
|
63
65
|
=begin rdoc
|
@@ -67,18 +69,10 @@ Either yeild the class, or instanciate it.
|
|
67
69
|
* +**kwargs+ - Any additional paramters
|
68
70
|
=end
|
69
71
|
def initialize(api_key, **kwargs)
|
70
|
-
@debug = kwargs[:debug] || FALSE
|
71
|
-
log "Debug= #{@debug}"
|
72
|
-
|
73
72
|
@api_key = api_key
|
74
|
-
log "Api Key= #{@api_key}"
|
75
|
-
|
76
|
-
log "Args= #{kwargs}"
|
77
73
|
|
78
74
|
if block_given?
|
79
|
-
log "Yielding"
|
80
75
|
yield self
|
81
|
-
log "Yield complete."
|
82
76
|
end
|
83
77
|
end
|
84
78
|
|
@@ -94,77 +88,60 @@ A weather class for retrieving current and forecasted weather conditions.
|
|
94
88
|
=end
|
95
89
|
public
|
96
90
|
def get(path, **query)
|
97
|
-
log "Starting request= #{path} -> #{query}"
|
98
|
-
start = Time.now
|
99
91
|
|
100
92
|
# Format Geocode info
|
101
93
|
query = alias_geocodes(query)
|
102
94
|
|
103
95
|
# Add the api key
|
104
|
-
query[:APPID] =
|
96
|
+
query[:APPID] = api_key
|
105
97
|
|
106
98
|
# Create the uri
|
107
99
|
uri = format_uri(OWMO::URL, Paths[path], query)
|
108
100
|
|
109
101
|
# Get the weather data
|
110
|
-
|
102
|
+
GET(uri)
|
103
|
+
end
|
111
104
|
|
112
|
-
|
113
|
-
log "Request completed in #{elapsed_sec} seconds."
|
105
|
+
private
|
114
106
|
|
115
|
-
|
116
|
-
|
107
|
+
=begin rdoc
|
108
|
+
Retruns the geocode keys from specified query.
|
109
|
+
=end
|
110
|
+
def query_geocode_keys(**query)
|
111
|
+
query.keys & Geocode_Aliases.keys
|
112
|
+
end
|
117
113
|
|
118
114
|
=begin rdoc
|
119
115
|
Aliases some geocode parameters to the correct ones, for example :city_name is
|
120
116
|
easier to read than :q
|
121
117
|
=end
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
query[Geocode_Aliases[key]] = query.delete(key)
|
118
|
+
def alias_geocodes(**query)
|
119
|
+
query_geocode_keys(query).each do |key|
|
120
|
+
query[Geocode_Aliases[key]] = query.delete(key)
|
121
|
+
end
|
122
|
+
query
|
128
123
|
end
|
129
124
|
|
130
|
-
log "Query after= #{query}"
|
131
|
-
query
|
132
|
-
end
|
133
|
-
|
134
125
|
=begin rdoc
|
135
126
|
Formats the url with the given url, path, and query
|
136
127
|
=end
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
log "URI= #{uri}"
|
141
|
-
uri
|
142
|
-
end
|
128
|
+
def format_uri(url, path, query)
|
129
|
+
URI "#{url}/#{path}?#{URI.encode_www_form(query).gsub('%2C', ',')}"
|
130
|
+
end
|
143
131
|
|
144
132
|
=begin rdoc
|
145
133
|
Sends the GET request to OpenWeatherMap.org
|
146
134
|
=end
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
http.request(Net::HTTP::Get.new(uri))
|
152
|
-
end
|
153
|
-
log "Request returned= #{response.weather_code}: #{response.weather_message}".gsub(/: $/, '')
|
154
|
-
|
155
|
-
# Check the response
|
156
|
-
raise WeatherResponseError.new(response) if response.has_error?
|
135
|
+
def GET(uri)
|
136
|
+
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
137
|
+
http.request(Net::HTTP::Get.new(uri))
|
138
|
+
end
|
157
139
|
|
158
|
-
|
159
|
-
|
140
|
+
# Check the response
|
141
|
+
raise WeatherResponseError.new(response) if response.has_error?
|
160
142
|
|
161
|
-
|
162
|
-
|
163
|
-
=end
|
164
|
-
private
|
165
|
-
def log(msg)
|
166
|
-
puts "#{DateTime.now} :~> #{msg}" if @debug
|
167
|
-
end
|
143
|
+
response.weather
|
144
|
+
end
|
168
145
|
|
169
146
|
end
|
170
147
|
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.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robb
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|