Class: Dommy::Geolocation

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods
Defined in:
lib/dommy/navigator.rb

Overview

‘navigator.geolocation` — stub Geolocation API. Real implementations query the OS; dommy holds a mock position tests configure via `test_set_position(coords)` or `test_set_error(error_code)`.

Spec: www.w3.org/TR/geolocation/

Constant Summary collapse

DEFAULT_COORDS =
{
  "latitude" => 0.0,
  "longitude" => 0.0,
  "accuracy" => 0.0,
  "altitude" => nil,
  "altitudeAccuracy" => nil,
  "heading" => nil,
  "speed" => nil
}.freeze

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(window) ⇒ Geolocation

Returns a new instance of Geolocation.



361
362
363
364
365
366
367
# File 'lib/dommy/navigator.rb', line 361

def initialize(window)
  @window = window
  @position = nil
  @error = nil
  @watches = {}
  @next_watch_id = 0
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



408
409
410
411
412
413
414
415
416
417
# File 'lib/dommy/navigator.rb', line 408

def __js_call__(method, args)
  case method
  when "getCurrentPosition"
    get_current_position(args[0], args[1], args[2])
  when "watchPosition"
    watch_position(args[0], args[1], args[2])
  when "clearWatch"
    clear_watch(args[0])
  end
end

#__test_set_error__(code, message = "") ⇒ Object

Test seam: install a permission/positioning error (code 1=PERMISSION_DENIED, 2=POSITION_UNAVAILABLE, 3=TIMEOUT).



378
379
380
381
# File 'lib/dommy/navigator.rb', line 378

def __test_set_error__(code, message = "")
  @position = nil
  @error = {"code" => code.to_i, "message" => message.to_s}
end

#__test_set_position__(coords = {}) ⇒ Object

Test seam: install a mock position.



370
371
372
373
374
# File 'lib/dommy/navigator.rb', line 370

def __test_set_position__(coords = {})
  merged = DEFAULT_COORDS.merge(coords.transform_keys(&:to_s))
  @position = {"coords" => merged, "timestamp" => @window.scheduler.now_ms}
  @error = nil
end

#clear_watch(id) ⇒ Object Also known as: clearWatch



399
400
401
402
# File 'lib/dommy/navigator.rb', line 399

def clear_watch(id)
  @watches.delete(id)
  nil
end

#get_current_position(success, failure = nil, _options = nil) ⇒ Object Also known as: getCurrentPosition



383
384
385
386
# File 'lib/dommy/navigator.rb', line 383

def get_current_position(success, failure = nil, _options = nil)
  @window.scheduler.queue_microtask(proc { deliver(success, failure) })
  nil
end

#watch_position(success, failure = nil, _options = nil) ⇒ Object Also known as: watchPosition



390
391
392
393
394
395
# File 'lib/dommy/navigator.rb', line 390

def watch_position(success, failure = nil, _options = nil)
  id = (@next_watch_id += 1)
  @watches[id] = [success, failure]
  @window.scheduler.queue_microtask(proc { deliver(success, failure) })
  id
end