owmo 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84b6f969b10f394d6c8f9c0fda1ac2dbd6929811
4
- data.tar.gz: 0ebeca0df38b5d6104890759bdf756feccf3fd68
3
+ metadata.gz: 4256e7133f1d73d8d3c4c4c542547eb3be368eee
4
+ data.tar.gz: 8184cb85272239ffacc56931eec0c91f8698a135
5
5
  SHA512:
6
- metadata.gz: 10dcefd739e026f47f6e3b4adfbef8101fbfee1ecf1477b483a5d8b418eb3b04aa4c0591687ccc794446dc100514c7f98d1a8cda64faa57533ac99fbc1a1cc1d
7
- data.tar.gz: 87a157f21b913e319187b28569e058cdd85401217af165ed00e031528f1d53c601c80a2c30dc6e8b8615d80235779623ba690608f5f0f0f274515800f6bb4cee
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 (@weather['cod'] || "200").to_i if @weather.is_a? Hash
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 @weather['message'] if @weather.is_a? Hash
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
- private
46
- def parse_weather
47
- begin
48
- @weather = JSON.parse(self.body)
49
- rescue
50
- @weather = self.body
51
- end if @weather.nil?
52
- end
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
@@ -34,7 +34,7 @@ Yield a weather object for querying weather data
34
34
  if block_given?
35
35
  yield weather
36
36
  else
37
- return weather
37
+ weather
38
38
  end
39
39
  end
40
40
  end
@@ -3,6 +3,6 @@ module OWMO
3
3
  =begin rdoc
4
4
  Gem Version
5
5
  =end
6
- VERSION = "2.0.0"
6
+ VERSION = "2.0.1"
7
7
 
8
8
  end
@@ -1,5 +1,6 @@
1
+ require 'logger'
1
2
  require 'net/http'
2
- require 'time'
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] = @api_key
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
- weather = GET(uri)
102
+ GET(uri)
103
+ end
111
104
 
112
- elapsed_sec = (Time.now - start).round 5
113
- log "Request completed in #{elapsed_sec} seconds."
105
+ private
114
106
 
115
- weather
116
- end
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
- private
123
- def alias_geocodes(**query)
124
- log "Query before= #{query}"
125
-
126
- (query.keys & Geocode_Aliases.keys).each do |key|
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
- private
138
- def format_uri(url, path, query)
139
- uri = URI "#{url}/#{path}?#{URI.encode_www_form(query).gsub('%2C', ',')}"
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
- private
148
- def GET(uri)
149
- log "Starting GET request"
150
- response = Net::HTTP.start(uri.hostname, uri.port) do |http|
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
- response.weather
159
- end
140
+ # Check the response
141
+ raise WeatherResponseError.new(response) if response.has_error?
160
142
 
161
- =begin rdoc
162
- Simple log method for debugging purposes
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.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-06-23 00:00:00.000000000 Z
11
+ date: 2017-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler