forecast 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ require 'net/http'
2
+ require 'rexml/document'
3
+ require 'open-uri'
4
+ require 'json'
5
+
6
+ class Forecast
7
+ module Utils
8
+ class << self
9
+
10
+ def get_json(url, params)
11
+ query_string = URI.encode_www_form(params)
12
+ url = url + "?" + query_string
13
+ resp = Net::HTTP.get_response(URI.parse(url))
14
+ data = resp.body
15
+ result = JSON.parse(data)
16
+ if result && result['cod'] != "404"
17
+ return result
18
+ end
19
+ return nil
20
+ end
21
+
22
+ def get_doc(url, params)
23
+ query_string = URI.encode_www_form(params)
24
+ url = url + "?" + query_string
25
+ xml_data = Net::HTTP.get_response(URI.parse(url)).body
26
+ doc = REXML::Document.new(xml_data)
27
+ return doc
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ class Forecast
2
+ VERSION = "0.0.2"
3
+ end
data/lib/forecast.rb ADDED
@@ -0,0 +1,134 @@
1
+ require "forecast/version"
2
+ require "forecast/config"
3
+ require "forecast/model"
4
+ require "forecast/collection"
5
+ require "forecast/adapter"
6
+ require "forecast/adapters/yahoo_adapter"
7
+ require "forecast/adapters/open_weather_map_adapter"
8
+ require "forecast/adapters/wunderground_adapter"
9
+ require "yaml"
10
+ require "redis"
11
+
12
+ class Forecast
13
+
14
+ # instance
15
+
16
+ include Forecast::Model
17
+
18
+ attr_accessor :latitude, :longitude, :date, :temp, :temp_min, :temp_max, :condition, :orig_condition
19
+
20
+ # class
21
+ class << self
22
+
23
+ def current(latitude, longitude)
24
+ cache_key = "current:#{latitude},#{longitude}"
25
+ forecast = read_cache(cache_key)
26
+ if forecast == nil
27
+ forecast = adapter.current(latitude, longitude)
28
+ write_cache(cache_key, forecast)
29
+ end
30
+ return forecast
31
+ end
32
+
33
+ def hourly(latitude, longitude)
34
+ cache_key = "hourly:#{latitude},#{longitude}"
35
+ forecasts = read_cache(cache_key)
36
+ if forecasts == nil
37
+ forecasts = adapter.hourly(latitude, longitude)
38
+ write_cache(cache_key, forecasts)
39
+ end
40
+ return forecasts
41
+ end
42
+
43
+ def daily(latitude, longitude)
44
+ cache_key = "daily:#{latitude},#{longitude}"
45
+ forecasts = read_cache(cache_key)
46
+ if forecasts == nil
47
+ forecasts = adapter.daily(latitude, longitude)
48
+ write_cache(cache_key, forecasts)
49
+ end
50
+ return forecasts
51
+ end
52
+
53
+ private
54
+
55
+ @adapter = nil
56
+
57
+ def adapter
58
+ if @adapter == nil
59
+ @adapter = Forecast::Adapter.instance
60
+ end
61
+ return @adapter
62
+ end
63
+
64
+
65
+ @cache = nil
66
+
67
+ def cache
68
+ cache = Forecast.config.cache
69
+ if @cache == nil && ( cache != nil || (!!cache == cache) && cache )
70
+ if !!cache == cache
71
+ Forecast.config.cache = {
72
+ expire: 5,
73
+ prefix: :forecast,
74
+ host: "127.0.0.1",
75
+ port: "6379"
76
+ }
77
+ end
78
+ begin
79
+ @cache = Redis.new(host: Forecast.config.cache['host'], port: Forecast.config.cache['port'])
80
+ @cache.ping
81
+ rescue
82
+ puts "error connecting to redis"
83
+ end
84
+ end
85
+ return @cache
86
+ end
87
+
88
+ def cache_key(key)
89
+ cache_prefix = Forecast.config.cache[:prefix]
90
+ return "#{cache_prefix.to_s}:#{key}"
91
+ end
92
+
93
+ def cache_expire
94
+ cache_expire = Forecast.config.cache[:expire]
95
+ end
96
+
97
+ def write_cache(key, data)
98
+ if cache == nil
99
+ return;
100
+ end
101
+ puts "WRITE TO CACHE... " + cache_key(key).to_s + ", cache_expire: " + cache_expire.to_s
102
+ cache.set(cache_key(key), data.to_json)
103
+ cache.expire(cache_key(key), cache_expire)
104
+ end
105
+
106
+ def read_cache(key)
107
+ if cache == nil
108
+ return nil;
109
+ end
110
+ cached_result = cache.get(cache_key(key))
111
+ result = nil
112
+ if cached_result != nil
113
+ puts "READ FROM CACHE: " + cache_key(key).to_s + ", cache_expire: " + cache_expire.to_s
114
+ json = JSON.parse(cached_result)
115
+ if json.is_a?(Array)
116
+ result = Forecast::Collection.new
117
+ json.each do |obj|
118
+ forecast = Forecast.new
119
+ forecast.from_json(obj)
120
+ result << forecast
121
+ end
122
+ elsif json.is_a?(Object)
123
+ result = Forecast.new
124
+ result.from_json(json)
125
+ end
126
+ end
127
+ return result
128
+ end
129
+
130
+
131
+
132
+ end
133
+
134
+ end
@@ -0,0 +1,41 @@
1
+ require 'forecast'
2
+ describe Forecast do
3
+
4
+ def dump_forecast(forecast)
5
+ if forecast != nil
6
+ puts forecast.date.strftime("%F %T") + " | " + forecast.temp.to_s + " | " + forecast.condition + " | " + forecast.icon
7
+ else
8
+ puts 'nil'
9
+ end
10
+ end
11
+
12
+ # it "current" do
13
+ # puts "*** CURRENT ***"
14
+ # forecast = Forecast.current(54.9999, 9.534)
15
+ # dump_forecast(forecast)
16
+ # expect(forecast).not_to be_nil
17
+ # end
18
+ # it "hourly" do
19
+ # puts "*** HOURLY FORECASTS ***"
20
+ # forecasts = Forecast.hourly(54.9999, 9.534)
21
+ # forecasts.each do |forecast|
22
+ # dump_forecast(forecast)
23
+ # end
24
+ # expect(forecasts.size).to be >= 1
25
+ # end
26
+ # it "daily" do
27
+ # puts "*** DAILY FORECASTS ***"
28
+ # forecasts = Forecast.daily(54.9999, 9.534)
29
+ # forecasts.each do |forecast|
30
+ # #puts forecast.date.strftime("%F %T") + " | " + forecast.temp_min.to_s + " | " + forecast.temp_max.to_s + " | " + forecast.condition + " | " + forecast.icon
31
+ # dump_forecast(forecast)
32
+ # end
33
+ # expect(forecasts.size).to be >= 1
34
+ # end
35
+ it "select_date" do
36
+ puts "*** SELECT FORECAST ***"
37
+ forecast = Forecast.daily(54.9999, 9.534).select_date(Time.now + (24*60*60) * 2)
38
+ dump_forecast(forecast)
39
+ expect(forecast).not_to be_nil
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: forecast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Rafael Nowrotek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.6'
69
+ description: A Forecast-Multi-API-Wrapper with a unified model and integrated caching
70
+ email:
71
+ - mail@benignware.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .README.md.html
77
+ - .gitignore
78
+ - .project
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - dump.rdb
84
+ - forecast.gemspec
85
+ - lib/forecast.rb
86
+ - lib/forecast/adapter.rb
87
+ - lib/forecast/adapters/open_weather_map_adapter.rb
88
+ - lib/forecast/adapters/open_weather_map_adapter.yml
89
+ - lib/forecast/adapters/wunderground_adapter.rb
90
+ - lib/forecast/adapters/yahoo_adapter.rb
91
+ - lib/forecast/cache.rb
92
+ - lib/forecast/collection.rb
93
+ - lib/forecast/conditions.yml
94
+ - lib/forecast/config.rb
95
+ - lib/forecast/config.yml
96
+ - lib/forecast/model.rb
97
+ - lib/forecast/themes/weather_icons.yml
98
+ - lib/forecast/utils.rb
99
+ - lib/forecast/version.rb
100
+ - spec/forecast_spec.rb
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.1.11
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: A Forecast-Multi-API-Wrapper with a unified model and integrated caching
125
+ test_files:
126
+ - spec/forecast_spec.rb