skiplan_client 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,83 +1,83 @@
1
- require 'skiplan_client/version'
2
- require 'skiplan_client/forecast'
3
- require 'skiplan_client/metrics'
4
- require 'skiplan_client/zone'
5
- require 'skiplan_client/skiplan'
6
- require 'open-uri'
7
- require 'nokogiri'
8
- require 'active_support/core_ext/hash/conversions'
9
-
10
- module SkiplanClient
11
-
12
- # Configuration defaults
13
- @config = {
14
- :base_url => ''
15
- }
16
-
17
- def self.configure(url)
18
- @config[:base_url] = url
19
- end
20
-
21
- def self.weather(zone)
22
- xml = Nokogiri::XML(open(@config[:base_url]))
23
- skiplan = Skiplan.new
24
-
25
- zone_nodes = xml.xpath('//ZONE[@reference="' + zone + '"]')
26
- zone_nodes.each do |n|
27
- skiplan.forecasts[n['nom']] = Forecast.new(Hash.from_xml(n.to_s)['ZONE'])
28
- end
29
-
30
- skiplan
31
- end
32
-
33
- def self.ski_area
34
- xml = Nokogiri::XML(open(@config[:base_url]))
35
- skiplan = Skiplan.new
36
-
37
- area_metrics = xml.xpath('//INDICES')
38
- area_metrics.each do |area|
39
- skiplan.metrics[area['nom']] = Metrics.new(Hash.from_xml(area.to_s)['INDICES'])
40
- end
41
-
42
- zones = xml.xpath('//SECTEUR')
43
- zones.each do |z|
44
- skiplan.zones[z['nom']] = Zone.new(Hash.from_xml(z.to_s)['SECTEUR'])
45
- end
46
-
47
- skiplan
48
- end
49
-
50
- def self.texts
51
- xml = Nokogiri::XML(open(@config[:base_url]))
52
- {
53
- fr: SkiplanClient.localized_texts(xml, 'fr'),
54
- en: SkiplanClient.localized_texts(xml, 'en')
55
- }
56
- end
57
-
58
- private
59
-
60
- def self.localized_texts(xml, locale)
61
- text_messages = {}
62
-
63
- today_forecast = xml.xpath('//BULLETINS//JOUR//LANGUE[@val="' + locale + '"]')
64
- text_messages['today_forecast'] = Hash.from_xml(today_forecast.to_s)['LANGUE']
65
-
66
- tomorrow_forecast = xml.xpath('//BULLETINS//LENDEMAIN//LANGUE[@val="' + locale + '"]')
67
- text_messages['tomorrow_forecast'] = Hash.from_xml(tomorrow_forecast.to_s)['LANGUE']
68
-
69
- forecasts_comment = xml.xpath('//BULLETINS//COMMENTAIRES//LANGUE[@val="' + locale + '"]')
70
- text_messages['forecasts_comment'] = Hash.from_xml(forecasts_comment.to_s)['LANGUE']
71
-
72
- ski_area_comment = xml.xpath('//INDICES//COMMENTAIRES//LANGUE[@val="' + locale + '"]')
73
- text_messages['ski_area'] = Hash.from_xml(ski_area_comment.to_s)['LANGUE']
74
-
75
- conditions_comment = xml.xpath('//INDICES//ETAT_ROUTE')
76
- text_messages['conditions'] = Hash.from_xml(conditions_comment.to_s)['ETAT_ROUTE']['lib']
77
-
78
- roads_comment = xml.xpath('//INDICES//ETAT_CHAUSSEE')
79
- text_messages['roads'] = Hash.from_xml(roads_comment.to_s)['ETAT_CHAUSSEE']['lib']
80
-
81
- text_messages
82
- end
1
+ require 'skiplan_client/version'
2
+ require 'skiplan_client/forecast'
3
+ require 'skiplan_client/metrics'
4
+ require 'skiplan_client/zone'
5
+ require 'skiplan_client/skiplan'
6
+ require 'open-uri'
7
+ require 'nokogiri'
8
+ require 'active_support/core_ext/hash/conversions'
9
+
10
+ module SkiplanClient
11
+
12
+ # Configuration defaults
13
+ @config = {
14
+ :base_url => ''
15
+ }
16
+
17
+ def self.configure(url)
18
+ @config[:base_url] = url
19
+ end
20
+
21
+ def self.weather(zone)
22
+ xml = Nokogiri::XML(open(@config[:base_url]))
23
+ skiplan = Skiplan.new(xml.xpath('//DATEINFO').text)
24
+
25
+ zone_nodes = xml.xpath('//ZONE[@reference="' + zone + '"]')
26
+ zone_nodes.each do |n|
27
+ skiplan.forecasts[n['nom']] = Forecast.new(Hash.from_xml(n.to_s)['ZONE'])
28
+ end
29
+
30
+ skiplan
31
+ end
32
+
33
+ def self.ski_area
34
+ xml = Nokogiri::XML(open(@config[:base_url]))
35
+ skiplan = Skiplan.new(xml.xpath('//DATEINFO').text)
36
+
37
+ area_metrics = xml.xpath('//INDICES')
38
+ area_metrics.each do |area|
39
+ skiplan.metrics[area['nom']] = Metrics.new(Hash.from_xml(area.to_s)['INDICES'])
40
+ end
41
+
42
+ zones = xml.xpath('//SECTEUR')
43
+ zones.each do |z|
44
+ skiplan.zones[z['nom']] = Zone.new(Hash.from_xml(z.to_s)['SECTEUR'])
45
+ end
46
+
47
+ skiplan
48
+ end
49
+
50
+ def self.texts
51
+ xml = Nokogiri::XML(open(@config[:base_url]))
52
+ {
53
+ fr: SkiplanClient.localized_texts(xml, 'fr'),
54
+ en: SkiplanClient.localized_texts(xml, 'en')
55
+ }
56
+ end
57
+
58
+ private
59
+
60
+ def self.localized_texts(xml, locale)
61
+ text_messages = {}
62
+
63
+ today_forecast = xml.xpath('//BULLETINS//JOUR//LANGUE[@val="' + locale + '"]')
64
+ text_messages['today_forecast'] = Hash.from_xml(today_forecast.to_s)['LANGUE']
65
+
66
+ tomorrow_forecast = xml.xpath('//BULLETINS//LENDEMAIN//LANGUE[@val="' + locale + '"]')
67
+ text_messages['tomorrow_forecast'] = Hash.from_xml(tomorrow_forecast.to_s)['LANGUE']
68
+
69
+ forecasts_comment = xml.xpath('//BULLETINS//COMMENTAIRES//LANGUE[@val="' + locale + '"]')
70
+ text_messages['forecasts_comment'] = Hash.from_xml(forecasts_comment.to_s)['LANGUE']
71
+
72
+ ski_area_comment = xml.xpath('//INDICES//COMMENTAIRES//LANGUE[@val="' + locale + '"]')
73
+ text_messages['ski_area'] = Hash.from_xml(ski_area_comment.to_s)['LANGUE']
74
+
75
+ conditions_comment = xml.xpath('//INDICES//ETAT_ROUTE')
76
+ text_messages['conditions'] = Hash.from_xml(conditions_comment.to_s)['ETAT_ROUTE']['lib']
77
+
78
+ roads_comment = xml.xpath('//INDICES//ETAT_CHAUSSEE')
79
+ text_messages['roads'] = Hash.from_xml(roads_comment.to_s)['ETAT_CHAUSSEE']['lib']
80
+
81
+ text_messages
82
+ end
83
83
  end
