sunnyday 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +22 -0
- data/LICENSE +22 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/bin/sunnyday +9 -0
- data/lib/sunnyday/cli/cli.rb +66 -0
- data/lib/sunnyday/cli/output.rb +52 -0
- data/lib/sunnyday/client.rb +51 -0
- data/lib/sunnyday/configuration.rb +26 -0
- data/lib/sunnyday/version.rb +3 -0
- data/lib/sunnyday.rb +12 -0
- data/sunnyday.gemspec +23 -0
- data/test/test.rb +49 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 176c4a595e4f4d2ff634509716f5b70878d7c190
|
4
|
+
data.tar.gz: 79f0a6c54cd74a4d405d9c6c5fd581c0fa17a5fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b62c768fa1c83cb5eef9f0e842911eb66c55581d1b2d5b36ae34b0ceb1691af650f9c0ded0b7cb59ce3ad29ce5fdc26aff20dff58b7c25cd3b878ea3f25b8fd
|
7
|
+
data.tar.gz: febfec8367d0cd912fd676b5cfe4202f8c41782d2d7b731830e99ef81b347a008e4a48ad8756ef21a730360bae0296875791b9a11f5c8a57f472f22ed60623fb
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sunnyday (0.1.2)
|
5
|
+
term-ansicolor
|
6
|
+
thor
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
rake (10.1.0)
|
12
|
+
term-ansicolor (1.2.2)
|
13
|
+
tins (~> 0.8)
|
14
|
+
thor (0.18.1)
|
15
|
+
tins (0.12.0)
|
16
|
+
|
17
|
+
PLATFORMS
|
18
|
+
ruby
|
19
|
+
|
20
|
+
DEPENDENCIES
|
21
|
+
rake
|
22
|
+
sunnyday!
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Heiko Dreyer
|
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,39 @@
|
|
1
|
+
# Readme
|
2
|
+
|
3
|
+
sunnyday is a ruby wrapper around the openweather.org api.
|
4
|
+
|
5
|
+
Include the gem and quickly fetch weather data, e.g. like that:
|
6
|
+
|
7
|
+
SunnyDay::Client.new.current_weather(q: 'London, UK')
|
8
|
+
|
9
|
+
There is also a CLI:
|
10
|
+
|
11
|
+
sunnyday weather London,UK
|
12
|
+
|
13
|
+
However, CLI output is still WIP.
|
14
|
+
|
15
|
+
---
|
16
|
+
# License
|
17
|
+
|
18
|
+
Copyright (c) 2013 Heiko Dreyer
|
19
|
+
|
20
|
+
MIT License
|
21
|
+
|
22
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
23
|
+
a copy of this software and associated documentation files (the
|
24
|
+
"Software"), to deal in the Software without restriction, including
|
25
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
26
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
27
|
+
permit persons to whom the Software is furnished to do so, subject to
|
28
|
+
the following conditions:
|
29
|
+
|
30
|
+
The above copyright notice and this permission notice shall be
|
31
|
+
included in all copies or substantial portions of the Software.
|
32
|
+
|
33
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
34
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
35
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
36
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
37
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
38
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
39
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/bin/sunnyday
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'sunnyday/cli/output'
|
3
|
+
|
4
|
+
module SunnyDay
|
5
|
+
class CLI < Thor
|
6
|
+
class_option :lat, :type => :numeric
|
7
|
+
class_option :long, :type => :numeric
|
8
|
+
class_option :id, :type => :numeric
|
9
|
+
class_option :units, :type => :string
|
10
|
+
class_option :lang, :type => :string
|
11
|
+
class_option :app_id, :type => :numeric
|
12
|
+
class_option :api_version, :type => :numeric
|
13
|
+
|
14
|
+
desc 'weather <city>', 'Retrieves current weather for a specific city. You can provide a city name (standard), latitude and longitude or a city id.'
|
15
|
+
def weather(city = nil)
|
16
|
+
return unless validate(city)
|
17
|
+
data = client.current_weather(w_options(city))
|
18
|
+
SunnyDay::Output.new.weather(data)
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'forecast <city>', 'Retrieves current forecast for a specific city. You can provide a city name (standard), latitude and longitude or a city id.'
|
22
|
+
def forecast(city = nil)
|
23
|
+
return unless validate(city)
|
24
|
+
data = client.forecast(w_options(city))
|
25
|
+
SunnyDay::Output.new.forecast(data)
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'd_forecast <days> <city>', 'Retrieves daily forecast for a specific city. You can provide a city name (standard), latitude and longitude or a city id.'
|
29
|
+
def d_forecast(days, city = nil)
|
30
|
+
return unless validate(city)
|
31
|
+
opts = w_options(city)
|
32
|
+
opts[:cnt] = days
|
33
|
+
data = client.daily_forecast(opts)
|
34
|
+
SunnyDay::Output.new.daily_forecast(data)
|
35
|
+
end
|
36
|
+
|
37
|
+
desc 'find <city> --limit', 'Searching for city. You can provide a city name (standard) or latitude and longitude.'
|
38
|
+
option :limit, :type => :numeric
|
39
|
+
def find(city = nil)
|
40
|
+
return unless validate(city)
|
41
|
+
opts = w_options(city)
|
42
|
+
opts[:cnt] = (options[:limit] - 1) if options[:limit] && options[:limit] > 0
|
43
|
+
opts.delete(:limit) if opts.has_key?(:limit)
|
44
|
+
data = client.find(opts)
|
45
|
+
SunnyDay::Output.new.find(data)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def validate(city)
|
51
|
+
valid = !city.nil? || (!options[:lat].nil? && !options[:long].nil?) || !options[:id].nil?
|
52
|
+
puts 'Sorry, your input doesn’t appear to be valid. Use "sunnyday help" to see available options.' unless valid
|
53
|
+
valid
|
54
|
+
end
|
55
|
+
|
56
|
+
def w_options(city)
|
57
|
+
opts = options.dup
|
58
|
+
opts[:q] = city unless city.nil?
|
59
|
+
opts
|
60
|
+
end
|
61
|
+
|
62
|
+
def client(config = {})
|
63
|
+
SunnyDay.client(config)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'term/ansicolor'
|
2
|
+
|
3
|
+
module SunnyDay
|
4
|
+
class Color
|
5
|
+
extend Term::ANSIColor
|
6
|
+
end
|
7
|
+
|
8
|
+
class Output
|
9
|
+
def weather(data)
|
10
|
+
return if has_error?(data)
|
11
|
+
print Color.bold, "\nWeather data for #{data[:name]}, #{data[:sys][:country]}\n", Color.clear
|
12
|
+
puts Color.green, "Current temperature: #{data[:main][:temp]} degrees.", Color.clear
|
13
|
+
puts "Weather: #{data[:weather][0][:description]}\n"
|
14
|
+
puts ''
|
15
|
+
print "Max temperature: #{data[:main][:temp_max]} degrees.\n"
|
16
|
+
print "Min temperature: #{data[:main][:temp_min]} degrees.\n"
|
17
|
+
print "Sunrise: #{Time.at(data[:sys][:sunrise])}\n"
|
18
|
+
print "Sunset: #{Time.at(data[:sys][:sunset])}\n"
|
19
|
+
puts ''
|
20
|
+
end
|
21
|
+
|
22
|
+
def forecast(data)
|
23
|
+
return if has_error?(data)
|
24
|
+
puts data
|
25
|
+
end
|
26
|
+
|
27
|
+
def daily_forecast(data)
|
28
|
+
return if has_error?(data)
|
29
|
+
puts data
|
30
|
+
end
|
31
|
+
|
32
|
+
def find(data)
|
33
|
+
return if has_error?(data)
|
34
|
+
print Color.bold, "\nFound #{data[:count]} #{data[:count] == 1 ? 'entry' : 'entries'}:\n\n", Color.clear
|
35
|
+
data[:list].each do |entry|
|
36
|
+
print Color.green, "#{entry[:name]}, #{entry[:sys][:country]} -> id: #{entry[:id]}\n",Color.clear
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def has_error?(data)
|
43
|
+
has_error = data[:cod].to_s != '200'
|
44
|
+
error(data[:message]) if has_error
|
45
|
+
has_error
|
46
|
+
end
|
47
|
+
|
48
|
+
def error(message)
|
49
|
+
print Color.red, "\n#{message}\n", Color.clear
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module SunnyDay
|
5
|
+
class Client
|
6
|
+
attr_accessor :config
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@config = SunnyDay::Configuration.new(options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def configure
|
13
|
+
yield(@config) if block_given?
|
14
|
+
end
|
15
|
+
|
16
|
+
def current_weather(options = {})
|
17
|
+
call('weather', options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def forecast(options = {})
|
21
|
+
call('forecast', options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def daily_forecast(options = {})
|
25
|
+
call('forecast/daily', options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def find(options = {})
|
29
|
+
call('find', options)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def call(path, options = {})
|
35
|
+
options.delete(:mode) if options.has_key?(:mode)
|
36
|
+
uri = "/data/#{@config.api_version}/#{full_path(path, options)}language=#{@config.language}"
|
37
|
+
uri.concat("&units=#{@config.units}") unless @config.units.nil?
|
38
|
+
uri.concat("&APPID=#{@config.app_id}") unless @config.app_id.nil?
|
39
|
+
response = Net::HTTP.get_response(BASE_URL, uri)
|
40
|
+
JSON.parse(response.body, symbolize_names: true)
|
41
|
+
end
|
42
|
+
|
43
|
+
def full_path(path, options)
|
44
|
+
string = ''
|
45
|
+
options.each_key do |key|
|
46
|
+
string << "#{key}=#{options[key]}&"
|
47
|
+
end
|
48
|
+
"#{path}?#{string}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module SunnyDay
|
2
|
+
BASE_URL = 'api.openweathermap.org'
|
3
|
+
|
4
|
+
class Configuration
|
5
|
+
# App id. Default none.
|
6
|
+
attr_accessor :app_id
|
7
|
+
|
8
|
+
# API version. Default 2.5.
|
9
|
+
attr_accessor :api_version
|
10
|
+
|
11
|
+
# Language. Default 'en'
|
12
|
+
attr_accessor :language
|
13
|
+
|
14
|
+
# Units - 'metric' or 'imperial'. Default metric.
|
15
|
+
attr_accessor :units
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
options.each_key do |key|
|
19
|
+
instance_variable_set("@#{key}", options[key])
|
20
|
+
end
|
21
|
+
|
22
|
+
@api_version ||= '2.5'
|
23
|
+
@language ||= 'en'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/sunnyday.rb
ADDED
data/sunnyday.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'sunnyday/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.platform = Gem::Platform::RUBY
|
6
|
+
s.name = 'sunnyday'
|
7
|
+
s.version = SunnyDay::VERSION
|
8
|
+
s.date = '2013-10-19'
|
9
|
+
s.summary = 'Sunnyday is a wrapper around the OpenWeatherMap api (openweathermap.com)'
|
10
|
+
s.description = 'The OpenWeatherMap service provides free weather data. This gem is a wrapper around the public api. Visit openweathermap.com for more details.'
|
11
|
+
s.authors = ['Heiko Dreyer']
|
12
|
+
s.email = 'heiko@boxedfolder.com'
|
13
|
+
s.homepage = 'http://github.com/bfolder/sunnyday'
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split($/)
|
17
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
s.required_ruby_version = '>= 1.8.7'
|
21
|
+
s.add_dependency 'thor'
|
22
|
+
s.add_dependency 'term-ansicolor'
|
23
|
+
end
|
data/test/test.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'sunnyday'
|
5
|
+
|
6
|
+
class TestCaseGeneral < Test::Unit::TestCase
|
7
|
+
def test_client_configuration
|
8
|
+
@client = SunnyDay.client
|
9
|
+
assert_not_nil(@client, 'Client should not be nil')
|
10
|
+
|
11
|
+
@client.configure do |config|
|
12
|
+
config.app_id = 21311323
|
13
|
+
end
|
14
|
+
assert_equal(21311323, @client.config.app_id, 'App id should equal 21311323')
|
15
|
+
|
16
|
+
@client = SunnyDay::Client.new(app_id: 21311323, language: 'de', api_version: '1.5', units: 'imperial')
|
17
|
+
assert_not_nil(@client, 'Client should not be nil')
|
18
|
+
assert_equal(21311323, @client.config.app_id, 'App id should equal 21311323')
|
19
|
+
assert_equal('de', @client.config.language, 'Language should be de')
|
20
|
+
assert_equal('1.5', @client.config.api_version, 'Api version should be 1.5')
|
21
|
+
assert_equal('imperial', @client.config.units, 'Units should be imperial')
|
22
|
+
end
|
23
|
+
|
24
|
+
def setup
|
25
|
+
@client = SunnyDay.client
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_current_weather
|
29
|
+
results = @client.current_weather(q: 'London, UK')
|
30
|
+
assert_equal('London', results[:name], 'Should be London')
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_forecast
|
34
|
+
results = @client.forecast(q: 'London,UK')
|
35
|
+
assert_equal('London', results[:city][:name], 'Should be London')
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_daily_forecast
|
39
|
+
results = @client.daily_forecast(q: 'London,UK', cnt: 7)
|
40
|
+
assert_equal('London', results[:city][:name], 'Should be London')
|
41
|
+
assert_equal(7, results[:list].size, 'This should be 7 days forecast')
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_find
|
45
|
+
results = @client.find(q: 'London,UK')
|
46
|
+
assert(results[:list].size > 0, 'There should be at least one result')
|
47
|
+
assert_equal('London', results[:list][0][:name], 'Should be London')
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sunnyday
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Heiko Dreyer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
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: term-ansicolor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: The OpenWeatherMap service provides free weather data. This gem is a
|
42
|
+
wrapper around the public api. Visit openweathermap.com for more details.
|
43
|
+
email: heiko@boxedfolder.com
|
44
|
+
executables:
|
45
|
+
- sunnyday
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- Gemfile.lock
|
52
|
+
- LICENSE
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/sunnyday
|
56
|
+
- lib/sunnyday.rb
|
57
|
+
- lib/sunnyday/cli/cli.rb
|
58
|
+
- lib/sunnyday/cli/output.rb
|
59
|
+
- lib/sunnyday/client.rb
|
60
|
+
- lib/sunnyday/configuration.rb
|
61
|
+
- lib/sunnyday/version.rb
|
62
|
+
- sunnyday.gemspec
|
63
|
+
- test/test.rb
|
64
|
+
homepage: http://github.com/bfolder/sunnyday
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 1.8.7
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.1.11
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Sunnyday is a wrapper around the OpenWeatherMap api (openweathermap.com)
|
88
|
+
test_files:
|
89
|
+
- test/test.rb
|