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: https://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.



387
388
389
390
391
392
393
# File 'lib/dommy/navigator.rb', line 387

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

Instance Method Details

#__js_call__(method, args) ⇒ Object



434
435
436
437
438
439
440
441
442
443
# File 'lib/dommy/navigator.rb', line 434

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



404
405
406
407
# File 'lib/dommy/navigator.rb', line 404

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.



396
397
398
399
400
# File 'lib/dommy/navigator.rb', line 396

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



425
426
427
428
# File 'lib/dommy/navigator.rb', line 425

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

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



409
410
411
412
# File 'lib/dommy/navigator.rb', line 409

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



416
417
418
419
420
421
# File 'lib/dommy/navigator.rb', line 416

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