open-weather-api 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/CONTRIBUTING.md +9 -9
- data/Gemfile +2 -2
- data/lib/open-weather-api/api.rb +56 -56
- data/lib/open-weather-api/config.rb +8 -8
- data/lib/open-weather-api/resources/base.rb +66 -66
- data/lib/open-weather-api/resources/current.rb +44 -44
- data/lib/open-weather-api/resources/forecast_daily.rb +18 -18
- data/lib/open-weather-api/resources/forecast_hourly.rb +27 -27
- data/lib/open-weather-api/resources/handlers/base.rb +98 -98
- data/lib/open-weather-api/resources/handlers/current.rb +54 -54
- data/lib/open-weather-api/resources/raw.rb +14 -14
- data/lib/open-weather-api/version.rb +1 -1
- data/open-weather-api.gemspec +1 -1
- data/rakefile +29 -28
- data/spec/current_weather_spec.rb +50 -50
- data/spec/forecast_daily_spec.rb +33 -33
- data/spec/forecast_hourly_spec.rb +33 -33
- data/spec/raw_spec.rb +16 -16
- data/spec/spec_helper.rb +11 -11
- metadata +15 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 436fa2c7c481014d726c6b5fbe94b0cbc77cf507
|
4
|
+
data.tar.gz: e2708147d1b78d1cea762a791b4fef81444dfd3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9201525c0082f3923197a37d7d865904dcd02e14e2140a23b3ddbd638e77ecd65abc44829917458559f6725db900b6537382a8fdd8857bd979b9c948db568306
|
7
|
+
data.tar.gz: 8e657d1ec3e72cf57bec4a0c8dad89b34d1a7a756e2b4663fe66c21691fd3f78a85412ac1e542ea1fd8d54769a9d0470187c0e7f5c2e1255d5729a78e28b18e1
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--color
|
1
|
+
--color
|
2
2
|
--format Fuubar
|
data/CONTRIBUTING.md
CHANGED
@@ -1,9 +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
|
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/Gemfile
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
source "http://rubygems.org"
|
2
|
-
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
3
|
gemspec
|
data/lib/open-weather-api/api.rb
CHANGED
@@ -1,57 +1,57 @@
|
|
1
|
-
module OpenWeatherAPI
|
2
|
-
class API
|
3
|
-
|
4
|
-
VERSION = "2.5"
|
5
|
-
|
6
|
-
attr_accessor :api_key, :default_language, :default_country_code, :default_units
|
7
|
-
|
8
|
-
def initialize(options = {})
|
9
|
-
@api_key = options[:api_key] || options['api_key']
|
10
|
-
@default_language = options[:default_language] || options['default_language'] || 'en'
|
11
|
-
@default_country_code = options[:default_country_code] || options['default_country_code']
|
12
|
-
@default_units = options[:default_units] || options['default_units'] || 'metric'
|
13
|
-
end
|
14
|
-
|
15
|
-
def current(**args, &block)
|
16
|
-
fetch_current.execute(**args, &block)
|
17
|
-
end
|
18
|
-
|
19
|
-
def forecast(type = :hourly, **args, &block)
|
20
|
-
raise "Invalid '#type' forecast type" unless valid_forecast_type?(type)
|
21
|
-
|
22
|
-
self.send("fetch_forecast_#{type}").execute(**args, &block)
|
23
|
-
end
|
24
|
-
|
25
|
-
def raw(path = "/", **args, &block)
|
26
|
-
fetch_raw.execute(path, **args, &block)
|
27
|
-
end
|
28
|
-
|
29
|
-
def icon_url(icon_code)
|
30
|
-
"http://openweathermap.org/img/w/#{icon_code}.png"
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
VALID_FORECAST_TYPES = [:hourly, :daily]
|
36
|
-
|
37
|
-
def valid_forecast_type?(type)
|
38
|
-
VALID_FORECAST_TYPES.include? type.to_sym
|
39
|
-
end
|
40
|
-
|
41
|
-
def fetch_raw
|
42
|
-
@current ||= Resources::Raw.new self
|
43
|
-
end
|
44
|
-
|
45
|
-
def fetch_current
|
46
|
-
@current ||= Resources::Current.new self
|
47
|
-
end
|
48
|
-
|
49
|
-
def fetch_forecast_hourly
|
50
|
-
@forecast_hourly ||= Resources::ForecastHourly.new self
|
51
|
-
end
|
52
|
-
|
53
|
-
def fetch_forecast_daily
|
54
|
-
@forecast_daily ||= Resources::ForecastHourly.new self
|
55
|
-
end
|
56
|
-
end
|
1
|
+
module OpenWeatherAPI
|
2
|
+
class API
|
3
|
+
|
4
|
+
VERSION = "2.5"
|
5
|
+
|
6
|
+
attr_accessor :api_key, :default_language, :default_country_code, :default_units
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@api_key = options[:api_key] || options['api_key']
|
10
|
+
@default_language = options[:default_language] || options['default_language'] || 'en'
|
11
|
+
@default_country_code = options[:default_country_code] || options['default_country_code']
|
12
|
+
@default_units = options[:default_units] || options['default_units'] || 'metric'
|
13
|
+
end
|
14
|
+
|
15
|
+
def current(**args, &block)
|
16
|
+
fetch_current.execute(**args, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def forecast(type = :hourly, **args, &block)
|
20
|
+
raise "Invalid '#type' forecast type" unless valid_forecast_type?(type)
|
21
|
+
|
22
|
+
self.send("fetch_forecast_#{type}").execute(**args, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def raw(path = "/", **args, &block)
|
26
|
+
fetch_raw.execute(path, **args, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def icon_url(icon_code)
|
30
|
+
"http://openweathermap.org/img/w/#{icon_code}.png"
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
VALID_FORECAST_TYPES = [:hourly, :daily]
|
36
|
+
|
37
|
+
def valid_forecast_type?(type)
|
38
|
+
VALID_FORECAST_TYPES.include? type.to_sym
|
39
|
+
end
|
40
|
+
|
41
|
+
def fetch_raw
|
42
|
+
@current ||= Resources::Raw.new self
|
43
|
+
end
|
44
|
+
|
45
|
+
def fetch_current
|
46
|
+
@current ||= Resources::Current.new self
|
47
|
+
end
|
48
|
+
|
49
|
+
def fetch_forecast_hourly
|
50
|
+
@forecast_hourly ||= Resources::ForecastHourly.new self
|
51
|
+
end
|
52
|
+
|
53
|
+
def fetch_forecast_daily
|
54
|
+
@forecast_daily ||= Resources::ForecastHourly.new self
|
55
|
+
end
|
56
|
+
end
|
57
57
|
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
module OpenWeatherAPI
|
2
|
-
def self.configure(&block)
|
3
|
-
raise ArgumentError, 'No block was given.' unless block
|
4
|
-
|
5
|
-
api = Rails.configuration.open_weather_api = OpenWeatherAPI::API.new
|
6
|
-
block.call(api)
|
7
|
-
api
|
8
|
-
end
|
1
|
+
module OpenWeatherAPI
|
2
|
+
def self.configure(&block)
|
3
|
+
raise ArgumentError, 'No block was given.' unless block
|
4
|
+
|
5
|
+
api = Rails.configuration.open_weather_api = OpenWeatherAPI::API.new
|
6
|
+
block.call(api)
|
7
|
+
api
|
8
|
+
end
|
9
9
|
end
|
@@ -1,67 +1,67 @@
|
|
1
|
-
module OpenWeatherAPI
|
2
|
-
module Resources
|
3
|
-
class Base
|
4
|
-
|
5
|
-
attr_reader :api_obj
|
6
|
-
|
7
|
-
def initialize(api_obj)
|
8
|
-
@api_obj = api_obj
|
9
|
-
end
|
10
|
-
|
11
|
-
def execute(**hash, &block)
|
12
|
-
@parameters = hash
|
13
|
-
setup_indifferent_access(@parameters)
|
14
|
-
|
15
|
-
# Let's use json
|
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
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def mode
|
30
|
-
(@parameters[:mode] || :json).to_s.to_sym
|
31
|
-
end
|
32
|
-
|
33
|
-
def handle_response_raw(response)
|
34
|
-
response
|
35
|
-
end
|
36
|
-
|
37
|
-
def handle_response_json(response)
|
38
|
-
json = JSON.parse(response.body)
|
39
|
-
setup_indifferent_access(json)
|
40
|
-
end
|
41
|
-
|
42
|
-
def handle_response_xml(response)
|
43
|
-
response.body
|
44
|
-
end
|
45
|
-
|
46
|
-
def handle_response_html(response)
|
47
|
-
response.body
|
48
|
-
end
|
49
|
-
|
50
|
-
def base_url
|
51
|
-
"http://api.openweathermap.org/data/#{API::VERSION || '2.5'}/"
|
52
|
-
end
|
53
|
-
|
54
|
-
def setup_indifferent_access(sub_hash)
|
55
|
-
sub_hash.default_proc = proc{|h, k| h.key?(k.to_s) ? h[k.to_s] : nil}
|
56
|
-
sub_hash.each { |k, v| setup_indifferent_access(v) if v.is_a?(Hash) }
|
57
|
-
end
|
58
|
-
|
59
|
-
def build_params(parameters = {})
|
60
|
-
{
|
61
|
-
APPID: @api_obj.api_key,
|
62
|
-
lang: @api_obj.default_language
|
63
|
-
}.merge(parameters)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
1
|
+
module OpenWeatherAPI
|
2
|
+
module Resources
|
3
|
+
class Base
|
4
|
+
|
5
|
+
attr_reader :api_obj
|
6
|
+
|
7
|
+
def initialize(api_obj)
|
8
|
+
@api_obj = api_obj
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute(**hash, &block)
|
12
|
+
@parameters = hash
|
13
|
+
setup_indifferent_access(@parameters)
|
14
|
+
|
15
|
+
# Let's use json
|
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
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def mode
|
30
|
+
(@parameters[:mode] || :json).to_s.to_sym
|
31
|
+
end
|
32
|
+
|
33
|
+
def handle_response_raw(response)
|
34
|
+
response
|
35
|
+
end
|
36
|
+
|
37
|
+
def handle_response_json(response)
|
38
|
+
json = JSON.parse(response.body)
|
39
|
+
setup_indifferent_access(json)
|
40
|
+
end
|
41
|
+
|
42
|
+
def handle_response_xml(response)
|
43
|
+
response.body
|
44
|
+
end
|
45
|
+
|
46
|
+
def handle_response_html(response)
|
47
|
+
response.body
|
48
|
+
end
|
49
|
+
|
50
|
+
def base_url
|
51
|
+
"http://api.openweathermap.org/data/#{API::VERSION || '2.5'}/"
|
52
|
+
end
|
53
|
+
|
54
|
+
def setup_indifferent_access(sub_hash)
|
55
|
+
sub_hash.default_proc = proc{|h, k| h.key?(k.to_s) ? h[k.to_s] : nil}
|
56
|
+
sub_hash.each { |k, v| setup_indifferent_access(v) if v.is_a?(Hash) }
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_params(parameters = {})
|
60
|
+
{
|
61
|
+
APPID: @api_obj.api_key,
|
62
|
+
lang: @api_obj.default_language
|
63
|
+
}.merge(parameters)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
67
|
end
|
@@ -1,45 +1,45 @@
|
|
1
|
-
module OpenWeatherAPI
|
2
|
-
module Resources
|
3
|
-
class Current < Base
|
4
|
-
|
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?
|
10
|
-
end
|
11
|
-
|
12
|
-
def build_params(parameters = {})
|
13
|
-
super [city, city_id, geolocation, zipcode, bbox, circle].each{ |h| break h.handle if h.can? }
|
14
|
-
end
|
15
|
-
|
16
|
-
# Simple handlers
|
17
|
-
def city
|
18
|
-
Handlers::City.new @api_obj, @parameters
|
19
|
-
end
|
20
|
-
|
21
|
-
def city_id
|
22
|
-
Handlers::CityID.new @api_obj, @parameters
|
23
|
-
end
|
24
|
-
|
25
|
-
def geolocation
|
26
|
-
Handlers::Geolocation.new @api_obj, @parameters
|
27
|
-
end
|
28
|
-
|
29
|
-
def zipcode
|
30
|
-
Handlers::Zipcode.new @api_obj, @parameters
|
31
|
-
end
|
32
|
-
|
33
|
-
# Other handlers
|
34
|
-
# ------------------
|
35
|
-
def bbox
|
36
|
-
Handlers::BoundingBox.new @api_obj, @parameters
|
37
|
-
end
|
38
|
-
|
39
|
-
def circle
|
40
|
-
Handlers::Circle.new @api_obj, @parameters
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
1
|
+
module OpenWeatherAPI
|
2
|
+
module Resources
|
3
|
+
class Current < Base
|
4
|
+
|
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?
|
10
|
+
end
|
11
|
+
|
12
|
+
def build_params(parameters = {})
|
13
|
+
super [city, city_id, geolocation, zipcode, bbox, circle].each{ |h| break h.handle if h.can? }
|
14
|
+
end
|
15
|
+
|
16
|
+
# Simple handlers
|
17
|
+
def city
|
18
|
+
Handlers::City.new @api_obj, @parameters
|
19
|
+
end
|
20
|
+
|
21
|
+
def city_id
|
22
|
+
Handlers::CityID.new @api_obj, @parameters
|
23
|
+
end
|
24
|
+
|
25
|
+
def geolocation
|
26
|
+
Handlers::Geolocation.new @api_obj, @parameters
|
27
|
+
end
|
28
|
+
|
29
|
+
def zipcode
|
30
|
+
Handlers::Zipcode.new @api_obj, @parameters
|
31
|
+
end
|
32
|
+
|
33
|
+
# Other handlers
|
34
|
+
# ------------------
|
35
|
+
def bbox
|
36
|
+
Handlers::BoundingBox.new @api_obj, @parameters
|
37
|
+
end
|
38
|
+
|
39
|
+
def circle
|
40
|
+
Handlers::Circle.new @api_obj, @parameters
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
45
|
end
|
@@ -1,19 +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
|
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
19
|
end
|
@@ -1,28 +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
|
-
Handlers::City.new @api_obj, @parameters
|
16
|
-
end
|
17
|
-
|
18
|
-
def city_id
|
19
|
-
Handlers::CityID.new @api_obj, @parameters
|
20
|
-
end
|
21
|
-
|
22
|
-
def geolocation
|
23
|
-
Handlers::Geolocation.new @api_obj, @parameters
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
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
|
+
Handlers::City.new @api_obj, @parameters
|
16
|
+
end
|
17
|
+
|
18
|
+
def city_id
|
19
|
+
Handlers::CityID.new @api_obj, @parameters
|
20
|
+
end
|
21
|
+
|
22
|
+
def geolocation
|
23
|
+
Handlers::Geolocation.new @api_obj, @parameters
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
28
|
end
|