Class: Courier::Send::ElementalNode

Inherits:
Object
  • Object
show all
Defined in:
lib/trycourier/send/types/elemental_node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(member:, discriminant:) ⇒ Send::ElementalNode

Parameters:

  • member (Object)
  • discriminant (String)


23
24
25
26
27
28
# File 'lib/trycourier/send/types/elemental_node.rb', line 23

def initialize(member:, discriminant:)
  # @type [Object]
  @member = member
  # @type [String]
  @discriminant = discriminant
end

Instance Attribute Details

#discriminantObject (readonly)

Returns the value of attribute discriminant.



16
17
18
# File 'lib/trycourier/send/types/elemental_node.rb', line 16

def discriminant
  @discriminant
end

#memberObject (readonly)

Returns the value of attribute member.



16
17
18
# File 'lib/trycourier/send/types/elemental_node.rb', line 16

def member
  @member
end

Class Method Details

.action(member:) ⇒ Send::ElementalNode

Parameters:

Returns:



147
148
149
# File 'lib/trycourier/send/types/elemental_node.rb', line 147

def self.action(member:)
  new(member: member, discriminant: "action")
end

.channel(member:) ⇒ Send::ElementalNode

Parameters:

Returns:



135
136
137
# File 'lib/trycourier/send/types/elemental_node.rb', line 135

def self.channel(member:)
  new(member: member, discriminant: "channel")
end

.divider(member:) ⇒ Send::ElementalNode

Parameters:

Returns:



153
154
155
# File 'lib/trycourier/send/types/elemental_node.rb', line 153

def self.divider(member:)
  new(member: member, discriminant: "divider")
end

.from_json(json_object:) ⇒ Send::ElementalNode

Deserialize a JSON object to an instance of ElementalNode

Parameters:

  • json_object (JSON)

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/trycourier/send/types/elemental_node.rb', line 34

def self.from_json(json_object:)
  struct = JSON.parse(json_object, object_class: OpenStruct)
  member = case struct.type
           when "text"
             Send::ElementalTextNode.from_json(json_object: json_object)
           when "meta"
             Send::ElementalMetaNode.from_json(json_object: json_object)
           when "channel"
             Send::ElementalChannelNode.from_json(json_object: json_object)
           when "image"
             Send::ElementalImageNode.from_json(json_object: json_object)
           when "action"
             Send::ElementalActionNode.from_json(json_object: json_object)
           when "divider"
             Send::ElementalDividerNode.from_json(json_object: json_object)
           when "group"
             Send::ElementalGroupNode.from_json(json_object: json_object)
           when "quote"
             Send::ElementalQuoteNode.from_json(json_object: json_object)
           else
             Send::ElementalTextNode.from_json(json_object: json_object)
           end
  new(member: member, discriminant: struct.type)
end

.group(member:) ⇒ Send::ElementalNode

Parameters:

Returns:



159
160
161
# File 'lib/trycourier/send/types/elemental_node.rb', line 159

def self.group(member:)
  new(member: member, discriminant: "group")
end

.image(member:) ⇒ Send::ElementalNode

Parameters:

Returns:



141
142
143
# File 'lib/trycourier/send/types/elemental_node.rb', line 141

def self.image(member:)
  new(member: member, discriminant: "image")
end

.meta(member:) ⇒ Send::ElementalNode

Parameters:

Returns:



129
130
131
# File 'lib/trycourier/send/types/elemental_node.rb', line 129

def self.meta(member:)
  new(member: member, discriminant: "meta")
end

.quote(member:) ⇒ Send::ElementalNode

Parameters:

Returns:



165
166
167
# File 'lib/trycourier/send/types/elemental_node.rb', line 165

def self.quote(member:)
  new(member: member, discriminant: "quote")
end

.text(member:) ⇒ Send::ElementalNode

Parameters:

Returns:



123
124
125
# File 'lib/trycourier/send/types/elemental_node.rb', line 123

def self.text(member:)
  new(member: member, discriminant: "text")
end

.validate_raw(obj:) ⇒ Void

Leveraged for Union-type generation, validate_raw attempts to parse the given hash and check each fields type against the current object’s property definitions.

Parameters:

  • obj (Object)

Returns:

  • (Void)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/trycourier/send/types/elemental_node.rb', line 90

def self.validate_raw(obj:)
  case obj.type
  when "text"
    Send::ElementalTextNode.validate_raw(obj: obj)
  when "meta"
    Send::ElementalMetaNode.validate_raw(obj: obj)
  when "channel"
    Send::ElementalChannelNode.validate_raw(obj: obj)
  when "image"
    Send::ElementalImageNode.validate_raw(obj: obj)
  when "action"
    Send::ElementalActionNode.validate_raw(obj: obj)
  when "divider"
    Send::ElementalDividerNode.validate_raw(obj: obj)
  when "group"
    Send::ElementalGroupNode.validate_raw(obj: obj)
  when "quote"
    Send::ElementalQuoteNode.validate_raw(obj: obj)
  else
    raise("Passed value matched no type within the union, validation failed.")
  end
end

Instance Method Details

#is_a?(obj) ⇒ Boolean

For Union Types, is_a? functionality is delegated to the wrapped member.

Parameters:

  • obj (Object)

Returns:

  • (Boolean)


117
118
119
# File 'lib/trycourier/send/types/elemental_node.rb', line 117

def is_a?(obj)
  @member.is_a?(obj)
end

#kind_of?Object



19
# File 'lib/trycourier/send/types/elemental_node.rb', line 19

alias kind_of? is_a?

#to_json(*_args) ⇒ JSON

For Union Types, to_json functionality is delegated to the wrapped member.

Returns:

  • (JSON)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/trycourier/send/types/elemental_node.rb', line 62

def to_json(*_args)
  case @discriminant
  when "text"
    { **@member.to_json, type: @discriminant }.to_json
  when "meta"
    { **@member.to_json, type: @discriminant }.to_json
  when "channel"
    { **@member.to_json, type: @discriminant }.to_json
  when "image"
    { **@member.to_json, type: @discriminant }.to_json
  when "action"
    { **@member.to_json, type: @discriminant }.to_json
  when "divider"
    { **@member.to_json, type: @discriminant }.to_json
  when "group"
    { **@member.to_json, type: @discriminant }.to_json
  when "quote"
    { **@member.to_json, type: @discriminant }.to_json
  else
    { "type": @discriminant, value: @member }.to_json
  end
  @member.to_json
end