open_meteo_client 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +7 -0
- data/lib/open_meteo_client.rb +41 -0
- data/lib/open_meteo_client/open_meteo_response.rb +39 -0
- data/lib/open_meteo_client/version.rb +3 -0
- data/open_meteo_client.gemspec +27 -0
- data/test/open_meteo_client/open_meteo_response_test.rb +76 -0
- metadata +154 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 jeanbaptistevilain
|
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,29 @@
|
|
1
|
+
# OpenMeteoClient
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'open_meteo_client'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install open_meteo_client
|
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,41 @@
|
|
1
|
+
require 'open_meteo_client/version'
|
2
|
+
require 'open_meteo_client/open_meteo_response'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
module OpenMeteoClient
|
7
|
+
|
8
|
+
DEFAULT_ATTRIBUTES = [:times, :temp, :rh, :low_clouds, :medium_clouds, :high_clouds, :precipitations]
|
9
|
+
|
10
|
+
# Configuration defaults
|
11
|
+
@config = {
|
12
|
+
:base_url => 'http://api.ometfn.net/0.1/forecast',
|
13
|
+
:domain => 'eu12',
|
14
|
+
:results_type => 'full',
|
15
|
+
:results_format => 'json',
|
16
|
+
:relevant_attributes => DEFAULT_ATTRIBUTES
|
17
|
+
}
|
18
|
+
|
19
|
+
@valid_config_keys = @config.keys
|
20
|
+
@logger = Logger.new(STDOUT)
|
21
|
+
|
22
|
+
# Configure through hash
|
23
|
+
def self.configure(opts = {})
|
24
|
+
opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.config
|
28
|
+
@config
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.forecasts(latitude, longitude)
|
32
|
+
response = OpenMeteoResponse.new(@config[:relevant_attributes])
|
33
|
+
forecasts_query = "#{@config[:base_url]}/#{@config[:domain]}/#{latitude},#{longitude}/#{@config[:results_type]}.#{@config[:results_format]}"
|
34
|
+
@logger.info "Forecasts query : #{forecasts_query}"
|
35
|
+
open(forecasts_query) { |f|
|
36
|
+
f.each_line {|line| response.append_line(line)}
|
37
|
+
}
|
38
|
+
response
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class OpenMeteoResponse
|
4
|
+
|
5
|
+
def initialize(relevant_attributes)
|
6
|
+
@json_response = ''
|
7
|
+
@relevant_attributes = relevant_attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
def append_line(line)
|
11
|
+
@json_response += line unless line.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def as_hash
|
15
|
+
complete_hash = JSON.parse @json_response, :symbolize_names => true
|
16
|
+
complete_hash.keep_if {|key, value| @relevant_attributes.include?(key)}
|
17
|
+
end
|
18
|
+
|
19
|
+
def as_raw_json
|
20
|
+
@json_response
|
21
|
+
end
|
22
|
+
|
23
|
+
def as_list
|
24
|
+
response_hash = as_hash
|
25
|
+
forecasts = []
|
26
|
+
|
27
|
+
response_hash.each_pair { |key, value|
|
28
|
+
if value.is_a?(Array)
|
29
|
+
value.each_with_index do |val, i|
|
30
|
+
forecasts[i] ||= {}
|
31
|
+
forecasts[i][key] = val
|
32
|
+
end
|
33
|
+
end
|
34
|
+
}
|
35
|
+
|
36
|
+
forecasts
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -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 'open_meteo_client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "open_meteo_client"
|
8
|
+
spec.version = OpenMeteoClient::VERSION
|
9
|
+
spec.authors = ["jeanbaptistevilain"]
|
10
|
+
spec.email = ["jbvilain@gmail.com"]
|
11
|
+
spec.description = "Open Meteo Forecasts client gem"
|
12
|
+
spec.summary = "A basic model implementation to enable open meteo forecasts data usage in RoR projects"
|
13
|
+
spec.homepage = "https://github.com/jeanbaptistevilain/open_meteo_client"
|
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.0"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "test-unit"
|
24
|
+
spec.add_development_dependency "shoulda"
|
25
|
+
spec.add_runtime_dependency "rails", "~> 3.2.14"
|
26
|
+
spec.add_runtime_dependency "json"
|
27
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'shoulda'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'shoulda'
|
5
|
+
require 'open_meteo_client/open_meteo_response'
|
6
|
+
|
7
|
+
class OpenMeteoResponseTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
JSON_RESPONSE =
|
10
|
+
'{' +
|
11
|
+
'"doc":"http://api.ometfn.net/0.1/forecast/doc",' +
|
12
|
+
'"license":"http://api.ometfn.net/0.1/forecast/license",' +
|
13
|
+
'"domain":"eu12",' +
|
14
|
+
'"run":"2014051200",' +
|
15
|
+
'"grid":{"x":262,"y":126,"x_error":4188,"y_error":5071},' +
|
16
|
+
'"ntimes":3,' +
|
17
|
+
'"times":[1399852800,1399856400,1399860000],' +
|
18
|
+
'"temp":[1.2,1.7,1.6],' +
|
19
|
+
'"rh":[88,86,78],' +
|
20
|
+
'"low_clouds":[0,0,0],' +
|
21
|
+
'"medium_clouds":[0,0,0],' +
|
22
|
+
'"high_clouds":[0,14,14],' +
|
23
|
+
'"precipitations":[0,0,0],' +
|
24
|
+
'"pblh":[0,126,322],' +
|
25
|
+
'"pressure":[885.2,884.1,884.2],' +
|
26
|
+
'"wind_10m_ground_speed":[3.3,1.5,1.5],' +
|
27
|
+
'"wind_10m_ground_dir":[350,6,59],' +
|
28
|
+
'"wind_1000m_msl_speed":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],' +
|
29
|
+
'"wind_1000m_msl_dir":[270,270,270],' +
|
30
|
+
'"wind_2000m_msl_speed":[0,0,0],' +
|
31
|
+
'"wind_2000m_msl_dir":[270,270,270],' +
|
32
|
+
'"wind_3000m_msl_speed":[0,0,0],' +
|
33
|
+
'"wind_3000m_msl_dir":[270,270,270],' +
|
34
|
+
'"wind_4000m_msl_speed":[0,0,0],' +
|
35
|
+
'"wind_4000m_msl_dir":[270,270,270],' +
|
36
|
+
'"status":"200",' +
|
37
|
+
'"msg":"",' +
|
38
|
+
'"srv":"1.1.1.1"' +
|
39
|
+
'}'
|
40
|
+
|
41
|
+
setup do
|
42
|
+
@response = OpenMeteoResponse.new([:times, :temp, :low_clouds, :medium_clouds, :high_clouds, :precipitations])
|
43
|
+
@response.append_line(JSON_RESPONSE)
|
44
|
+
end
|
45
|
+
|
46
|
+
should 'return response as a filtered hash' do
|
47
|
+
response_hash = @response.as_hash
|
48
|
+
|
49
|
+
assert_equal [1399852800,1399856400,1399860000], response_hash[:times]
|
50
|
+
assert_equal [1.2,1.7,1.6], response_hash[:temp]
|
51
|
+
assert_equal [0,0,0], response_hash[:low_clouds]
|
52
|
+
assert_equal [0,0,0], response_hash[:medium_clouds]
|
53
|
+
assert_equal [0,14,14], response_hash[:high_clouds]
|
54
|
+
assert_equal [0,0,0], response_hash[:precipitations]
|
55
|
+
assert_nil response_hash[:pressure]
|
56
|
+
assert_nil response_hash[:invalid_key]
|
57
|
+
end
|
58
|
+
|
59
|
+
should 'return raw json response' do
|
60
|
+
assert_equal JSON_RESPONSE, @response.as_raw_json
|
61
|
+
end
|
62
|
+
|
63
|
+
should 'return a list of forecasts' do
|
64
|
+
|
65
|
+
forecasts = @response.as_list
|
66
|
+
|
67
|
+
assert_equal 3, forecasts.length
|
68
|
+
assert_equal [1399852800,1399856400,1399860000], forecasts.collect {|f| f[:times]}
|
69
|
+
assert_equal [1.2,1.7,1.6], forecasts.collect {|f| f[:temp]}
|
70
|
+
assert_equal [0,0,0], forecasts.collect {|f| f[:low_clouds]}
|
71
|
+
assert_equal [0,0,0], forecasts.collect {|f| f[:medium_clouds]}
|
72
|
+
assert_equal [0,14,14], forecasts.collect {|f| f[:high_clouds]}
|
73
|
+
assert_equal [0,0,0], forecasts.collect {|f| f[:precipitations]}
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: open_meteo_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- jeanbaptistevilain
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-05-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: test-unit
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: shoulda
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rails
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 3.2.14
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 3.2.14
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: json
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :runtime
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Open Meteo Forecasts client gem
|
111
|
+
email:
|
112
|
+
- jbvilain@gmail.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- Gemfile
|
119
|
+
- LICENSE.txt
|
120
|
+
- README.md
|
121
|
+
- Rakefile
|
122
|
+
- lib/open_meteo_client.rb
|
123
|
+
- lib/open_meteo_client/open_meteo_response.rb
|
124
|
+
- lib/open_meteo_client/version.rb
|
125
|
+
- open_meteo_client.gemspec
|
126
|
+
- test/open_meteo_client/open_meteo_response_test.rb
|
127
|
+
homepage: https://github.com/jeanbaptistevilain/open_meteo_client
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ! '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
none: false
|
142
|
+
requirements:
|
143
|
+
- - ! '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
requirements: []
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 1.8.24
|
149
|
+
signing_key:
|
150
|
+
specification_version: 3
|
151
|
+
summary: A basic model implementation to enable open meteo forecasts data usage in
|
152
|
+
RoR projects
|
153
|
+
test_files:
|
154
|
+
- test/open_meteo_client/open_meteo_response_test.rb
|