Class: MeteoUB::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/meteoub-gem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Data

Returns a new instance of Data.



24
25
26
27
# File 'lib/meteoub-gem.rb', line 24

def initialize(params = {})
  @data_uri = URI(DATA_URL)
  parse if get
end

Instance Attribute Details

#data_uriObject

Returns the value of attribute data_uri.



9
10
11
# File 'lib/meteoub-gem.rb', line 9

def data_uri
  @data_uri
end

#datetimeObject

Returns the value of attribute datetime.



12
13
14
# File 'lib/meteoub-gem.rb', line 12

def datetime
  @datetime
end

#humidityObject

Returns the value of attribute humidity.



15
16
17
# File 'lib/meteoub-gem.rb', line 15

def humidity
  @humidity
end

#max_wind_speedObject

Returns the value of attribute max_wind_speed.



20
21
22
# File 'lib/meteoub-gem.rb', line 20

def max_wind_speed
  @max_wind_speed
end

#max_wind_speed_km_hObject

Returns the value of attribute max_wind_speed_km_h.



21
22
23
# File 'lib/meteoub-gem.rb', line 21

def max_wind_speed_km_h
  @max_wind_speed_km_h
end

#precipitationObject

Returns the value of attribute precipitation.



19
20
21
# File 'lib/meteoub-gem.rb', line 19

def precipitation
  @precipitation
end

#pressureObject

Returns the value of attribute pressure.



14
15
16
# File 'lib/meteoub-gem.rb', line 14

def pressure
  @pressure
end

#rainObject

Returns the value of attribute rain.



18
19
20
# File 'lib/meteoub-gem.rb', line 18

def rain
  @rain
end

#raw_dataObject

Returns the value of attribute raw_data.



10
11
12
# File 'lib/meteoub-gem.rb', line 10

def raw_data
  @raw_data
end

#sunriseObject

Returns the value of attribute sunrise.



16
17
18
# File 'lib/meteoub-gem.rb', line 16

def sunrise
  @sunrise
end

#sunsetObject

Returns the value of attribute sunset.



17
18
19
# File 'lib/meteoub-gem.rb', line 17

def sunset
  @sunset
end

#temperatureObject

Returns the value of attribute temperature.



13
14
15
# File 'lib/meteoub-gem.rb', line 13

def temperature
  @temperature
end

#windroseObject

Returns the value of attribute windrose.



22
23
24
# File 'lib/meteoub-gem.rb', line 22

def windrose
  @windrose
end

Instance Method Details

#getObject



29
30
31
32
33
34
# File 'lib/meteoub-gem.rb', line 29

def get
  @raw_data = Net::HTTP.get(@data_uri).split("\n")
  true
rescue
  false
end

#parseObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/meteoub-gem.rb', line 36

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)
end

#parse_date(date_raw) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/meteoub-gem.rb', line 50

def parse_date(date_raw)
  hour, minutes = date_raw.split(":")
  if minutes == 60
    minutes = 0
    hour += 1
  end
  DateTime.strptime(@raw_data[0] + " #{hour}:#{minutes} UTC", "%d-%m-%y %k:%M %Z")
end

#parse_windrose(windrose_raw) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/meteoub-gem.rb', line 60

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

#saveObject



101
102
103
104
# File 'lib/meteoub-gem.rb', line 101

def save
  measure = Measure.new({:datetime => @datetime, :temperature => @temperature})
  measure.save
end