open-weather-api 0.0.5 → 0.0.6

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
  SHA1:
3
- metadata.gz: dcd2a1c29774ef26c4a0b443e44f2649485a8f51
4
- data.tar.gz: f8b4a8fc6668ee26393366a6df94bd4dd339b1d8
3
+ metadata.gz: 81a5a0437a5df3681e01d999042349f65a262edd
4
+ data.tar.gz: 58658dab6c7aae11b3946d73de3f61aabf3c32e0
5
5
  SHA512:
6
- metadata.gz: 4eb042521d7463a6e7d095a2f05065d451ad794b8ed2f8a0b3c553a61d1cf19d4786cb12a042a5e9d5072e66d9cd95b7a1e8ced2ff5ecfa9424972614f9a8047
7
- data.tar.gz: 63dda9940489f441c7ecff1d980712bbc80cd5430082969ff6e6ce789d87395efca167f9e4357bc3e6e9aea42ecc9b1009ac6b45f00acb7074f53c8c4bbfc7c2
6
+ metadata.gz: 81e3fcf7f66b33d7b64af4ba6d3ddab30b80c63cdfa37c31150b2bbff313c3c9aea6071ddb728ab09592acebb4da15db9346fd8283d9791717216b2a28de8a4c
7
+ data.tar.gz: d64654935917d1ef3342685b77edf356d14ea6f490e8a1da4e983736fe595cad4a9a26840b01fa7ef2948f6be66f40fb20b186b859e3ad665d4aecdd7060b1d1
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,9 @@
1
+ # Contributing
2
+
3
+ 1. Fork it ( [https://gitlab.com/wikiti-random-stuff/open-weather-api/fork/new](https://gitlab.com/wikiti-random-stuff/open-weather-api/fork/new) )
4
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
5
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
6
+ - Feel free to add yourself to *README.md*'s authors :smile:.
7
+ 4. Run tests (`rake test`)
8
+ 5. Push to the branch (`git push origin my-new-feature`)
9
+ 6. Create new Merge Request
data/README.md CHANGED
@@ -106,9 +106,7 @@ json = open_weather_api.current circle: { lat: -16.3319, lon: 28.5046 }, cities_
106
106
 
107
107
  For more information about the API, visit [http://openweathermap.org/current](http://openweathermap.org/current).
108
108
 
109
- ### Forecast
110
-
111
- #### Hourly (actually, every 3 hours, up to 5 days)
109
+ ### Hourly forecast (actually, every 3 hours, up to 5 days)
112
110
 
113
111
  By city name:
114
112
 
@@ -130,6 +128,52 @@ json = open_weather_api.forecast :hourly, lon: -16.20302, lat: 28.53924
130
128
 
131
129
  For more information about the API, visit [http://openweathermap.org/forecast5](http://openweathermap.org/forecast5).
132
130
 
131
+ #### Daily forecast (up to 16 days)
132
+
133
+ Same as above, but changing `:hourly` to `:daily`. Also, you must add the `:days` to the resquests. Default is `1`.
134
+
135
+ By city name:
136
+
137
+ ````ruby
138
+ json = open_weather_api.forecast :daily, city: 'Santa Cruz de Tenerife', country_code: 'es', days: 2
139
+ ````
140
+
141
+ By city id:
142
+
143
+ ````ruby
144
+ json = open_weather_api.forecast :daily, id: 6360638, days: 2
145
+ ````
146
+
147
+ By geolocation:
148
+
149
+ ````ruby
150
+ json = open_weather_api.forecast :daily, lon: -16.20302, lat: 28.53924, days: 2
151
+ ````
152
+
153
+ For more information about the API, visit [http://openweathermap.org/forecast5](http://openweathermap.org/forecast5).
154
+
155
+ ### Historical data
156
+
157
+ TODO
158
+
159
+ ### Weather stations
160
+
161
+ TODO
162
+
163
+ ### UV index
164
+
165
+ TODO
166
+
167
+ ### Raw requests
168
+
169
+ You can also make your very own calls to the api:
170
+
171
+ ````ruby
172
+ json = open_weather_api.raw 'route/to/my/resource', param1: 'param', param2: 'param', lang: 'es'
173
+ ````
174
+
175
+ Note that it's not necessary to add `APPID` parameter to the call.
176
+
133
177
  ### Other
134
178
 
135
179
  Retrieve icon url:
@@ -143,7 +187,7 @@ end
143
187
  You can add manually any parameter you need for each request, and they will override the computed parameters:
144
188
 
145
189
  ````ruby
146
- open_weather_api.current city: 'Balashikha', country_code: "ru", lang: "ru"
190
+ open_weather_api.current city: 'Balashikha', country_code: 'ru', lang: 'ru', mypara: 'param'
147
191
  ````
148
192
 
149
193
  Also, you can define the response format with the `:mode` parameters. Valid formats are `:json` (returns a `Hash`), `:xml` and `:html` (both return a `String`):
@@ -168,13 +212,4 @@ This project has been developed by:
168
212
 
169
213
  | Avatar | Name | Nickname | Email |
170
214
  | ------- | ------------- | --------- | ------------------ |
171
- | ![](http://www.gravatar.com/avatar/2ae6d81e0605177ba9e17b19f54e6b6c.jpg?s=64) | Daniel Herzog | Wikiti | [wikiti.doghound@gmail.com](mailto:wikiti.doghound@gmail.com)
172
-
173
- ## Contributing
174
-
175
- 1. Fork it ( [https://gitlab.com/wikiti-random-stuff/open-weather-api/fork/new](https://gitlab.com/wikiti-random-stuff/open-weather-api/fork/new) )
176
- 2. Create your feature branch (`git checkout -b my-new-feature`)
177
- 3. Commit your changes (`git commit -am 'Add some feature'`)
178
- 4. Run tests (`rake test`)
179
- 5. Push to the branch (`git push origin my-new-feature`)
180
- 6. Create new Merge Request
215
+ | ![](http://www.gravatar.com/avatar/2ae6d81e0605177ba9e17b19f54e6b6c.jpg?s=64) | Daniel Herzog | Wikiti | [wikiti.doghound@gmail.com](mailto:wikiti.doghound@gmail.com) |
@@ -5,11 +5,18 @@ require 'uri'
5
5
  require 'rest-client'
6
6
  require 'json'
7
7
 
8
- # Main files
8
+ # Resources
9
9
  require 'open-weather-api/resources/base'
10
+ require 'open-weather-api/resources/raw'
10
11
  require 'open-weather-api/resources/current'
11
12
  require 'open-weather-api/resources/forecast_hourly'
13
+
14
+ # Handlers
12
15
  require 'open-weather-api/resources/handlers/base'
13
16
  require 'open-weather-api/resources/handlers/current'
17
+
18
+ # Configuration
14
19
  require 'open-weather-api/config'
20
+
21
+ # Main file
15
22
  require 'open-weather-api/api'
@@ -16,11 +16,16 @@ module OpenWeatherAPI
16
16
  fetch_current.execute(**args, &block)
17
17
  end
18
18
 
19
- # Not yet implemented
20
19
  def forecast(type = :hourly, **args, &block)
20
+ raise "Invalid '#type' forecast type" unless valid_forecast_type?(type)
21
+
21
22
  self.send("fetch_forecast_#{type}").execute(**args, &block)
22
23
  end
23
24
 
25
+ def raw(path = "/", **args, &block)
26
+ fetch_raw.execute(path, **args, &block)
27
+ end
28
+
24
29
  def icon_url(icon_code)
25
30
  "http://openweathermap.org/img/w/#{icon_code}.png"
26
31
  end
@@ -33,6 +38,10 @@ module OpenWeatherAPI
33
38
  VALID_FORECAST_TYPES.include? type.to_sym
34
39
  end
35
40
 
41
+ def fetch_raw
42
+ @current ||= Resources::Raw.new self
43
+ end
44
+
36
45
  def fetch_current
37
46
  @current ||= Resources::Current.new self
38
47
  end
@@ -27,7 +27,11 @@ module OpenWeatherAPI
27
27
  private
28
28
 
29
29
  def mode
30
- (@parameters[:mode] || 'json').to_s.to_sym
30
+ (@parameters[:mode] || :json).to_s.to_sym
31
+ end
32
+
33
+ def handle_response_raw(response)
34
+ response
31
35
  end
32
36
 
33
37
  def handle_response_json(response)
@@ -3,10 +3,10 @@ module OpenWeatherAPI
3
3
  class Current < Base
4
4
 
5
5
  def base_url
6
- return super + 'group' if city_id.multiple?
7
- return super + 'weather' if [city, city_id, geolocation, zipcode].any? { |h| h.can? }
8
- return super + 'box/city' if bbox.can?
9
- return super + 'find' if circle.can?
6
+ return super + 'group/' if city_id.multiple?
7
+ return super + 'weather/' if [city, city_id, geolocation, zipcode].any? { |h| h.can? }
8
+ return super + 'box/city/' if bbox.can?
9
+ return super + 'find/' if circle.can?
10
10
  end
11
11
 
12
12
  def build_params(parameters = {})
@@ -15,29 +15,29 @@ module OpenWeatherAPI
15
15
 
16
16
  # Simple handlers
17
17
  def city
18
- City.new @api_obj, @parameters
18
+ Handlers::City.new @api_obj, @parameters
19
19
  end
20
20
 
21
21
  def city_id
22
- CityID.new @api_obj, @parameters
22
+ Handlers::CityID.new @api_obj, @parameters
23
23
  end
24
24
 
25
25
  def geolocation
26
- Geolocation.new @api_obj, @parameters
26
+ Handlers::Geolocation.new @api_obj, @parameters
27
27
  end
28
28
 
29
29
  def zipcode
30
- Zipcode.new @api_obj, @parameters
30
+ Handlers::Zipcode.new @api_obj, @parameters
31
31
  end
32
32
 
33
33
  # Other handlers
34
34
  # ------------------
35
35
  def bbox
36
- BoundingBox.new @api_obj, @parameters
36
+ Handlers::BoundingBox.new @api_obj, @parameters
37
37
  end
38
38
 
39
39
  def circle
40
- Circle.new @api_obj, @parameters
40
+ Handlers::Circle.new @api_obj, @parameters
41
41
  end
42
42
 
43
43
  end
@@ -0,0 +1,19 @@
1
+ module OpenWeatherAPI
2
+ module Resources
3
+ class ForecastDaily < ForecastHourly
4
+
5
+ def base_url
6
+ return super + 'daily/'
7
+ end
8
+
9
+ def build_params(parameters = {})
10
+ parameters.merge!(cnt: days)
11
+ super(parameters)
12
+ end
13
+
14
+ def days
15
+ @parameters[:days] || @parameters[:n_days] || @parameters[:cnt] || @parameters[:count]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -3,7 +3,7 @@ module OpenWeatherAPI
3
3
  class ForecastHourly < Base
4
4
 
5
5
  def base_url
6
- return super + 'forecast'
6
+ return super + 'forecast/'
7
7
  end
8
8
 
9
9
  def build_params(parameters = {})
@@ -12,15 +12,15 @@ module OpenWeatherAPI
12
12
 
13
13
  # Simple handlers
14
14
  def city
15
- City.new @api_obj, @parameters
15
+ Handlers::City.new @api_obj, @parameters
16
16
  end
17
17
 
18
18
  def city_id
19
- CityID.new @api_obj, @parameters
19
+ Handlers::CityID.new @api_obj, @parameters
20
20
  end
21
21
 
22
22
  def geolocation
23
- Geolocation.new @api_obj, @parameters
23
+ Handlers::Geolocation.new @api_obj, @parameters
24
24
  end
25
25
 
26
26
  end
@@ -1,97 +1,99 @@
1
1
  module OpenWeatherAPI
2
2
  module Resources
3
+ module Handlers
3
4
 
4
- class QueryHandler
5
- def initialize(api_obj, parameters = {})
6
- @api_obj = api_obj
7
- @parameters = parameters
8
- end
5
+ class QueryHandler
6
+ def initialize(api_obj, parameters = {})
7
+ @api_obj = api_obj
8
+ @parameters = parameters
9
+ end
9
10
 
10
- def handle
11
- build if can?
12
- end
11
+ def handle
12
+ build if can?
13
+ end
13
14
 
14
- def can?
15
- value != nil
16
- end
15
+ def can?
16
+ value != nil
17
+ end
17
18
 
18
- private
19
+ private
19
20
 
20
- def value
21
- true
22
- end
21
+ def value
22
+ true
23
+ end
23
24
 
24
- def build
25
- @parameters
26
- end
25
+ def build
26
+ @parameters
27
+ end
27
28
 
28
- def country_code
29
- @parameters[:country_code] || @parameters[:cc] || @api_obj.default_country_code
30
- end
29
+ def country_code
30
+ @parameters[:country_code] || @parameters[:cc] || @api_obj.default_country_code
31
+ end
31
32
 
32
- def cities_count
33
- @parameters[:count] || @parameters[:cnt] || @parameters[:cities_count]
34
- end
33
+ def cities_count
34
+ @parameters[:count] || @parameters[:cnt] || @parameters[:cities_count]
35
+ end
35
36
 
36
- def cluster
37
- @parameters[:cluster] if @parameters[:cluster].to_s == 'yes' || @parameters[:cluster].to_s == 'no'
38
- end
37
+ def cluster
38
+ @parameters[:cluster] if @parameters[:cluster].to_s == 'yes' || @parameters[:cluster].to_s == 'no'
39
+ end
39
40
 
40
- def fill(hash)
41
- hash[:cnt] = cities_count if cities_count
42
- hash[:cluster] = cluster if cluster
41
+ def fill(hash)
42
+ hash[:cnt] = cities_count if cities_count
43
+ hash[:cluster] = cluster if cluster
43
44
 
44
- hash
45
+ hash
46
+ end
45
47
  end
46
48
 
47
- end
48
-
49
- class City < QueryHandler
50
- private
49
+ class City < QueryHandler
50
+ private
51
51
 
52
- def build
53
- { q: [value, country_code].compact.flatten.join(',') }
52
+ def build
53
+ { q: [value, country_code].compact.flatten.join(',') }
54
+ end
55
+
56
+ def value
57
+ @parameters[:city]
58
+ end
54
59
  end
55
-
56
- def value
57
- @parameters[:city]
58
- end
59
- end
60
60
 
61
- class CityID < QueryHandler
62
- def multiple?
63
- value.is_a? Array
64
- end
61
+ class CityID < QueryHandler
62
+ def multiple?
63
+ value.is_a? Array
64
+ end
65
65
 
66
- private
66
+ private
67
67
 
68
- def build
69
- { id: [value].flatten.compact.join(',') }
70
- end
68
+ def build
69
+ { id: [value].flatten.compact.join(',') }
70
+ end
71
71
 
72
- def value
73
- @parameters[:id] || @parameters[:city_id]
72
+ def value
73
+ @parameters[:id] || @parameters[:city_id]
74
+ end
74
75
  end
75
- end
76
76
 
77
- class Geolocation < QueryHandler
78
- def can?
79
- latitude != nil && longitude != nil
80
- end
77
+ class Geolocation < QueryHandler
78
+ def can?
79
+ latitude != nil && longitude != nil
80
+ end
81
81
 
82
- private
82
+ private
83
83
 
84
- def build
85
- { lat: latitude, lon: longitude }
86
- end
84
+ def build
85
+ { lat: latitude, lon: longitude }
86
+ end
87
87
 
88
- def latitude
89
- @parameters[:latitude] || @parameters[:lat]
90
- end
88
+ def latitude
89
+ @parameters[:latitude] || @parameters[:lat]
90
+ end
91
91
 
92
- def longitude
93
- @parameters[:longitude] || @parameters[:lon]
92
+ def longitude
93
+ @parameters[:longitude] || @parameters[:lon]
94
+ end
94
95
  end
96
+
95
97
  end
96
98
  end
97
99
  end
@@ -1,8 +1,6 @@
1
1
  module OpenWeatherAPI
2
2
  module Resources
3
-
4
- class Current
5
- private
3
+ module Handlers
6
4
 
7
5
  class Zipcode < QueryHandler
8
6
  private
@@ -51,6 +49,7 @@ module OpenWeatherAPI
51
49
  fill lat: value[:lat], lon: value[:lon]
52
50
  end
53
51
  end
52
+
54
53
  end
55
54
  end
56
55
  end
@@ -0,0 +1,15 @@
1
+ module OpenWeatherAPI
2
+ module Resources
3
+ class Raw < Base
4
+
5
+ def execute(resource = '/', **args, &block)
6
+ @resource = resource
7
+ super(**args, &block)
8
+ end
9
+
10
+ def base_url
11
+ return super + @resource
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module OpenWeatherAPI
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpenWeatherAPI::API do
4
+ let(:api) { OpenWeatherAPI::API.new( api_key: ENV['OPEN_WEATHER_API_KEY'], default_language: 'es', default_country_code: 'es' ) }
5
+
6
+ describe 'When fetching daily forecast weather' do
7
+ it 'should retrieve data by city name' do
8
+ expect(api.forecast(:daily, city: 'Santa Cruz de Tenerife', days: 3)[:cod].to_i).to eq(200)
9
+ end
10
+
11
+ it 'should retrieve data by city id' do
12
+ expect(api.forecast(:daily, id: 6360638, days: 3)[:cod].to_i).to eq(200)
13
+ end
14
+
15
+ it 'should retrieve data by geolocation' do
16
+ expect(api.forecast(:daily, lon: -16.20302, lat: 28.53924, days: 3)[:cod].to_i).to eq(200)
17
+ end
18
+
19
+ it 'works with a given block' do
20
+ json1, json2 = nil
21
+ json1 = api.forecast(:daily, city: 'Santa Cruz de Tenerife', days: 3) { |json| json2 = json }
22
+
23
+ expect(json1).to eq(json2)
24
+ end
25
+
26
+ it 'works with xml format' do
27
+ expect(api.forecast(:daily, city: 'Santa Cruz de Tenerife', days: 3, mode: :xml)).not_to be_nil
28
+ end
29
+
30
+ it 'works with html format' do
31
+ expect(api.forecast(:daily, city: 'Santa Cruz de Tenerife', days: 3, mode: :html)).not_to be_nil
32
+ end
33
+ end
34
+ end
data/spec/raw_spec.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpenWeatherAPI::API do
4
+ let(:api) { OpenWeatherAPI::API.new( api_key: ENV['OPEN_WEATHER_API_KEY'], default_language: 'es', default_country_code: 'es' ) }
5
+
6
+ describe 'When fetching raw rest requests with valid data' do
7
+ it 'should work' do
8
+ expect(api.raw("weather", q: 'Santa Cruz de Tenerife,ES')[:cod].to_i).to eq(200)
9
+ end
10
+ end
11
+
12
+ describe 'When fetching raw rest requests with invalid data' do
13
+ it 'should fail' do
14
+ expect { api.raw("undefined_resource", q: 'whatever', mode: :raw) }.to raise_error
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open-weather-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wikiti
@@ -62,6 +62,7 @@ extra_rdoc_files: []
62
62
  files:
63
63
  - .gitignore
64
64
  - .rspec
65
+ - CONTRIBUTING.md
65
66
  - Gemfile
66
67
  - LICENSE.txt
67
68
  - README.md
@@ -71,14 +72,18 @@ files:
71
72
  - lib/open-weather-api/config.rb
72
73
  - lib/open-weather-api/resources/base.rb
73
74
  - lib/open-weather-api/resources/current.rb
75
+ - lib/open-weather-api/resources/forecast_daily.rb
74
76
  - lib/open-weather-api/resources/forecast_hourly.rb
75
77
  - lib/open-weather-api/resources/handlers/base.rb
76
78
  - lib/open-weather-api/resources/handlers/current.rb
79
+ - lib/open-weather-api/resources/raw.rb
77
80
  - lib/open-weather-api/version.rb
78
81
  - open-weather-api.gemspec
79
82
  - rakefile
80
83
  - spec/current_weather_spec.rb
84
+ - spec/forecast_daily_spec.rb
81
85
  - spec/forecast_hourly_spec.rb
86
+ - spec/raw_spec.rb
82
87
  - spec/spec_helper.rb
83
88
  homepage: https://gitlab.com/wikiti-random-stuff/open-weather-api
84
89
  licenses:
@@ -106,6 +111,8 @@ specification_version: 4
106
111
  summary: Simple wrapper for Open Weather Map API
107
112
  test_files:
108
113
  - spec/current_weather_spec.rb
114
+ - spec/forecast_daily_spec.rb
109
115
  - spec/forecast_hourly_spec.rb
116
+ - spec/raw_spec.rb
110
117
  - spec/spec_helper.rb
111
118
  has_rdoc: