Class: Usps::Support::Models::Toast

Inherits:
Object
  • Object
show all
Defined in:
lib/usps/support/models/toast.rb

Overview

Toast constructor

Defined Under Namespace

Modules: Reserved

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Toast

Returns a new instance of Toast.



21
22
23
24
25
26
27
# File 'lib/usps/support/models/toast.rb', line 21

def initialize(options)
  @type = options[:type] || :NOTICE
  @title = options[:title]
  @body = options[:body]
  @timeout = options[:timeout] || 4_000
  @id = options[:id]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/usps/support/models/toast.rb', line 7

def body
  @body
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/usps/support/models/toast.rb', line 7

def id
  @id
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/usps/support/models/toast.rb', line 7

def timeout
  @timeout
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/usps/support/models/toast.rb', line 7

def title
  @title
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/usps/support/models/toast.rb', line 7

def type
  @type
end

Class Method Details

.errorObject



17
18
19
# File 'lib/usps/support/models/toast.rb', line 17

def self.error(**)
  new(type: :ERROR, **)
end

.noticeObject



13
14
15
# File 'lib/usps/support/models/toast.rb', line 13

def self.notice(**)
  new(type: :NOTICE, **)
end

.successObject



9
10
11
# File 'lib/usps/support/models/toast.rb', line 9

def self.success(**)
  new(type: :SUCCESS, **)
end

Instance Method Details

#==(other) ⇒ Object



54
# File 'lib/usps/support/models/toast.rb', line 54

def ==(other) = to_h == other.to_h

#broadcast!Object



29
30
31
# File 'lib/usps/support/models/toast.rb', line 29

def broadcast!
  BroadcastToastChannel.broadcast!(title, body, timeout:, type:, id:)
end

#dismiss!Object



33
34
35
# File 'lib/usps/support/models/toast.rb', line 33

def dismiss!
  BroadcastToastChannel.dismiss!(id)
end

#to_hObject



44
45
46
47
48
49
50
51
52
# File 'lib/usps/support/models/toast.rb', line 44

def to_h
  {
    type: type || :NOTICE,
    title:,
    body:,
    timeout:,
    id:
  }
end

#to_json(*_) ⇒ Object



56
57
58
59
60
# File 'lib/usps/support/models/toast.rb', line 56

def to_json(*_)
  validate!

  JSON.dump(to_h)
end

#to_sObject Also known as: inspect



37
38
39
40
41
# File 'lib/usps/support/models/toast.rb', line 37

def to_s
  attributes = to_h.map { |key, value| "@#{key}=#{value.inspect}" }.join(' ')

  "#<Toast #{attributes}>"
end