OpenWeather 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: 04848fe1578b21c27a4254fad19d41a04a58bc38
4
+ data.tar.gz: 473287b838114e720b68d7cfebc976d1895aa7b7
5
+ SHA512:
6
+ metadata.gz: 1f0e62dd9999d64457f0541ccac32178b74c5f4eb4fd67b8a240d89ef94182b9d2da798aba999439f2d8120bfd679eaf52aab35cb1885c5a557070debf58ae08
7
+ data.tar.gz: b05d78a68a6dc70cd05c608fe6793616f9cac440348f45973d8c3fedfc99a6a01cdd39e1cb01b94c08a3a0fbfddd8911a98f9302c83093ee533cd78163a1b3d5
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ # RubyMine
20
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in OpenWeather.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris911
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.
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'OpenWeather/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "OpenWeather"
8
+ spec.version = OpenWeather::VERSION
9
+ spec.authors = ["Chris911"]
10
+ spec.email = ["Christophe.naud.dulude@gmail.com"]
11
+ spec.description = "Ruby wrapper for Open Weather Map - http://openweathermap.org/"
12
+ spec.summary = "Ruby wrapper for Open Weather Map - http://openweathermap.org/"
13
+ spec.homepage = "https://github.com/Chris911/OpenWeather"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_runtime_dependency "httparty"
26
+ spec.add_runtime_dependency "json"
27
+ end
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # OpenWeather
2
+
3
+ Ruby wrapper for Open Weather Map - http://openweathermap.org/
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'OpenWeather'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install OpenWeather
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,40 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ require "OpenWeather/version"
5
+ require "OpenWeather/forecast"
6
+ require "OpenWeather/weather"
7
+ require "OpenWeather/utilities"
8
+ require "OpenWeather/exceptions"
9
+ require "OpenWeather/conditions"
10
+
11
+ module OpenWeather
12
+ class Client
13
+ include HTTParty
14
+
15
+ [Forecast, Weather, Utilities].each do |inc|
16
+ include inc
17
+ end
18
+
19
+ attr_accessor(:units_format, :data_format)
20
+
21
+ # Open Weather Map Client
22
+ #
23
+ # @param opts [Hash] A hash of options
24
+ # @option opts [String] :url The Open Weather Map API URL.
25
+ # @option opts [String] :useragent Useragent used for the request
26
+ # @option opts [String] :api_key Your API key if you have one
27
+ def initialize( opts = {} )
28
+ options = {url: "http://api.openweathermap.org/data/2.5", useragent: "Open Weather Ruby v#{VERSION}"}.merge opts
29
+ @baseurl = options[:url]
30
+ @headers = {'User-Agent' => options[:useragent]}
31
+ # For HTTParty
32
+ self.class.base_uri options[:url]
33
+ self.class.headers @headers
34
+
35
+ @units_format = 'metric'
36
+ @data_format = 'json'
37
+ @api_key = options[:api_key] if options[:api_key]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ module OpenWeather
2
+ CONDITIONS = {
3
+ 200 => "Thunderstorm",
4
+ 300 => "Drizzle",
5
+ 500 => "Rain",
6
+ 600 => "Snow",
7
+ 700 => "Atmoshere",
8
+ 800 => "Clouds",
9
+ 900 => "Extreme"
10
+ }
11
+ end
@@ -0,0 +1,7 @@
1
+ module OpenWeather
2
+
3
+ # Thrown whenever the webserver returns anything but 200
4
+ #
5
+ class WebserverError < StandardError
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ module OpenWeather
2
+ module Forecast
3
+ def forecast_raw(city)
4
+ get('/forecast', query: { q: city, units: @units_format, mode: @data_format })
5
+ end
6
+
7
+ def forecast_tomorrow_min(city)
8
+ forecast = forecast_raw(city)
9
+ tomorrow_list = forecast['list'].select { |weather| weather['dt_txt'].split(" ")[0] == tomorrow }
10
+ tomorrow_list.min_by { |temp| temp['main']['temp_min'] }['main']['temp_min']
11
+ end
12
+
13
+ def forecast_tomorrow_max(city)
14
+ forecast = forecast_raw(city)
15
+ tomorrow_list = forecast['list'].select { |weather| weather['dt_txt'].split(" ")[0] == tomorrow }
16
+ tomorrow_list.max_by { |temp| temp['main']['temp_max'] }['main']['temp_max']
17
+ end
18
+
19
+ def forecast_tomorrow_rain?(city)
20
+ forecast = forecast_raw(city)
21
+ tomorrow_list = forecast['list'].select { |weather| weather['dt_txt'].split(" ")[0] == tomorrow }
22
+ # This selects any period of time during the day where the condition code is between 500 and 599 which
23
+ # corresponds to rainy condition. If the array is empty that means it won't rain so we return the opposite.
24
+ ! tomorrow_list.select { |code| Integer(code['weather'][0]['id']).between?(500, 599) }.empty?
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,34 @@
1
+ module OpenWeather
2
+ # Utility functions.
3
+ #
4
+ # @author Mostly https://github.com/paradox460/snoo
5
+ module Utilities
6
+
7
+ def today
8
+ time = Time.new
9
+ time.strftime("%Y-%m-%d")
10
+ end
11
+
12
+ def tomorrow
13
+ time = Time.now + (60 * 60 * 24)
14
+ time.strftime("%Y-%m-%d")
15
+ end
16
+
17
+ private
18
+ # HTTParty get wrapper. This serves to clean up code, as well as throw webserver errors wherever needed
19
+ #
20
+ def get *args, &block
21
+ response = self.class.get *args, &block
22
+ raise WebserverError, response.code unless response.code == 200
23
+ response
24
+ end
25
+
26
+ # HTTParty POST wrapper. This serves to clean up code, as well as throw webserver errors wherever needed
27
+ #
28
+ def post *args, &block
29
+ response = self.class.post *args, &block
30
+ raise WebserverError, response.code unless response.code == 200
31
+ response
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module OpenWeather
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ module OpenWeather
2
+ module Weather
3
+ def weather_raw(city)
4
+ get('/weather', query: { q: city, units: @units_format, mode: @data_format })
5
+ end
6
+
7
+ def weather_min(city)
8
+ weather = weather_raw(city)
9
+ weather['main']['temp_min']
10
+ end
11
+
12
+ def weather_max(city)
13
+ weather = weather_raw(city)
14
+ weather['main']['temp_max']
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ require "OpenWeather"
2
+ require "rspec"
3
+
4
+ describe "Fetch Forecast" do
5
+
6
+ it "should get proper coordinates" do
7
+ owc = OpenWeather::Client.new
8
+ forecast = owc.forecast_raw "Montreal"
9
+ forecast['city']['coord']['lon'].should == -73.587807
10
+ forecast['city']['coord']['lat'].should == 45.508839
11
+ end
12
+
13
+ it "min should be smaller than max" do
14
+ owc = OpenWeather::Client.new
15
+ min = owc.forecast_tomorrow_min "Montreal"
16
+ max = owc.forecast_tomorrow_max "Montreal"
17
+ min.should <= max
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ require "rspec"
2
+ require "OpenWeather"
3
+ require "json"
4
+
5
+ describe "Fetch Weather" do
6
+
7
+ it "should get proper coordinates" do
8
+ owc = OpenWeather::Client.new
9
+ weather = owc.weather_raw "Montreal"
10
+ weather['coord']['lon'].should == -73.587807
11
+ weather['coord']['lat'].should == 45.508839
12
+ end
13
+
14
+ it "should return unknown city" do
15
+ owc = OpenWeather::Client.new
16
+ weather = owc.weather_raw "FakeCity123"
17
+ weather['message'].should == "Error: Not found city"
18
+ weather['cod'].should == "404"
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: OpenWeather
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris911
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Ruby wrapper for Open Weather Map - http://openweathermap.org/
84
+ email:
85
+ - Christophe.naud.dulude@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - OpenWeather.gemspec
94
+ - README.md
95
+ - Rakefile
96
+ - lib/OpenWeather.rb
97
+ - lib/OpenWeather/conditions.rb
98
+ - lib/OpenWeather/exceptions.rb
99
+ - lib/OpenWeather/forecast.rb
100
+ - lib/OpenWeather/utilities.rb
101
+ - lib/OpenWeather/version.rb
102
+ - lib/OpenWeather/weather.rb
103
+ - tests/forecast_spec.rb
104
+ - tests/weather_spec.rb
105
+ homepage: https://github.com/Chris911/OpenWeather
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.0.3
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Ruby wrapper for Open Weather Map - http://openweathermap.org/
129
+ test_files: []