open-weather-api 0.0.4 → 0.0.5
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/README.md +46 -13
- data/lib/open-weather-api.rb +1 -0
- data/lib/open-weather-api/api.rb +2 -2
- data/lib/open-weather-api/resources/base.rb +21 -7
- data/lib/open-weather-api/resources/forecast_hourly.rb +28 -0
- data/lib/open-weather-api/resources/handlers/base.rb +48 -0
- data/lib/open-weather-api/resources/handlers/current.rb +0 -48
- data/lib/open-weather-api/version.rb +1 -1
- data/spec/current_weather_spec.rb +8 -0
- data/spec/forecast_hourly_spec.rb +34 -0
- data/spec/spec_helper.rb +2 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcd2a1c29774ef26c4a0b443e44f2649485a8f51
|
4
|
+
data.tar.gz: f8b4a8fc6668ee26393366a6df94bd4dd339b1d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4eb042521d7463a6e7d095a2f05065d451ad794b8ed2f8a0b3c553a61d1cf19d4786cb12a042a5e9d5072e66d9cd95b7a1e8ced2ff5ecfa9424972614f9a8047
|
7
|
+
data.tar.gz: 63dda9940489f441c7ecff1d980712bbc80cd5430082969ff6e6ce789d87395efca167f9e4357bc3e6e9aea42ecc9b1009ac6b45f00acb7074f53c8c4bbfc7c2
|
data/README.md
CHANGED
@@ -49,7 +49,7 @@ Outside of the configuration file, you can access the `api` object as follows:
|
|
49
49
|
Rails.configuration.open_weather_api
|
50
50
|
````
|
51
51
|
|
52
|
-
###
|
52
|
+
### Generic
|
53
53
|
|
54
54
|
```ruby
|
55
55
|
open_weather_api = OpenWeatherAPI::API.new api_key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", default_language: 'es', default_units: 'metric', default_country_code: 'es'
|
@@ -60,8 +60,6 @@ open_weather_api = OpenWeatherAPI::API.new api_key: "xxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
60
60
|
|
61
61
|
Finally, you can use the different resources of the API:
|
62
62
|
|
63
|
-
**NOTE**: You can add manually any parameter you need for each request, and they will override the computed parameters.
|
64
|
-
|
65
63
|
### Current Weather
|
66
64
|
|
67
65
|
By city name:
|
@@ -106,19 +104,31 @@ By a geolocated circle (**WARNING**: Unexpected behaviour by API):
|
|
106
104
|
json = open_weather_api.current circle: { lat: -16.3319, lon: 28.5046 }, cities_count: 2
|
107
105
|
````
|
108
106
|
|
109
|
-
|
107
|
+
For more information about the API, visit [http://openweathermap.org/current](http://openweathermap.org/current).
|
108
|
+
|
109
|
+
### Forecast
|
110
|
+
|
111
|
+
#### Hourly (actually, every 3 hours, up to 5 days)
|
112
|
+
|
113
|
+
By city name:
|
110
114
|
|
111
115
|
````ruby
|
112
|
-
json = open_weather_api.
|
113
|
-
puts JSON.pretty_generate(json)
|
114
|
-
end
|
116
|
+
json = open_weather_api.forecast :hourly, city: 'Santa Cruz de Tenerife', country_code: 'es'
|
115
117
|
````
|
116
118
|
|
117
|
-
|
119
|
+
By city id:
|
118
120
|
|
119
|
-
|
121
|
+
````ruby
|
122
|
+
json = open_weather_api.forecast :hourly, id: 6360638
|
123
|
+
````
|
120
124
|
|
121
|
-
|
125
|
+
By geolocation:
|
126
|
+
|
127
|
+
````ruby
|
128
|
+
json = open_weather_api.forecast :hourly, lon: -16.20302, lat: 28.53924
|
129
|
+
````
|
130
|
+
|
131
|
+
For more information about the API, visit [http://openweathermap.org/forecast5](http://openweathermap.org/forecast5).
|
122
132
|
|
123
133
|
### Other
|
124
134
|
|
@@ -130,6 +140,28 @@ open_weather_api.current city: 'Santa Cruz de Tenerife', country_code: 'es' do |
|
|
130
140
|
end
|
131
141
|
````
|
132
142
|
|
143
|
+
You can add manually any parameter you need for each request, and they will override the computed parameters:
|
144
|
+
|
145
|
+
````ruby
|
146
|
+
open_weather_api.current city: 'Balashikha', country_code: "ru", lang: "ru"
|
147
|
+
````
|
148
|
+
|
149
|
+
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`):
|
150
|
+
|
151
|
+
````ruby
|
152
|
+
open_weather_api.current city: 'Santa Cruz de Tenerife', mode: :xml do |xml_str|
|
153
|
+
puts "XML data: #{xml_str}"
|
154
|
+
end
|
155
|
+
````
|
156
|
+
|
157
|
+
You can use ruby blocks to handle the response:
|
158
|
+
|
159
|
+
````ruby
|
160
|
+
open_weather_api.current city: 'Santa Cruz de Tenerife', country_code: 'es', mode: :json do |json|
|
161
|
+
puts JSON.pretty_generate(json)
|
162
|
+
end
|
163
|
+
````
|
164
|
+
|
133
165
|
## Authors ##
|
134
166
|
|
135
167
|
This project has been developed by:
|
@@ -142,6 +174,7 @@ This project has been developed by:
|
|
142
174
|
|
143
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) )
|
144
176
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
145
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
146
|
-
4.
|
147
|
-
5.
|
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
|
data/lib/open-weather-api.rb
CHANGED
@@ -8,6 +8,7 @@ require 'json'
|
|
8
8
|
# Main files
|
9
9
|
require 'open-weather-api/resources/base'
|
10
10
|
require 'open-weather-api/resources/current'
|
11
|
+
require 'open-weather-api/resources/forecast_hourly'
|
11
12
|
require 'open-weather-api/resources/handlers/base'
|
12
13
|
require 'open-weather-api/resources/handlers/current'
|
13
14
|
require 'open-weather-api/config'
|
data/lib/open-weather-api/api.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
module OpenWeatherAPI
|
2
2
|
class API
|
3
3
|
|
4
|
+
VERSION = "2.5"
|
5
|
+
|
4
6
|
attr_accessor :api_key, :default_language, :default_country_code, :default_units
|
5
7
|
|
6
8
|
def initialize(options = {})
|
@@ -16,8 +18,6 @@ module OpenWeatherAPI
|
|
16
18
|
|
17
19
|
# Not yet implemented
|
18
20
|
def forecast(type = :hourly, **args, &block)
|
19
|
-
raise ArgumentError, "Invalid #{type} forecast type."
|
20
|
-
|
21
21
|
self.send("fetch_forecast_#{type}").execute(**args, &block)
|
22
22
|
end
|
23
23
|
|
@@ -13,24 +13,38 @@ module OpenWeatherAPI
|
|
13
13
|
setup_indifferent_access(@parameters)
|
14
14
|
|
15
15
|
# Let's use json
|
16
|
-
|
16
|
+
response = RestClient.send :get, base_url, params: build_params(@parameters)
|
17
|
+
raise "Invalid response." unless response.code == 200
|
18
|
+
|
19
|
+
# Handle the response format
|
20
|
+
response = self.send "handle_response_#{mode}", response
|
21
|
+
|
22
|
+
# Handle the block
|
23
|
+
return block.call(response) if block_given?
|
24
|
+
response
|
17
25
|
end
|
18
26
|
|
19
27
|
private
|
20
28
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
29
|
+
def mode
|
30
|
+
(@parameters[:mode] || 'json').to_s.to_sym
|
31
|
+
end
|
24
32
|
|
33
|
+
def handle_response_json(response)
|
25
34
|
json = JSON.parse(response.body)
|
26
35
|
setup_indifferent_access(json)
|
36
|
+
end
|
37
|
+
|
38
|
+
def handle_response_xml(response)
|
39
|
+
response.body
|
40
|
+
end
|
27
41
|
|
28
|
-
|
29
|
-
|
42
|
+
def handle_response_html(response)
|
43
|
+
response.body
|
30
44
|
end
|
31
45
|
|
32
46
|
def base_url
|
33
|
-
|
47
|
+
"http://api.openweathermap.org/data/#{API::VERSION || '2.5'}/"
|
34
48
|
end
|
35
49
|
|
36
50
|
def setup_indifferent_access(sub_hash)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module OpenWeatherAPI
|
2
|
+
module Resources
|
3
|
+
class ForecastHourly < Base
|
4
|
+
|
5
|
+
def base_url
|
6
|
+
return super + 'forecast'
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_params(parameters = {})
|
10
|
+
super [city, city_id, geolocation].each{ |h| break h.handle if h.can? }
|
11
|
+
end
|
12
|
+
|
13
|
+
# Simple handlers
|
14
|
+
def city
|
15
|
+
City.new @api_obj, @parameters
|
16
|
+
end
|
17
|
+
|
18
|
+
def city_id
|
19
|
+
CityID.new @api_obj, @parameters
|
20
|
+
end
|
21
|
+
|
22
|
+
def geolocation
|
23
|
+
Geolocation.new @api_obj, @parameters
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -45,5 +45,53 @@ module OpenWeatherAPI
|
|
45
45
|
end
|
46
46
|
|
47
47
|
end
|
48
|
+
|
49
|
+
class City < QueryHandler
|
50
|
+
private
|
51
|
+
|
52
|
+
def build
|
53
|
+
{ q: [value, country_code].compact.flatten.join(',') }
|
54
|
+
end
|
55
|
+
|
56
|
+
def value
|
57
|
+
@parameters[:city]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class CityID < QueryHandler
|
62
|
+
def multiple?
|
63
|
+
value.is_a? Array
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def build
|
69
|
+
{ id: [value].flatten.compact.join(',') }
|
70
|
+
end
|
71
|
+
|
72
|
+
def value
|
73
|
+
@parameters[:id] || @parameters[:city_id]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class Geolocation < QueryHandler
|
78
|
+
def can?
|
79
|
+
latitude != nil && longitude != nil
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def build
|
85
|
+
{ lat: latitude, lon: longitude }
|
86
|
+
end
|
87
|
+
|
88
|
+
def latitude
|
89
|
+
@parameters[:latitude] || @parameters[:lat]
|
90
|
+
end
|
91
|
+
|
92
|
+
def longitude
|
93
|
+
@parameters[:longitude] || @parameters[:lon]
|
94
|
+
end
|
95
|
+
end
|
48
96
|
end
|
49
97
|
end
|
@@ -4,54 +4,6 @@ module OpenWeatherAPI
|
|
4
4
|
class Current
|
5
5
|
private
|
6
6
|
|
7
|
-
class City < QueryHandler
|
8
|
-
private
|
9
|
-
|
10
|
-
def build
|
11
|
-
{ q: [value, country_code].compact.flatten.join(',') }
|
12
|
-
end
|
13
|
-
|
14
|
-
def value
|
15
|
-
@parameters[:city]
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class CityID < QueryHandler
|
20
|
-
def multiple?
|
21
|
-
value.is_a? Array
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def build
|
27
|
-
{ id: [value].flatten.compact.join(',') }
|
28
|
-
end
|
29
|
-
|
30
|
-
def value
|
31
|
-
@parameters[:id] || @parameters[:city_id]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class Geolocation < QueryHandler
|
36
|
-
def can?
|
37
|
-
latitude != nil && longitude != nil
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
|
42
|
-
def build
|
43
|
-
{ lat: latitude, lon: longitude }
|
44
|
-
end
|
45
|
-
|
46
|
-
def latitude
|
47
|
-
@parameters[:latitude] || @parameters[:lat]
|
48
|
-
end
|
49
|
-
|
50
|
-
def longitude
|
51
|
-
@parameters[:longitude] || @parameters[:lon]
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
7
|
class Zipcode < QueryHandler
|
56
8
|
private
|
57
9
|
|
@@ -39,5 +39,13 @@ describe OpenWeatherAPI::API do
|
|
39
39
|
|
40
40
|
expect(json1).to eq(json2)
|
41
41
|
end
|
42
|
+
|
43
|
+
it 'works with xml format' do
|
44
|
+
expect(api.current(city: 'Santa Cruz de Tenerife', mode: :xml)).not_to be_nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'works with html format' do
|
48
|
+
expect(api.current(city: 'Santa Cruz de Tenerife', mode: :html)).not_to be_nil
|
49
|
+
end
|
42
50
|
end
|
43
51
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe OpenWeatherAPI::API do
|
4
|
+
describe 'When fetching hourly forecast weather' do
|
5
|
+
let(:api) { OpenWeatherAPI::API.new( api_key: ENV['OPEN_WEATHER_API_KEY'], default_language: 'es', default_country_code: 'es' ) }
|
6
|
+
|
7
|
+
it 'should retrieve data by city name' do
|
8
|
+
expect(api.forecast(:hourly, city: 'Santa Cruz de Tenerife')[:cod].to_i).to eq(200)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should retrieve data by city id' do
|
12
|
+
expect(api.forecast(:hourly, id: 6360638)[:cod].to_i).to eq(200)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should retrieve data by geolocation' do
|
16
|
+
expect(api.forecast(:hourly, lon: -16.20302, lat: 28.53924)[: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(:hourly, city: 'Santa Cruz de Tenerife') { |json| json2 = json }
|
22
|
+
|
23
|
+
expect(json1).to eq(json2)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'works with xml format' do
|
27
|
+
expect(api.forecast(:hourly, city: 'Santa Cruz de Tenerife', mode: :xml)).not_to be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'works with html format' do
|
31
|
+
expect(api.forecast(:hourly, city: 'Santa Cruz de Tenerife', mode: :html)).not_to be_nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wikiti
|
@@ -71,12 +71,14 @@ files:
|
|
71
71
|
- lib/open-weather-api/config.rb
|
72
72
|
- lib/open-weather-api/resources/base.rb
|
73
73
|
- lib/open-weather-api/resources/current.rb
|
74
|
+
- lib/open-weather-api/resources/forecast_hourly.rb
|
74
75
|
- lib/open-weather-api/resources/handlers/base.rb
|
75
76
|
- lib/open-weather-api/resources/handlers/current.rb
|
76
77
|
- lib/open-weather-api/version.rb
|
77
78
|
- open-weather-api.gemspec
|
78
79
|
- rakefile
|
79
80
|
- spec/current_weather_spec.rb
|
81
|
+
- spec/forecast_hourly_spec.rb
|
80
82
|
- spec/spec_helper.rb
|
81
83
|
homepage: https://gitlab.com/wikiti-random-stuff/open-weather-api
|
82
84
|
licenses:
|
@@ -104,5 +106,6 @@ specification_version: 4
|
|
104
106
|
summary: Simple wrapper for Open Weather Map API
|
105
107
|
test_files:
|
106
108
|
- spec/current_weather_spec.rb
|
109
|
+
- spec/forecast_hourly_spec.rb
|
107
110
|
- spec/spec_helper.rb
|
108
111
|
has_rdoc:
|