open_weather_service 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 262c31c6038f07cbef2181192ae4d63b62f254a3
4
+ data.tar.gz: afe215cfd77d9e2122f4fdc3224401f34963b072
5
+ SHA512:
6
+ metadata.gz: a716e447a886e91764f500714dd3a644d41cf1cdbb92516b7e80467745f21cd914434d9bb51d1a52ca5616def7e2b3e82b75e40f82dd9e97997aaa39a4ff12e8
7
+ data.tar.gz: 3cb5d7378ebefeab93bc209f1aa357c9af3170d07ac22ef4968ccab0d27fb6e03de3bdbbb7393700c619731270347179616f850edc4d27d79bd0fa96bd62f6eb
@@ -0,0 +1,62 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ module OpenWeather
4
+ # current weather
5
+ class Current
6
+
7
+ URL = "http://api.openweathermap.org/data/2.5/weather?q=#{CITY},#{COUNTRY}"
8
+
9
+ def self.weather
10
+ if Manager.can_consume?
11
+ @weather = load_weather
12
+ @weather
13
+ else
14
+ @weather
15
+ end
16
+ end
17
+
18
+ def self.speed
19
+ weather if weather.nil?
20
+ weather.speed
21
+ end
22
+
23
+ def self.humidity
24
+ weather if weather.nil?
25
+ weather.humidity
26
+ end
27
+
28
+ def self.temp
29
+ weather if weather.nil?
30
+ weather.temp
31
+ end
32
+
33
+ def self.temp_min
34
+ weather if weather.nil?
35
+ weather.temp_min
36
+ end
37
+
38
+ def self.temp_max
39
+ weather if weather.nil?
40
+ weather.temp_max
41
+ end
42
+
43
+ def self.icon
44
+ weather if weather.nil?
45
+ weather.icon
46
+ end
47
+
48
+ def self.load_weather
49
+ response = RestClient.get url
50
+ if response.code == 200
51
+ json = JSON.parse response.body
52
+ @weather = Weather.new(json)
53
+ else
54
+ @weather = Weather.new
55
+ end
56
+ end
57
+
58
+ def self.url
59
+ URL + "&appid=#{API_KEY}"
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,28 @@
1
+ module OpenWeather
2
+ class Forecast
3
+ URL = "http://api.openweathermap.org/data/2.5/forecast?q=#{CITY},#{COUNTRY}&mode=json"
4
+
5
+ def self.weathers
6
+ if Manager.can_consume?
7
+ @weathers = load_weather
8
+ @weathers
9
+ else
10
+ @weathers
11
+ end
12
+ end
13
+
14
+ def self.load_weather
15
+ response = RestClient.get url
16
+ if response.code == 200
17
+ json = JSON.parse response.body
18
+ @weathers = json['list'].collect { |j| Weather.new(j) }
19
+ else
20
+ @weathers = []
21
+ end
22
+ end
23
+
24
+ def self.url
25
+ URL + "&appid=#{API_KEY}"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,37 @@
1
+ module OpenWeather
2
+ # Manage the number of calls to the api
3
+ class Manager
4
+ LIMIT_CALLS = 59 # free plan
5
+ LIMIT_SECONDS = 60
6
+
7
+ @calls = 0
8
+ @last_call_at = Time.now
9
+
10
+ def self.can_consume?
11
+ if @calls < LIMIT_CALLS
12
+ call
13
+ true
14
+ elsif valid_time?
15
+ reset
16
+ true
17
+ else
18
+ # puts (Time.now - @last_call_at)
19
+ false
20
+ end
21
+ end
22
+
23
+ def self.call
24
+ @calls += 1
25
+ end
26
+
27
+ def self.reset
28
+ # puts "Reset..."
29
+ @calls = 0
30
+ @last_call_at = Time.now
31
+ end
32
+
33
+ def self.valid_time?
34
+ (Time.now - @last_call_at) >= LIMIT_SECONDS
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ module OpenWeather
2
+ #API_KEY = ENV['API_KEY']
3
+ API_KEY = '29583c9be58c2a1b546d2cadc4c193b5'
4
+ CITY = 'Atlanta'
5
+ COUNTRY = 'us'
6
+ end
7
+
8
+ require_relative 'manager'
9
+ require_relative 'current'
10
+ require_relative 'weather'
11
+ require_relative 'forecast'
@@ -0,0 +1,22 @@
1
+ module OpenWeather
2
+ class Weather
3
+ attr_reader :speed, :humidity, :temp, :temp_min, :temp_max, :icon, :date
4
+
5
+ def initialize(options = {})
6
+ main = options['main']
7
+ @speed = options['wind']['speed']
8
+ @humidity = main['humidity']
9
+ @temp = to_fahrenheit(main['temp'])
10
+ @temp_min = to_fahrenheit(main['temp_min'])
11
+ @temp_max = to_fahrenheit(main['temp_max'])
12
+ @icon = options['weather'][0]['main']
13
+ @date = options['dt_txt'] || Time.now
14
+ end
15
+
16
+ private
17
+
18
+ def to_fahrenheit(x)
19
+ { kelvin: x, fahrenheit: x * (9 / 5.0) - 459.67 }
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_weather_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Andres Torres Cruz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Allows you to get the current weather and the forecast of the current
14
+ week
15
+ email: carlosandrestorres28@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/current.rb
21
+ - lib/forecast.rb
22
+ - lib/manager.rb
23
+ - lib/open_weather.rb
24
+ - lib/weather.rb
25
+ homepage: https://github.com/katorres02/open_weather_client_ruby
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.4.6
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: REST client for open_weather service
49
+ test_files: []