@@ -1,11 +1,11 @@
1
- module AttributeHelper
2
- def attributes=(hash)
3
- hash.each do |key, value|
4
- instance_variable_set("@#{key}", value)
5
- end
6
- end
7
-
8
- def attributes
9
- Hash[instance_variables.map { |name| [name.to_s[1..-1].to_sym, instance_variable_get(name)] }]
10
- end
1
+ module AttributeHelper
2
+ def attributes=(hash)
3
+ hash.each do |key, value|
4
+ instance_variable_set("@#{key}", value)
5
+ end
6
+ end
7
+
8
+ def attributes
9
+ Hash[instance_variables.map { |name| [name.to_s[1..-1].to_sym, instance_variable_get(name)] }]
10
+ end
11
11
  end
@@ -1,63 +1,63 @@
1
- # encoding: UTF-8
2
- require 'skiplan_client/attribute_helper'
3
-
4
- class Forecast
5
-
6
- include AttributeHelper
7
-
8
- def initialize(attributes)
9
- self.attributes = attributes
10
- end
11
-
12
- def updated_at
13
- @datemaj
14
- end
15
-
16
- def weather_am
17
- @CIEL_ID
18
- end
19
-
20
- def weather_pm
21
- @CIEL_ID_APM
22
- end
23
-
24
- def temp_am
25
- @TEMPERATURE
26
- end
27
-
28
- def temp_pm
29
- @TEMPERATURE_APM
30
- end
31
-
32
- def temp_felt
33
- @TEMPERATURE_RESSENTIE
34
- end
35
-
36
- def wind_speed
37
- @VENT
38
- end
39
-
40
- def wind_dir
41
- @DIRECTION
42
- end
43
-
44
- def avalanche
45
- @VALRISQUE
46
- end
47
-
48
- def snow_height
49
- @CUMUL
50
- end
51
-
52
- def last_fall
53
- @DERNIERE_CHUTE
54
- end
55
-
56
- def fresh_snow
57
- @NEIGE
58
- end
59
-
60
- def visibility
61
- @VISIBILITE
62
- end
1
+ # encoding: UTF-8
2
+ require 'skiplan_client/attribute_helper'
3
+
4
+ class Forecast
5
+
6
+ include AttributeHelper
7
+
8
+ def initialize(attributes)
9
+ self.attributes = attributes
10
+ end
11
+
12
+ def updated_at
13
+ @datemaj
14
+ end
15
+
16
+ def weather_am
17
+ @CIEL_ID
18
+ end
19
+
20
+ def weather_pm
21
+ @CIEL_ID_APM
22
+ end
23
+
24
+ def temp_am
25
+ @TEMPERATURE
26
+ end
27
+
28
+ def temp_pm
29
+ @TEMPERATURE_APM
30
+ end
31
+
32
+ def temp_felt
33
+ @TEMPERATURE_RESSENTIE
34
+ end
35
+
36
+ def wind_speed
37
+ @VENT
38
+ end
39
+
40
+ def wind_dir
41
+ @DIRECTION
42
+ end
43
+
44
+ def avalanche
45
+ @VALRISQUE
46
+ end
47
+
48
+ def snow_height
49
+ @CUMUL
50
+ end
51
+
52
+ def last_fall
53
+ @DERNIERE_CHUTE
54
+ end
55
+
56
+ def fresh_snow
57
+ @NEIGE
58
+ end
59
+
60
+ def visibility
61
+ @VISIBILITE
62
+ end
63
63
  end
