Class: Sunrset::TimeAndSun

Inherits:
Object
  • Object
show all
Defined in:
lib/sunrset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(longitude, latitude) ⇒ TimeAndSun

Returns a new instance of TimeAndSun.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sunrset.rb', line 10

def initialize longitude, latitude
  url_time_api = "http://www.earthtools.org/timezone/#{longitude}/#{latitude}"
  p url_time_api
  time_xml_data = Net::HTTP.get_response(URI.parse(url_time_api)).body
  @time_data = XmlSimple.xml_in(time_xml_data)
  @date = DateTime.parse @time_data["localtime"].first

  url_sun_api = "http://www.earthtools.org/sun/#{longitude}/#{latitude}/#{"%02d" % @date.day}/#{"%02d" % @date.month}/#{time_zone}/#{dst? ? 1 : 0}"
  sun_xml_data = Net::HTTP.get_response(URI.parse(url_sun_api)).body
  @sun_data = XmlSimple.xml_in(sun_xml_data)
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'lib/sunrset.rb', line 9

def date
  @date
end

#sun_dataObject (readonly)

Returns the value of attribute sun_data.



9
10
11
# File 'lib/sunrset.rb', line 9

def sun_data
  @sun_data
end

#time_dataObject (readonly)

Returns the value of attribute time_data.



9
10
11
# File 'lib/sunrset.rb', line 9

def time_data
  @time_data
end

Instance Method Details

#dst?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/sunrset.rb', line 22

def dst?
  @time_data["dst"].first.eql?("True") ? true : false
end

#sunriseObject



31
32
33
# File 'lib/sunrset.rb', line 31

def sunrise
  DateTime.parse "#{@date.to_date.to_s} #{@sun_data["morning"].first["sunrise"].first}"
end

#sunsetObject



35
36
37
# File 'lib/sunrset.rb', line 35

def sunset
  DateTime.parse "#{@date.to_date.to_s} #{@sun_data["evening"].first["sunset"].first}"
end

#time_zoneObject



26
27
28
29
# File 'lib/sunrset.rb', line 26

def time_zone
  offset = @time_data["offset"].first
  offset.to_i > 0 ? "+#{offset}" : offset
end