Class: Line::Bot::V2::Webhook::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/line/bot/v2/webhook/model/event.rb

Overview

Webhook event

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, source: nil, timestamp:, mode:, webhook_event_id:, delivery_context:, **dynamic_attributes) ⇒ Event



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/line/bot/v2/webhook/model/event.rb', line 41

def initialize(
  type:,
  source: nil,
  timestamp:,
  mode:,
  webhook_event_id:,
  delivery_context:,
  **dynamic_attributes
)
  
  @type = type
  @source = source.is_a?(Line::Bot::V2::Webhook::Source) || source.nil? ? source : Line::Bot::V2::Webhook::Source.create(**source) # steep:ignore
  @timestamp = timestamp
  @mode = mode
  @webhook_event_id = webhook_event_id
  @delivery_context = delivery_context.is_a?(Line::Bot::V2::Webhook::DeliveryContext) ? delivery_context : Line::Bot::V2::Webhook::DeliveryContext.create(**delivery_context) # steep:ignore

  dynamic_attributes.each do |key, value|
    self.class.attr_accessor key

    if value.is_a?(Hash)
      struct_klass = Struct.new(*value.keys.map(&:to_sym))
      struct_values = value.map { |_k, v| v.is_a?(Hash) ? Line::Bot::V2::Utils.hash_to_struct(v) : v }
      instance_variable_set("@#{key}", struct_klass.new(*struct_values))
    else
      instance_variable_set("@#{key}", value)
    end
  end
end

Instance Attribute Details

#delivery_contextDeliveryContext



33
34
35
# File 'lib/line/bot/v2/webhook/model/event.rb', line 33

def delivery_context
  @delivery_context
end

#modeString



27
28
29
# File 'lib/line/bot/v2/webhook/model/event.rb', line 27

def mode
  @mode
end

#sourceSource?



21
22
23
# File 'lib/line/bot/v2/webhook/model/event.rb', line 21

def source
  @source
end

#timestampInteger



24
25
26
# File 'lib/line/bot/v2/webhook/model/event.rb', line 24

def timestamp
  @timestamp
end

#typeString



18
19
20
# File 'lib/line/bot/v2/webhook/model/event.rb', line 18

def type
  @type
end

#webhook_event_idString



30
31
32
# File 'lib/line/bot/v2/webhook/model/event.rb', line 30

def webhook_event_id
  @webhook_event_id
end

Class Method Details

.create(args) ⇒ Line::Bot::V2::Webhook::Event

Create an instance of the class from a hash



74
75
76
77
78
79
# File 'lib/line/bot/v2/webhook/model/event.rb', line 74

def self.create(args) # steep:ignore
  symbolized_args = Line::Bot::V2::Utils.deep_symbolize(args)
  klass = detect_class(type: symbolized_args[:type])
  return klass.new(**symbolized_args) if klass # steep:ignore
  return new(**symbolized_args) # steep:ignore
end

Instance Method Details

#==(other) ⇒ Boolean



83
84
85
86
87
88
89
# File 'lib/line/bot/v2/webhook/model/event.rb', line 83

def ==(other)
  return false unless self.class == other.class

  instance_variables.all? do |var|
      instance_variable_get(var) == other.instance_variable_get(var)
  end
end

#hashInteger



92
93
94
# File 'lib/line/bot/v2/webhook/model/event.rb', line 92

def hash
  [self.class, *instance_variables.map { |var| instance_variable_get(var) }].hash
end