Class: Dommy::Geolocation

Inherits:
Object
  • Object
show all
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 `set_position(coords)` or `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

Constructor Details

#initialize(window) ⇒ Geolocation

Returns a new instance of Geolocation.



351
352
353
354
355
356
357
# File 'lib/dommy/navigator.rb', line 351

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

Instance Method Details

#__js_call__(method, args) ⇒ Object



396
397
398
399
400
401
402
403
404
405
# File 'lib/dommy/navigator.rb', line 396

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

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

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



368
369
370
371
# File 'lib/dommy/navigator.rb', line 368

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

#__set_position__(coords = {}) ⇒ Object

Test seam: install a mock position.



360
361
362
363
364
# File 'lib/dommy/navigator.rb', line 360

def __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



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

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

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



373
374
375
376
# File 'lib/dommy/navigator.rb', line 373

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



380
381
382
383
384
385
# File 'lib/dommy/navigator.rb', line 380

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