owmo 2.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 125a82a8915262a016d7b78461f403abf325d76d177d97700e1be023ad3e22eb
4
- data.tar.gz: 6d47d5e59c2d3b31322a478dc252a33cfdbc88139d86d641f29cc67861d9c88c
3
+ metadata.gz: edf9511c6a44ae70b3f4823aef686e12f5b7f13e530526364a1f6dc9c476b96b
4
+ data.tar.gz: 0f10c539d774fc03f682d1c2977c12518f9e028288d9a7584d220e76dd2e55fe
5
5
  SHA512:
6
- metadata.gz: a574a3a6a6b8f81bb77da1a4e8f10809fe8f43012b214061c269c7c91c22768543357e280d18cc7ef72e0f2cce04f9af35ae3c70c3370f5364257cae4fdd79eb
7
- data.tar.gz: 9a93faefecb90ae2147ef9d20b2fe1f280ba582fba580e8588d7bf6084ab2e9b400e9b0a2e162c0dfc5efac105860fa8b15e28850d26c4215c7d96c80db4640d
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
@@ -3,5 +3,5 @@
3
3
  module OWMO
4
4
  # rdoc
5
5
  # Gem Version
6
- VERSION = '2.1.1'
6
+ VERSION = '2.1.2'
7
7
  end
data/lib/owmo/weather.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'core_extensions/net/http_response/weather_response'
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(response)
23
- super("ERROR #{response.weather_code}: #{response.message}")
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
- response = http_get(uri)
87
+ http_response = http_get(uri)
88
88
 
89
- raise(WeatherResponseError, response) if response.error?
89
+ weather_response = WeatherResponse.new(http_response)
90
90
 
91
- response.weather
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.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-10 00:00:00.000000000 Z
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