Class: Dommy::Notification

Inherits:
Object
  • Object
show all
Includes:
EventTarget
Defined in:
lib/dommy/notification.rb

Overview

‘Notification` polyfill. Real browsers prompt the user; dommy exposes the permission state as a class-level slot tests can toggle via `Notification.set_permission(“granted”)`.

Spec: notifications.spec.whatwg.org/

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventTarget

#__deliver_event__, #add_event_listener, #dispatch_event, #invoke_listener, #remove_event_listener

Constructor Details

#initialize(window, title, options = nil) ⇒ Notification

Returns a new instance of Notification.



38
39
40
41
42
43
44
45
46
47
# File 'lib/dommy/notification.rb', line 38

def initialize(window, title, options = nil)
  @window = window
  @title = title.to_s
  opts = options.is_a?(Hash) ? options : {}
  @body = (opts["body"] || opts[:body] || "").to_s
  @icon = (opts["icon"] || opts[:icon] || "").to_s
  @tag = (opts["tag"] || opts[:tag] || "").to_s
  @data = opts["data"] || opts[:data]
  @closed = false
end

Class Attribute Details

.permissionObject (readonly)

Returns the value of attribute permission.



15
16
17
# File 'lib/dommy/notification.rb', line 15

def permission
  @permission
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



36
37
38
# File 'lib/dommy/notification.rb', line 36

def body
  @body
end

#dataObject (readonly)

Returns the value of attribute data.



36
37
38
# File 'lib/dommy/notification.rb', line 36

def data
  @data
end

#iconObject (readonly)

Returns the value of attribute icon.



36
37
38
# File 'lib/dommy/notification.rb', line 36

def icon
  @icon
end

#tagObject (readonly)

Returns the value of attribute tag.



36
37
38
# File 'lib/dommy/notification.rb', line 36

def tag
  @tag
end

#titleObject (readonly)

Returns the value of attribute title.



36
37
38
# File 'lib/dommy/notification.rb', line 36

def title
  @title
end

Class Method Details

.__set_permission__(value) ⇒ Object



17
18
19
# File 'lib/dommy/notification.rb', line 17

def __set_permission__(value)
  @permission = value.to_s
end

.request_permission(window, callback = nil) ⇒ Object

Asynchronous spec API: returns a Promise (here a value that ‘.await`-able). Callbacks receive the current permission value.



24
25
26
27
28
29
30
31
32
33
# File 'lib/dommy/notification.rb', line 24

def request_permission(window, callback = nil)
  promise = PromiseValue.resolve(window, @permission)
  if callback.respond_to?(:__js_call__)
    callback.__js_call__("call", [@permission])
  elsif callback.respond_to?(:call)
    callback.call(@permission)
  end

  promise
end

Instance Method Details

#__event_parent__Object



85
86
87
# File 'lib/dommy/notification.rb', line 85

def __event_parent__
  nil
end

#__js_call__(method, args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dommy/notification.rb', line 72

def __js_call__(method, args)
  case method
  when "close"
    close
  when "addEventListener"
    add_event_listener(args[0], args[1], args[2])
  when "removeEventListener"
    remove_event_listener(args[0], args[1])
  when "dispatchEvent"
    dispatch_event(args[0])
  end
end

#__js_get__(key) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dommy/notification.rb', line 57

def __js_get__(key)
  case key
  when "title"
    @title
  when "body"
    @body
  when "icon"
    @icon
  when "tag"
    @tag
  when "data"
    @data
  end
end

#closeObject



49
50
51
52
53
54
55
# File 'lib/dommy/notification.rb', line 49

def close
  return if @closed

  @closed = true
  dispatch_event(Event.new("close"))
  nil
end