Module: SunCalc

Defined in:
lib/suncalc.rb,
lib/suncalc/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.contextObject



10
11
12
# File 'lib/suncalc.rb', line 10

def self.context
  ExecJS.compile(source)
end

.sourceObject



6
7
8
# File 'lib/suncalc.rb', line 6

def self.source
  "window = {}; #{File.read(Pathname(__FILE__).dirname.join('..', 'vendor', 'assets', 'javascripts', 'suncalc.js'))}"
end

.times(date, latitude, longitude) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/suncalc.rb', line 14

def self.times(date, latitude, longitude)
  times = self.context.eval("window.SunCalc.getTimes(new Date(#{date.to_f * 1000}), #{latitude}, #{longitude})")

  times.keys.each do |key|
    old_key = key
    key = key.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr('-', '_').
    gsub(/\s/, '_').
    gsub(/__+/, '_').
    downcase.
    to_sym

    times[key] = times.delete(old_key)
  end

  times
end