@@ -1,84 +1,84 @@
1
- # encoding: UTF-8
2
- require 'skiplan_client/attribute_helper'
3
-
4
- class Metrics
5
-
6
- include AttributeHelper
7
-
8
- METRICS = ['ETAT_CHAUSSEE', 'ETAT_ROUTE', 'SKI_NUIT', 'KM_SKATING', 'COMMENTAIRES', 'SKI_ALPIN', 'SKI_ALPIN_VERTES', 'SKI_ALPIN_BLEUES', 'SKI_ALPIN_ROUGES',
9
- 'SKI_ALPIN_NOIRES', 'SKI_NORDIQUE', 'SKI_NORDIQUE_VERTES', 'SKI_NORDIQUE_BLEUES', 'SKI_NORDIQUE_ROUGES', 'SKI_NORDIQUE_NOIRES',
10
- 'REMONTEES', 'PIETONS', 'RAQUETTES', 'LUGE', 'SNOWPARK']
11
-
12
- def initialize(attributes)
13
- self.attributes = attributes.keep_if {|k, v| METRICS.include?(k)} unless attributes.nil?
14
- end
15
-
16
- def alpine
17
- alpine_metrics = {}
18
- alpine_metrics[:total] = get_ratio(@SKI_ALPIN)
19
- alpine_metrics[:green] = get_ratio(@SKI_ALPIN_VERTES)
20
- alpine_metrics[:blue] = get_ratio(@SKI_ALPIN_BLEUES)
21
- alpine_metrics[:red] = get_ratio(@SKI_ALPIN_ROUGES)
22
- alpine_metrics[:black] = get_ratio(@SKI_ALPIN_NOIRES)
23
-
24
- alpine_metrics
25
- end
26
-
27
- def nordic
28
- nordic_metrics = {}
29
- nordic_metrics[:total] = get_ratio(@SKI_NORDIQUE)
30
- nordic_metrics[:green] = get_ratio(@SKI_NORDIQUE_VERTES)
31
- nordic_metrics[:blue] = get_ratio(@SKI_NORDIQUE_BLEUES)
32
- nordic_metrics[:red] = get_ratio(@SKI_NORDIQUE_ROUGES)
33
- nordic_metrics[:black] = get_ratio(@SKI_NORDIQUE_NOIRES)
34
- nordic_metrics[:km] = get_ratio_km(@SKI_NORDIQUE)
35
-
36
- nordic_metrics
37
- end
38
-
39
- def pedestrian
40
- {:total => get_ratio_km(@PIETONS)}
41
- end
42
-
43
- def snowshoes
44
- {:total => get_ratio_km(@RAQUETTES)}
45
- end
46
-
47
- def sledge
48
- {:total => get_ratio(@LUGE)}
49
- end
50
-
51
- def snowpark
52
- {:total => get_ratio(@SNOWPARK)}
53
- end
54
-
55
- def skilifts
56
- {:total => get_ratio(@REMONTEES)}
57
- end
58
-
59
- def night_ski
60
- @SKI_NUIT && @SKI_NUIT['etat'] == '1'
61
- end
62
-
63
- def skating
64
- @KM_SKATING['ouvert'] unless @KM_SKATING.nil?
65
- end
66
-
67
- def driving_conditions
68
- @ETAT_ROUTE['lib']
69
- end
70
-
71
- def roads
72
- @ETAT_CHAUSSEE['lib'] unless (@ETAT_CHAUSSEE.nil? || @ETAT_CHAUSSEE['val'] == '8')
73
- end
74
-
75
- private
76
-
77
- def get_ratio(hash)
78
- "#{hash['ouvertes']}/#{hash['total']}" unless hash.nil?
79
- end
80
-
81
- def get_ratio_km(hash)
82
- "#{sprintf("%g", hash['lng_ouverts'].to_f)}/#{sprintf("%g", hash['lng_total'].to_f)}km" unless hash.nil?
83
- end
1
+ # encoding: UTF-8
2
+ require 'skiplan_client/attribute_helper'
3
+
4
+ class Metrics
5
+
6
+ include AttributeHelper
7
+
8
+ METRICS = ['ETAT_CHAUSSEE', 'ETAT_ROUTE', 'SKI_NUIT', 'KM_SKATING', 'COMMENTAIRES', 'SKI_ALPIN', 'SKI_ALPIN_VERTES', 'SKI_ALPIN_BLEUES', 'SKI_ALPIN_ROUGES',
9
+ 'SKI_ALPIN_NOIRES', 'SKI_NORDIQUE', 'SKI_NORDIQUE_VERTES', 'SKI_NORDIQUE_BLEUES', 'SKI_NORDIQUE_ROUGES', 'SKI_NORDIQUE_NOIRES',
10
+ 'REMONTEES', 'PIETONS', 'RAQUETTES', 'LUGE', 'SNOWPARK']
11
+
12
+ def initialize(attributes)
13
+ self.attributes = attributes.keep_if {|k, v| METRICS.include?(k)} unless attributes.nil?
14
+ end
15
+
16
+ def alpine
17
+ alpine_metrics = {}
18
+ alpine_metrics[:total] = get_ratio(@SKI_ALPIN)
19
+ alpine_metrics[:green] = get_ratio(@SKI_ALPIN_VERTES)
20
+ alpine_metrics[:blue] = get_ratio(@SKI_ALPIN_BLEUES)
21
+ alpine_metrics[:red] = get_ratio(@SKI_ALPIN_ROUGES)
22
+ alpine_metrics[:black] = get_ratio(@SKI_ALPIN_NOIRES)
23
+
24
+ alpine_metrics
25
+ end
26
+
27
+ def nordic
28
+ nordic_metrics = {}
29
+ nordic_metrics[:total] = get_ratio(@SKI_NORDIQUE)
30
+ nordic_metrics[:green] = get_ratio(@SKI_NORDIQUE_VERTES)
31
+ nordic_metrics[:blue] = get_ratio(@SKI_NORDIQUE_BLEUES)
32
+ nordic_metrics[:red] = get_ratio(@SKI_NORDIQUE_ROUGES)
33
+ nordic_metrics[:black] = get_ratio(@SKI_NORDIQUE_NOIRES)
34
+ nordic_metrics[:km] = get_ratio_km(@SKI_NORDIQUE)
35
+
36
+ nordic_metrics
37
+ end
38
+
39
+ def pedestrian
40
+ {:total => get_ratio_km(@PIETONS)}
41
+ end
42
+
43
+ def snowshoes
44
+ {:total => get_ratio_km(@RAQUETTES)}
45
+ end
46
+
47
+ def sledge
48
+ {:total => get_ratio(@LUGE)}
49
+ end
50
+
51
+ def snowpark
52
+ {:total => get_ratio(@SNOWPARK)}
53
+ end
54
+
55
+ def skilifts
56
+ {:total => get_ratio(@REMONTEES)}
57
+ end
58
+
59
+ def night_ski
60
+ @SKI_NUIT && @SKI_NUIT['etat'] == '1'
61
+ end
62
+
63
+ def skating
64
+ @KM_SKATING['ouvert'] unless @KM_SKATING.nil?
65
+ end
66
+
67
+ def driving_conditions
68
+ @ETAT_ROUTE['lib']
69
+ end
70
+
71
+ def roads
72
+ @ETAT_CHAUSSEE['lib'] unless (@ETAT_CHAUSSEE.nil? || @ETAT_CHAUSSEE['val'] == '8')
73
+ end
74
+
75
+ private
76
+
77
+ def get_ratio(hash)
78
+ "#{hash['ouvertes_previsions']}/#{hash['total']}" unless hash.nil?
79
+ end
80
+
81
+ def get_ratio_km(hash)
82
+ "#{sprintf("%g", hash['lng_ouverts'].to_f)}/#{sprintf("%g", hash['lng_total'].to_f)}km" unless hash.nil?
83
+ end
84
84
  end