open-weather-api 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -3
- data/lib/open-weather-api.rb +3 -1
- data/lib/open-weather-api/resources/{base_resource.rb → base.rb} +0 -0
- data/lib/open-weather-api/resources/current.rb +16 -128
- data/lib/open-weather-api/resources/handlers/base.rb +48 -0
- data/lib/open-weather-api/resources/handlers/current.rb +104 -0
- data/lib/open-weather-api/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65b0e18d84c87ea5d6bc9709a9a816e8d3255f95
|
4
|
+
data.tar.gz: e875db9ab10e3106fdebfc71c167e5c59e9fb783
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a4b690505ec259f619696b0bbe217fcab3acb5972c9683fb101154aa248a5a75912b5be20828aff24c2d9f33cfd6fea12cf458597793235973dbcb84ad5d4eb
|
7
|
+
data.tar.gz: c60e78f5eddd4aae49efe20392035c728de3731cb9d56f27d36982c08cc6caaafa9afbf1fab55fa3c3b0f1d15d48b716358a86949ca9dfa33be89edad287fa1b
|
data/README.md
CHANGED
@@ -96,17 +96,27 @@ json = open_weather_api.current circle: { lat: -16.3319, lon: 28.5046 }, cities_
|
|
96
96
|
You can also use ruby blocks to handle the response:
|
97
97
|
|
98
98
|
````ruby
|
99
|
-
open_weather_api.current city: 'Santa Cruz de Tenerife', country_code: 'es' do |json|
|
99
|
+
json = open_weather_api.current city: 'Santa Cruz de Tenerife', country_code: 'es' do |json|
|
100
100
|
puts JSON.pretty_generate(json)
|
101
101
|
end
|
102
102
|
````
|
103
103
|
|
104
|
-
For more information about the API, visit
|
104
|
+
For more information about the API, visit [http://openweathermap.org/current](http://openweathermap.org/current).
|
105
105
|
|
106
106
|
### Forecast
|
107
107
|
|
108
108
|
TODO.
|
109
109
|
|
110
|
+
### Other
|
111
|
+
|
112
|
+
Retrieve icon url:
|
113
|
+
|
114
|
+
````ruby
|
115
|
+
api.current city: 'Santa Cruz de Tenerife', country_code: 'es' do |json|
|
116
|
+
puts "Icon url: #{api.icon_url json['weather'].first['icon']}"
|
117
|
+
end
|
118
|
+
````
|
119
|
+
|
110
120
|
## Authors ##
|
111
121
|
|
112
122
|
This project has been developed by:
|
@@ -117,7 +127,7 @@ This project has been developed by:
|
|
117
127
|
|
118
128
|
## Contributing
|
119
129
|
|
120
|
-
1. Fork it (
|
130
|
+
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) )
|
121
131
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
122
132
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
123
133
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/open-weather-api.rb
CHANGED
@@ -6,6 +6,8 @@ require 'rest-client'
|
|
6
6
|
require 'json'
|
7
7
|
|
8
8
|
# Main files
|
9
|
-
require 'open-weather-api/resources/
|
9
|
+
require 'open-weather-api/resources/base'
|
10
10
|
require 'open-weather-api/resources/current'
|
11
|
+
require 'open-weather-api/resources/handlers/base'
|
12
|
+
require 'open-weather-api/resources/handlers/current'
|
11
13
|
require 'open-weather-api/api'
|
File without changes
|
@@ -1,157 +1,45 @@
|
|
1
1
|
module OpenWeatherAPI
|
2
2
|
module Resources
|
3
|
-
class Current < Base
|
4
|
-
|
5
|
-
|
3
|
+
class Current < Base
|
4
|
+
|
6
5
|
def base_url
|
7
|
-
return super + 'group' if
|
8
|
-
return super + 'weather' if
|
9
|
-
return super + 'box/city' if
|
10
|
-
return super + 'find' if
|
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?
|
11
10
|
end
|
12
11
|
|
13
12
|
def build_params(parameters = {})
|
14
|
-
super
|
15
|
-
end
|
16
|
-
|
17
|
-
# Basic city
|
18
|
-
# ------------------
|
19
|
-
|
20
|
-
# For city
|
21
|
-
def is_city?
|
22
|
-
city != nil
|
13
|
+
super [city, city_id, geolocation, zipcode, bbox, circle].each{ |h| break h.handle if h.can? }
|
23
14
|
end
|
24
15
|
|
16
|
+
# Simple handlers
|
25
17
|
def city
|
26
|
-
@parameters
|
18
|
+
City.new @parameters
|
27
19
|
end
|
28
20
|
|
29
|
-
def country_code
|
30
|
-
@parameters[:country_code] || @api_obj.default_country_code
|
31
|
-
end
|
32
|
-
|
33
|
-
def build_for_city
|
34
|
-
return { q: [city, country_code].compact.flatten.join(',') } if is_city?
|
35
|
-
end
|
36
|
-
|
37
|
-
# For city id
|
38
|
-
def is_city_id?
|
39
|
-
city_id != nil
|
40
|
-
end
|
41
|
-
|
42
|
-
def is_multiple_city_id?
|
43
|
-
city_id.is_a? Array
|
44
|
-
end
|
45
|
-
|
46
21
|
def city_id
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
def build_for_city_id
|
51
|
-
return { id: city_id } if is_city_id?
|
52
|
-
end
|
53
|
-
|
54
|
-
def build_for_multiple
|
55
|
-
return { id: city_id.compact.join(',') } if is_multiple_city_id?
|
56
|
-
end
|
57
|
-
|
58
|
-
# For geolocation
|
59
|
-
def is_geolocation?
|
60
|
-
geolocation[:lat] != nil && geolocation[:lon] != nil
|
22
|
+
CityID.new @parameters
|
61
23
|
end
|
62
24
|
|
63
25
|
def geolocation
|
64
|
-
|
65
|
-
lat: latitude,
|
66
|
-
lon: longitude
|
67
|
-
}
|
68
|
-
end
|
69
|
-
|
70
|
-
def latitude
|
71
|
-
@parameters[:latitude] || @parameters[:lat]
|
72
|
-
end
|
73
|
-
|
74
|
-
def longitude
|
75
|
-
@parameters[:longitude] || @parameters[:lon]
|
76
|
-
end
|
77
|
-
|
78
|
-
def build_for_geolocation
|
79
|
-
return geolocation if is_geolocation?
|
80
|
-
end
|
81
|
-
|
82
|
-
# For zip code
|
83
|
-
def is_zipcode?
|
84
|
-
zipcode != nil
|
26
|
+
Geolocation.new @parameters
|
85
27
|
end
|
86
28
|
|
87
29
|
def zipcode
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
def build_for_zipcode
|
92
|
-
return { zip: [zipcode, country_code].compact.flatten.join(',') } if is_zipcode?
|
30
|
+
Zipcode.new @parameters
|
93
31
|
end
|
94
32
|
|
95
|
-
#
|
33
|
+
# Other handlers
|
96
34
|
# ------------------
|
97
|
-
|
98
|
-
def cities_count
|
99
|
-
@parameters[:count] || @parameters[:cnt] || @parameters[:cities_count]
|
100
|
-
end
|
101
|
-
|
102
|
-
def cluster
|
103
|
-
@parameters[:cluster] if @parameters[:cluster].to_s == 'yes' || @parameters[:cluster].to_s == 'no'
|
104
|
-
end
|
105
|
-
|
106
|
-
# Bounding box
|
107
|
-
def is_bbox?
|
108
|
-
bbox != nil
|
109
|
-
end
|
110
|
-
|
111
|
-
def bbox_top
|
112
|
-
[ bbox[:topleft][:lat], bbox[:topleft][:lon] ].join(',')
|
113
|
-
end
|
114
|
-
|
115
|
-
def bbox_bottom
|
116
|
-
[ bbox[:bottomright][:lat], bbox[:bottomright][:lon] ].join(',')
|
117
|
-
end
|
118
|
-
|
119
|
-
def bbox_zoom
|
120
|
-
bbox[:zoom] || bbox[:map_zoom] || 10
|
121
|
-
end
|
122
|
-
|
123
35
|
def bbox
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
def build_for_bbox
|
128
|
-
if is_bbox?
|
129
|
-
hash = { bbox: [bbox_top, bbox_bottom, bbox_zoom].join(',') }
|
130
|
-
hash[:cnt] = cities_count if cities_count
|
131
|
-
hash[:cluster] = cluster if cluster
|
132
|
-
|
133
|
-
hash
|
134
|
-
end
|
36
|
+
BoundingBox.new @parameters
|
135
37
|
end
|
136
|
-
|
137
|
-
# Circle
|
138
|
-
def is_circle?
|
139
|
-
circle != nil
|
140
|
-
end
|
141
|
-
|
38
|
+
|
142
39
|
def circle
|
143
|
-
@parameters
|
40
|
+
Circle.new @parameters
|
144
41
|
end
|
145
42
|
|
146
|
-
def build_for_circle
|
147
|
-
if is_circle?
|
148
|
-
hash = { lat: circle[:lat], lon: circle[:lon]}
|
149
|
-
hash[:cnt] = cities_count if cities_count
|
150
|
-
hash[:cluster] = cluster if cluster
|
151
|
-
|
152
|
-
hash
|
153
|
-
end
|
154
|
-
end
|
155
43
|
end
|
156
44
|
end
|
157
45
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module OpenWeatherAPI
|
2
|
+
module Resources
|
3
|
+
|
4
|
+
class QueryHandler
|
5
|
+
def initialize(parameters = {})
|
6
|
+
@parameters = parameters
|
7
|
+
end
|
8
|
+
|
9
|
+
def handle
|
10
|
+
build if can?
|
11
|
+
end
|
12
|
+
|
13
|
+
def can?
|
14
|
+
value != nil
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def value
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
23
|
+
def build
|
24
|
+
@parameters
|
25
|
+
end
|
26
|
+
|
27
|
+
def country_code
|
28
|
+
@parameters[:country_code] || @api_obj.default_country_code
|
29
|
+
end
|
30
|
+
|
31
|
+
def cities_count
|
32
|
+
@parameters[:count] || @parameters[:cnt] || @parameters[:cities_count]
|
33
|
+
end
|
34
|
+
|
35
|
+
def cluster
|
36
|
+
@parameters[:cluster] if @parameters[:cluster].to_s == 'yes' || @parameters[:cluster].to_s == 'no'
|
37
|
+
end
|
38
|
+
|
39
|
+
def fill(hash)
|
40
|
+
hash[:cnt] = cities_count if cities_count
|
41
|
+
hash[:cluster] = cluster if cluster
|
42
|
+
|
43
|
+
hash
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module OpenWeatherAPI
|
2
|
+
module Resources
|
3
|
+
|
4
|
+
class Current
|
5
|
+
private
|
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
|
+
{ q: [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
|
+
class Zipcode < QueryHandler
|
56
|
+
private
|
57
|
+
|
58
|
+
def build
|
59
|
+
{ zip: [value, country_code].compact.flatten.join(',') }
|
60
|
+
end
|
61
|
+
|
62
|
+
def value
|
63
|
+
@parameters[:zipcode] || @parameters[:zip]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class BoundingBox < QueryHandler
|
68
|
+
private
|
69
|
+
|
70
|
+
def topleft
|
71
|
+
[ value[:topleft][:lat], value[:topleft][:lon] ].join(',')
|
72
|
+
end
|
73
|
+
|
74
|
+
def bottomright
|
75
|
+
[ value[:bottomright][:lat], value[:bottomright][:lon] ].join(',')
|
76
|
+
end
|
77
|
+
|
78
|
+
def zoom
|
79
|
+
value[:zoom] || value[:map_zoom] || 10
|
80
|
+
end
|
81
|
+
|
82
|
+
def value
|
83
|
+
@parameters[:box] || @parameters[:box] || @parameters[:rect] || @parameters[:rectangle]
|
84
|
+
end
|
85
|
+
|
86
|
+
def build
|
87
|
+
fill bbox: [topleft, bottomright, zoom].join(',')
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class Circle < QueryHandler
|
92
|
+
private
|
93
|
+
|
94
|
+
def value
|
95
|
+
@parameters[:circle]
|
96
|
+
end
|
97
|
+
|
98
|
+
def build
|
99
|
+
fill lat: value[:lat], lon: value[:lon]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
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.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wikiti
|
@@ -38,8 +38,10 @@ files:
|
|
38
38
|
- doc/icon.png
|
39
39
|
- lib/open-weather-api.rb
|
40
40
|
- lib/open-weather-api/api.rb
|
41
|
-
- lib/open-weather-api/resources/
|
41
|
+
- lib/open-weather-api/resources/base.rb
|
42
42
|
- lib/open-weather-api/resources/current.rb
|
43
|
+
- lib/open-weather-api/resources/handlers/base.rb
|
44
|
+
- lib/open-weather-api/resources/handlers/current.rb
|
43
45
|
- lib/open-weather-api/version.rb
|
44
46
|
- open-weather-api.gemspec
|
45
47
|
homepage: https://gitlab.com/wikiti-random-stuff/open-weather-api
|