Class: MeteoUB::Data
- Inherits:
-
Object
- Object
- MeteoUB::Data
- Defined in:
- lib/meteoub-gem.rb
Instance Attribute Summary collapse
-
#data_uri ⇒ Object
Returns the value of attribute data_uri.
-
#datetime ⇒ Object
Returns the value of attribute datetime.
-
#datetime_max ⇒ Object
Returns the value of attribute datetime_max.
-
#datetime_min ⇒ Object
Returns the value of attribute datetime_min.
-
#humidity ⇒ Object
Returns the value of attribute humidity.
-
#max_wind_speed ⇒ Object
Returns the value of attribute max_wind_speed.
-
#max_wind_speed_km_h ⇒ Object
Returns the value of attribute max_wind_speed_km_h.
-
#precipitation ⇒ Object
Returns the value of attribute precipitation.
-
#pressure ⇒ Object
Returns the value of attribute pressure.
-
#rain ⇒ Object
Returns the value of attribute rain.
-
#raw_data ⇒ Object
Returns the value of attribute raw_data.
-
#sunrise ⇒ Object
Returns the value of attribute sunrise.
-
#sunset ⇒ Object
Returns the value of attribute sunset.
-
#temperature ⇒ Object
Returns the value of attribute temperature.
-
#temperature_max ⇒ Object
Returns the value of attribute temperature_max.
-
#temperature_min ⇒ Object
Returns the value of attribute temperature_min.
-
#windrose ⇒ Object
Returns the value of attribute windrose.
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(params = {}) ⇒ Data
constructor
A new instance of Data.
- #parse ⇒ Object
- #parse_date(date_raw) ⇒ Object
- #parse_maxmin(maxmin_raw) ⇒ Object
- #parse_windrose(windrose_raw) ⇒ Object
- #sanitize_time(time_split) ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Data
Returns a new instance of Data.
29 30 31 32 33 |
# File 'lib/meteoub-gem.rb', line 29 def initialize(params = {}) @data_uri = URI(DATA_URL) @maxmin_uri = URI(MAXMIN_URL) parse if get end |
Instance Attribute Details
#data_uri ⇒ Object
Returns the value of attribute data_uri.
10 11 12 |
# File 'lib/meteoub-gem.rb', line 10 def data_uri @data_uri end |
#datetime ⇒ Object
Returns the value of attribute datetime.
13 14 15 |
# File 'lib/meteoub-gem.rb', line 13 def datetime @datetime end |
#datetime_max ⇒ Object
Returns the value of attribute datetime_max.
25 26 27 |
# File 'lib/meteoub-gem.rb', line 25 def datetime_max @datetime_max end |
#datetime_min ⇒ Object
Returns the value of attribute datetime_min.
27 28 29 |
# File 'lib/meteoub-gem.rb', line 27 def datetime_min @datetime_min end |
#humidity ⇒ Object
Returns the value of attribute humidity.
16 17 18 |
# File 'lib/meteoub-gem.rb', line 16 def humidity @humidity end |
#max_wind_speed ⇒ Object
Returns the value of attribute max_wind_speed.
21 22 23 |
# File 'lib/meteoub-gem.rb', line 21 def max_wind_speed @max_wind_speed end |
#max_wind_speed_km_h ⇒ Object
Returns the value of attribute max_wind_speed_km_h.
22 23 24 |
# File 'lib/meteoub-gem.rb', line 22 def max_wind_speed_km_h @max_wind_speed_km_h end |
#precipitation ⇒ Object
Returns the value of attribute precipitation.
20 21 22 |
# File 'lib/meteoub-gem.rb', line 20 def precipitation @precipitation end |
#pressure ⇒ Object
Returns the value of attribute pressure.
15 16 17 |
# File 'lib/meteoub-gem.rb', line 15 def pressure @pressure end |
#rain ⇒ Object
Returns the value of attribute rain.
19 20 21 |
# File 'lib/meteoub-gem.rb', line 19 def rain @rain end |
#raw_data ⇒ Object
Returns the value of attribute raw_data.
11 12 13 |
# File 'lib/meteoub-gem.rb', line 11 def raw_data @raw_data end |
#sunrise ⇒ Object
Returns the value of attribute sunrise.
17 18 19 |
# File 'lib/meteoub-gem.rb', line 17 def sunrise @sunrise end |
#sunset ⇒ Object
Returns the value of attribute sunset.
18 19 20 |
# File 'lib/meteoub-gem.rb', line 18 def sunset @sunset end |
#temperature ⇒ Object
Returns the value of attribute temperature.
14 15 16 |
# File 'lib/meteoub-gem.rb', line 14 def temperature @temperature end |
#temperature_max ⇒ Object
Returns the value of attribute temperature_max.
24 25 26 |
# File 'lib/meteoub-gem.rb', line 24 def temperature_max @temperature_max end |
#temperature_min ⇒ Object
Returns the value of attribute temperature_min.
26 27 28 |
# File 'lib/meteoub-gem.rb', line 26 def temperature_min @temperature_min end |
#windrose ⇒ Object
Returns the value of attribute windrose.
23 24 25 |
# File 'lib/meteoub-gem.rb', line 23 def windrose @windrose end |
Instance Method Details
#get ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/meteoub-gem.rb', line 35 def get @raw_data = Net::HTTP.get(@data_uri).split("\n") @raw_maxmin = Net::HTTP.get(@maxmin_uri).split("\n") true rescue false end |
#parse ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/meteoub-gem.rb', line 43 def parse @datetime = DateTime.strptime(@raw_data[0] + " " + @raw_data[1] + " UTC", "%d-%m-%y %k:%M %Z") @temperature = @raw_data[2].to_f @pressure = @raw_data[10].to_f @humidity = @raw_data[7].to_f @sunrise = parse_date(@raw_data[19]) @sunset = parse_date(@raw_data[20]) @rain = @raw_data[22].to_i == 1 @precipitation = @raw_data[21].to_i @max_wind_speed = @raw_data[12].to_f @max_wind_speed_km_h = @max_wind_speed * 3.6 @windrose = parse_windrose(@raw_data[13].to_f) @temperature_max, @datetime_max = parse_maxmin(@raw_maxmin[0]) @temperature_min, @datetime_min = parse_maxmin(@raw_maxmin[1]) end |
#parse_date(date_raw) ⇒ Object
59 60 61 62 |
# File 'lib/meteoub-gem.rb', line 59 def parse_date(date_raw) hour, minutes = sanitize_time(date_raw.split(":")) DateTime.strptime(@raw_data[0] + " #{hour}:#{minutes} UTC", "%d-%m-%y %k:%M %Z") end |
#parse_maxmin(maxmin_raw) ⇒ Object
64 65 66 67 68 |
# File 'lib/meteoub-gem.rb', line 64 def parse_maxmin(maxmin_raw) temperature, time, date = maxmin_raw.split(" ") hour, minutes = sanitize_time(time.split(":")) [temperature.to_f, DateTime.strptime("#{date} #{hour}:#{minutes} UTC", "%Y%m%d %k:%M %Z")] end |
#parse_windrose(windrose_raw) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/meteoub-gem.rb', line 79 def parse_windrose(windrose_raw) case(windrose_raw) when (0..11.25) return "N" when (11.26..33.75) return "NNE" when (33.76..56.25) return "NE" when (56.26..78.75) return "ENE" when (78.76..101.25) return "E" when (101.26..123.75) return "ESE" when (123.76..146.25) return "SE" when (146.26..168.75) return "SSE" when (168.76..191.25) return "S" when (191.16..213.75) return "SSW" when (213.76..236.25) return "SW" when (236.26..258.75) return "WSW" when (258.76..281.25) return "W" when (281.26..303.75) return "WNW" when (303.76..326.25) return "NW" when (326.26..348.75) return "NNW" when (348.76..360.0) return "N" else return "???" end end |
#sanitize_time(time_split) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/meteoub-gem.rb', line 70 def sanitize_time(time_split) if time_split[1] == "60" time_split[1] = 0 time_split[0] = time_split[0].to_i + 1 end time_split end |
#save ⇒ Object
120 121 122 123 |
# File 'lib/meteoub-gem.rb', line 120 def save measure = Measure.new({:datetime => @datetime, :temperature => @temperature}) measure.save end |