open-weather-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 49dda5814718454abaf2b857407b17e071a19f6d
4
+ data.tar.gz: b3037f267453d565228cf4357fcb01923b31f359
5
+ SHA512:
6
+ metadata.gz: 1e980c13e0c064cc4861093438275f131abc1a30ad591f687ab272fef2a6d26d14a7bebc8aeebfb55b38a77b70bdb4f499c182e1ff6275742b8856a3c4ca0ddb
7
+ data.tar.gz: c3b5a7c89c8cbc4dd052fbb5e46627c841e9b87d66e5e0b214302c7f3048b57e2e7cc1aeb8b90c88123dd27df320ddab1a77f58c3a860c6753cd94f9550c8741
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
17
+ build/
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Wikiti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # OpenWeatherApi wrapper for Ruby
2
+
3
+ <div align="center">
4
+ ![](doc/icon.png)
5
+ <div align="left">
6
+
7
+ Simple wrapper for Open Weather Map API.
8
+
9
+ Please visit the this [link](http://openweathermap.org/api) for more information.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'open-weather-api'
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install open-weather-api
24
+
25
+ ## Usage
26
+
27
+ First, you need to init the API:
28
+
29
+ ### Rails
30
+
31
+ ```ruby
32
+ # config/initializers/open-weather-api.rb
33
+
34
+ Rails.configuration.open_weather_api = OpenWeatherAPI::API.new api_key: ENV['OPEN_WEATHER_API_KEY'], language: 'es', units: 'metric'
35
+ ```
36
+
37
+ `language`, `default_country_code` and `metrics` are optional values, with defaults `'en'`, `nil` and `'metric'`.
38
+
39
+ ### Other
40
+
41
+ ```ruby
42
+ open_weather_api = OpenWeatherAPI::API.new api_key: ENV['OPEN_WEATHER_API_KEY'], language: 'es', units: 'metric'
43
+ # ...
44
+ ```
45
+
46
+ ----------------------------
47
+
48
+ Finally, you can use the different resources of the API:
49
+
50
+ **NOTE**: You can add manually any parameter you need for each request.
51
+
52
+ ### Current Weather
53
+
54
+ By city name:
55
+
56
+ ````ruby
57
+ json = open_weather_api.current city: 'Santa Cruz de Tenerife', country_code: 'es'
58
+ ````
59
+
60
+ By city id:
61
+
62
+ ````ruby
63
+ json = open_weather_api.current id: 6360638
64
+ ````
65
+
66
+ By multiple cities ids:
67
+
68
+ ````ruby
69
+ json = open_weather_api.current id: [6360638, 2511401]
70
+ ````
71
+
72
+ By geolocation:
73
+
74
+ ````ruby
75
+ json = open_weather_api.current lon: -16.20302, lat: 28.53924
76
+ ````
77
+
78
+ By zipcode:
79
+
80
+ ````ruby
81
+ json = open_weather_api.current zipcode: 38190, country_code: 'es'
82
+ ````
83
+
84
+ By a geolocated rectangle:
85
+
86
+ ````ruby
87
+ json = open_weather_api.current rectangle: { topleft: { lat: -16.3319, lon: 28.5046 }, bottomright: { lat: -16.1972, lon: 28.4400}, zoom: 10 }
88
+ ````
89
+
90
+ By a geolocated circle (**WARNING**: Unexpected behaviour by API):
91
+
92
+ ````ruby
93
+ json = open_weather_api.current circle: { lat: -16.3319, lon: 28.5046 }, cities_count: 2
94
+ ````
95
+
96
+ You can also use ruby blocks to handle the response:
97
+
98
+ ````ruby
99
+ open_weather_api.current city: 'Santa Cruz de Tenerife', country_code: 'es' do |json|
100
+ puts JSON.pretty_generate(json)
101
+ end
102
+ ````
103
+
104
+ For more information about the API, visit (http://openweathermap.org/current)[http://openweathermap.org/current].
105
+
106
+ ### Forecast
107
+
108
+ TODO.
109
+
110
+ ## Authors ##
111
+
112
+ This project has been developed by:
113
+
114
+ | Avatar | Name | Nickname | Email |
115
+ | ------- | ------------- | --------- | ------------------ |
116
+ | ![](http://www.gravatar.com/avatar/2ae6d81e0605177ba9e17b19f54e6b6c.jpg?s=64) | Daniel Herzog | Wikiti | [wikiti.doghound@gmail.com](mailto:wikiti.doghound@gmail.com)
117
+
118
+ ## Contributing
119
+
120
+ 1. Fork it ( http://github.com/wikiti/open-weather-api/fork )
121
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
122
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
123
+ 4. Push to the branch (`git push origin my-new-feature`)
124
+ 5. Create new Pull Request
data/doc/icon.png ADDED
Binary file
@@ -0,0 +1,11 @@
1
+ require 'open-weather-api/version'
2
+
3
+ # Dependencies
4
+ require 'uri'
5
+ require 'rest-client'
6
+ require 'json'
7
+
8
+ # Main files
9
+ require 'open-weather-api/resources/base_resource'
10
+ require 'open-weather-api/resources/current'
11
+ require 'open-weather-api/api'
@@ -0,0 +1,50 @@
1
+ module OpenWeatherAPI
2
+ class API
3
+
4
+ attr_reader :api_key, :options
5
+ attr_accessor :default_language, :default_country_code, :default_units
6
+
7
+ def initialize(options = {})
8
+ @options = options
9
+ @api_key = options[:api_key] || options['api_key']
10
+ @default_language = options[:language] || options['language'] || 'en'
11
+ @default_country_code = options[:default_country_code] || options['default_country_code']
12
+ @default_units = options[:units] || options['units'] || 'metric'
13
+ end
14
+
15
+ def current(**args, &block)
16
+ fetch_current.execute(**args, &block)
17
+ end
18
+
19
+ # Not yet implemented
20
+ def forecast(type = :hourly, **args, &block)
21
+ raise ArgumentError, "Invalid #{type} forecast type."
22
+
23
+ self.send("fetch_forecast_#{type}").execute(**args, &block)
24
+ end
25
+
26
+ def icon_url(icon_code)
27
+ "http://openweathermap.org/img/w/#{icon_code}.png"
28
+ end
29
+
30
+ private
31
+
32
+ VALID_FORECAST_TYPES = [:hourly, :daily]
33
+
34
+ def valid_forecast_type?(type)
35
+ VALID_FORECAST_TYPES.include? type.to_sym
36
+ end
37
+
38
+ def fetch_current
39
+ @current ||= Resources::Current.new self
40
+ end
41
+
42
+ def fetch_forecast_hourly
43
+ @forecast_hourly ||= Resources::ForecastHourly.new self
44
+ end
45
+
46
+ def fetch_forecast_daily
47
+ @forecast_daily ||= Resources::ForecastHourly.new self
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,47 @@
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
+ response = request
16
+ raise "Invalid response." unless response.code == 200
17
+
18
+ json = JSON.parse(response.body)
19
+
20
+ return block.call(json) if block_given?
21
+ json
22
+ end
23
+
24
+ private
25
+
26
+ def request(type = :get)
27
+ RestClient.send type, base_url, params: build_params(@parameters), accept: :json
28
+ end
29
+
30
+ def base_url
31
+ 'http://api.openweathermap.org/data/2.5/'
32
+ end
33
+
34
+ def setup_indifferent_access(sub_hash)
35
+ sub_hash.default_proc = proc{|h, k| h.key?(k.to_s) ? h[k.to_s] : nil}
36
+ sub_hash.each { |k, v| setup_indifferent_access(v) if v.is_a?(Hash) }
37
+ end
38
+
39
+ def build_params(parameters = {})
40
+ {
41
+ APPID: @api_obj.api_key,
42
+ lang: @api_obj.default_language
43
+ }.merge(parameters)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,157 @@
1
+ module OpenWeatherAPI
2
+ module Resources
3
+ class Current < Base
4
+ private
5
+
6
+ def base_url
7
+ return super + 'group' if is_multiple_city_id?
8
+ return super + 'weather' if is_city? || is_city_id? || is_geolocation? || is_zipcode?
9
+ return super + 'box/city' if is_bbox?
10
+ return super + 'find' if is_circle?
11
+ end
12
+
13
+ def build_params(parameters = {})
14
+ super build_for_city || build_for_multiple || build_for_city_id || build_for_geolocation || build_for_zipcode || build_for_bbox || build_for_circle
15
+ end
16
+
17
+ # Basic city
18
+ # ------------------
19
+
20
+ # For city
21
+ def is_city?
22
+ city != nil
23
+ end
24
+
25
+ def city
26
+ @parameters[:city]
27
+ end
28
+
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
+ def city_id
47
+ @parameters[:id] || @parameters[:city_id]
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
61
+ end
62
+
63
+ 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
85
+ end
86
+
87
+ def zipcode
88
+ @parameters[:zipcode] || @parameters[:zip]
89
+ end
90
+
91
+ def build_for_zipcode
92
+ return { zip: [zipcode, country_code].compact.flatten.join(',') } if is_zipcode?
93
+ end
94
+
95
+ # Multiples cities
96
+ # ------------------
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
+ def bbox
124
+ @parameters[:box] || @parameters[:box] || @parameters[:rect] || @parameters[:rectangle]
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
135
+ end
136
+
137
+ # Circle
138
+ def is_circle?
139
+ circle != nil
140
+ end
141
+
142
+ def circle
143
+ @parameters[:circle]
144
+ end
145
+
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
+ end
156
+ end
157
+ end
@@ -0,0 +1,3 @@
1
+ module OpenWeatherApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'open-weather-api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "open-weather-api"
8
+ spec.version = OpenWeatherApi::VERSION
9
+ spec.authors = ["Wikiti"]
10
+ spec.email = ["wikiti.doghound@gmail.com"]
11
+ spec.summary = %q{Simple wrapper for Open Weather Map API}
12
+ spec.description = %q{Simple wrapper for Open Weather Map API. The API description may be found here: http://openweathermap.org/api}
13
+ spec.homepage = "https://gitlab.com/wikiti-random-stuff/open-weather-api"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "rest-client", "~> 1.8"
22
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open-weather-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Wikiti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ description: 'Simple wrapper for Open Weather Map API. The API description may be
28
+ found here: http://openweathermap.org/api'
29
+ email:
30
+ - wikiti.doghound@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - .gitignore
36
+ - LICENSE.txt
37
+ - README.md
38
+ - doc/icon.png
39
+ - lib/open-weather-api.rb
40
+ - lib/open-weather-api/api.rb
41
+ - lib/open-weather-api/resources/base_resource.rb
42
+ - lib/open-weather-api/resources/current.rb
43
+ - lib/open-weather-api/version.rb
44
+ - open-weather-api.gemspec
45
+ homepage: https://gitlab.com/wikiti-random-stuff/open-weather-api
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.0.14
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Simple wrapper for Open Weather Map API
69
+ test_files: []
70
+ has_rdoc: