Module: TzLookupWrapper

Defined in:
lib/tz_lookup_wrapper/active_support.rb,
lib/tz_lookup_wrapper.rb,
lib/tz_lookup_wrapper/version.rb

Overview

Defined Under Namespace

Classes: ActiveSupport, TzLookupWrapperException

Constant Summary collapse

TZ_LOOKUP_PATH =
"#{File.dirname(__FILE__)}/../vendor/tz-lookup/index.js"
NODE_BIN =
begin
  if (`nodejs --version` rescue false)
    'nodejs'
  elsif (`node --version` rescue false)
    'node'
  else
    raise TzLookupWrapperException.new("Could not find nodejs runtime.");
  end
end
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.lookup(lat_r, lng_r) ⇒ Object

Lookup a timezone

Example:

>> TzLookupWrapper.lookup(43.7, -79.4)
=> "America/Toronto"

Arguments:

latitude: (Float)
longitude: (Float)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tz_lookup_wrapper.rb', line 28

def self.lookup(lat_r,lng_r)
  lat =   Float lat_r
  lng =   Float lng_r
  script = <<-HEREDOC
    try {
      process.stdout.write(require("#{TZ_LOOKUP_PATH}")(#{lat}, #{lng}));
    } catch (ex) {
      process.stdout.write(ex.message);
      process.exit(code=1)
    }
  HEREDOC
  result = nil
  IO.popen(NODE_BIN, 'r+') do |io|
    io.puts(script)
    io.close_write
    result = io.gets
  end
  if $?.success?
    result
  else
    raise TzLookupWrapperException.new result || "Empty output"
  end
end