eltiempobcn 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/eltiempo +4 -0
- data/lib/eltiempobcn.rb +74 -0
- data/lib/place.rb +80 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5d7682cb4ef72e599fe11c689f8000446d4a45a9bc2ecc86f9145eb79c196c91
|
4
|
+
data.tar.gz: c8493b7683ec829d6af27b6d2ca04677f1507b905c9a1d6faf09fd518cbece7c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85d3ad78327d60022c7e73a8f99dce2a1e4ffa7566a43f84fbbd1293b70a401040856954d35ef21d8314726d277acff67636857c25ee825ed7f731deb3e1fb6a
|
7
|
+
data.tar.gz: 426f60629adedde5d31a187599804abb89883dd75dc16a1b40892b74ec751414dd068f490fa3019b592ea9e07dd7d8268c13eead0548005b50c2550bafcb6eea
|
data/bin/eltiempo
ADDED
data/lib/eltiempobcn.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'i18n'
|
5
|
+
require 'byebug'
|
6
|
+
require_relative 'place.rb'
|
7
|
+
|
8
|
+
# gem uninstall -x eltiempobcn; gem build eltiempobcn.gemspec; gem install eltiempobcn
|
9
|
+
# load './lib/eltiempobcn.rb'; ElTiempoCLI.start( ['today', 'Barcelona'] )
|
10
|
+
class ElTiempoCLI < Thor
|
11
|
+
@@places, @@places_names = Place.get_places_list
|
12
|
+
|
13
|
+
#
|
14
|
+
#
|
15
|
+
desc "today PLACE", "Returns the forcast temperature for today for PLACE"
|
16
|
+
def today( place_name )
|
17
|
+
place = Place.set_temperatures( place_name.to_s, @@places )
|
18
|
+
if place.kind_of?( Place )
|
19
|
+
puts "Today's temperatures for #{ place.name } are: [ min: #{ place.min_today }, max: #{ place.max_today } ] celsius"
|
20
|
+
else
|
21
|
+
puts "Couldn't find the city #{ place_name } or its data."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
#
|
27
|
+
desc "av_max PLACE", "Returns the average of the Maximums for the week in PLACE"
|
28
|
+
def av_max( place_name )
|
29
|
+
place = Place.set_temperatures( place_name.to_s, @@places )
|
30
|
+
if place.kind_of?( Place )
|
31
|
+
puts "The average temperature for the Maximums this week in #{ place.name } are: #{ place.max_avg } celsius degrees"
|
32
|
+
else
|
33
|
+
puts "Couldn't find the city #{ place_name } or its data."
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
#
|
39
|
+
desc "av_min PLACE", "Returns the average of the minimums for the week in PLACE"
|
40
|
+
def av_min( place_name )
|
41
|
+
place = Place.set_temperatures( place_name.to_s, @@places )
|
42
|
+
if place.kind_of?( Place )
|
43
|
+
puts "The average temperature for the minimuims this week in #{ place.name } are: #{ place.min_avg } celsius degrees"
|
44
|
+
else
|
45
|
+
puts "Couldn't find the city #{ place_name } or its data."
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
#
|
51
|
+
desc "list", "Returns the list of places"
|
52
|
+
def list
|
53
|
+
puts @@places_names.join( " | " )
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
#
|
58
|
+
#
|
59
|
+
def self.start( args )
|
60
|
+
if @@places
|
61
|
+
clean_args = args
|
62
|
+
clean_args[ 0 ] = args[ 0 ].sub( /^-*/, "" ) unless clean_args[ 0 ].nil? # remove the leading dashes before execuing the start method
|
63
|
+
super( clean_args )
|
64
|
+
else
|
65
|
+
puts "Couldn't connect with the server, please check your connection and try again later"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
#
|
71
|
+
def self.exit_on_failure?
|
72
|
+
true
|
73
|
+
end
|
74
|
+
end
|
data/lib/place.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
#
|
2
|
+
#
|
3
|
+
class Place
|
4
|
+
attr_accessor :name, :max_today, :min_today, :max_avg, :min_avg
|
5
|
+
|
6
|
+
#
|
7
|
+
#
|
8
|
+
def initialize( name, max_today, min_today, max_avg, min_avg )
|
9
|
+
@name = name
|
10
|
+
@max_today = max_today
|
11
|
+
@min_today = min_today
|
12
|
+
@max_avg = max_avg
|
13
|
+
@min_avg = min_avg
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
#
|
18
|
+
def Place.verify_and_get_temperatures( place, places_list )
|
19
|
+
place = place.to_sym_city
|
20
|
+
city_values = Place.get_array_from_xml( place, places_list ) if places_list.has_key?( place )
|
21
|
+
return city_values
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
#
|
26
|
+
def Place.get_array_from_xml( place, places_list )
|
27
|
+
aff_id = "&affiliate_id=fvzoaihj8486"
|
28
|
+
necessary_data = [ :temperaturaminima, :temperaturamaxima ]
|
29
|
+
city_values = {}
|
30
|
+
|
31
|
+
place_url = ( places_list[place].sub( "&", "&" ) + aff_id )
|
32
|
+
doc = Nokogiri::XML( open( place_url ) )
|
33
|
+
data_city = doc.xpath( "//var" )
|
34
|
+
data_city.each do |weather_param|
|
35
|
+
city_values[ weather_param.children.children[0].to_s.to_sym_city ] = weather_param.children.children[2..-1].map{ |att| att.attributes['value'].value.to_f }
|
36
|
+
end
|
37
|
+
city_values = city_values.select{ |key, value| necessary_data.include?( key ) }
|
38
|
+
return city_values
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
#
|
43
|
+
def Place.get_places_list
|
44
|
+
begin
|
45
|
+
doc = Nokogiri::XML(open("http://api.tiempo.com/index.php?api_lang=es&division=102&affiliate_id=fvzoaihj8486"))
|
46
|
+
data_cities = doc.xpath( "//data" )
|
47
|
+
places = {}
|
48
|
+
names = []
|
49
|
+
data_cities.each do |data_city|
|
50
|
+
places[ data_city.children.children[0].to_s.to_sym_city ] = data_city.children.children[1].to_s
|
51
|
+
names << data_city.children.children[0].to_s
|
52
|
+
end
|
53
|
+
return places, names
|
54
|
+
rescue
|
55
|
+
return false, false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
#
|
61
|
+
def Place.set_temperatures( place, places )
|
62
|
+
place_data = Place.verify_and_get_temperatures( place.to_s, places )
|
63
|
+
|
64
|
+
place = Place.new( place,
|
65
|
+
place_data[ :temperaturamaxima ][0],
|
66
|
+
place_data[ :temperaturaminima ][0],
|
67
|
+
( place_data[ :temperaturamaxima ].sum / place_data[ :temperaturamaxima ].length ).round( 2 ),
|
68
|
+
( place_data[ :temperaturaminima ].sum / place_data[ :temperaturaminima ].length ).round( 2 ) )
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
#
|
74
|
+
#
|
75
|
+
class String
|
76
|
+
def to_sym_city
|
77
|
+
I18n.config.available_locales = :en
|
78
|
+
return I18n.transliterate( self.tr( " ", "").downcase ).to_sym
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eltiempobcn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcelo Jimenez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: It's a simple gem to get the ability to get the weather for different
|
14
|
+
regions in Catalonia using CLI
|
15
|
+
email:
|
16
|
+
- mejimenez@gmail.com
|
17
|
+
executables:
|
18
|
+
- eltiempo
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- bin/eltiempo
|
23
|
+
- lib/eltiempobcn.rb
|
24
|
+
- lib/place.rb
|
25
|
+
homepage: http://rubygems.org/gems/eltiempobcn
|
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.7.6
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: ElTiempoBcn test gem
|
49
|
+
test_files: []
|