Class: Slk::Models::Reaction

Inherits:
Data
  • Object
show all
Defined in:
lib/slk/models/reaction.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, count: 0, users: [], timestamps: nil) ⇒ Reaction

Returns a new instance of Reaction.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/slk/models/reaction.rb', line 15

def initialize(name:, count: 0, users: [], timestamps: nil)
  count_val = count.to_i
  count_val = 0 if count_val.negative? # Normalize invalid negative counts

  super(
    name: name.to_s.freeze,
    count: count_val,
    users: users.freeze,
    timestamps: timestamps&.freeze
  )
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count

Returns:

  • (Object)

    the current value of count



5
6
7
# File 'lib/slk/models/reaction.rb', line 5

def count
  @count
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



5
6
7
# File 'lib/slk/models/reaction.rb', line 5

def name
  @name
end

#timestampsObject (readonly)

Returns the value of attribute timestamps

Returns:

  • (Object)

    the current value of timestamps



5
6
7
# File 'lib/slk/models/reaction.rb', line 5

def timestamps
  @timestamps
end

#usersObject (readonly)

Returns the value of attribute users

Returns:

  • (Object)

    the current value of users



5
6
7
# File 'lib/slk/models/reaction.rb', line 5

def users
  @users
end

Class Method Details

.from_api(data) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/slk/models/reaction.rb', line 6

def self.from_api(data)
  new(
    name: data['name'],
    count: data['count'] || 0,
    users: data['users'] || [],
    timestamps: nil # Will be populated by ReactionEnricher
  )
end

Instance Method Details

#emoji_codeObject



45
46
47
# File 'lib/slk/models/reaction.rb', line 45

def emoji_code
  ":#{name}:"
end

#timestamp_for(user_id) ⇒ Object



41
42
43
# File 'lib/slk/models/reaction.rb', line 41

def timestamp_for(user_id)
  timestamps&.dig(user_id)
end

#timestamps?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/slk/models/reaction.rb', line 37

def timestamps?
  !timestamps.nil? && !timestamps.empty?
end

#to_sObject



49
50
51
# File 'lib/slk/models/reaction.rb', line 49

def to_s
  "#{count} #{emoji_code}"
end

#with_timestamps(timestamp_map) ⇒ Object

Create a new Reaction with timestamps added



28
29
30
31
32
33
34
35
# File 'lib/slk/models/reaction.rb', line 28

def with_timestamps(timestamp_map)
  Reaction.new(
    name: name,
    count: count,
    users: users,
    timestamps: timestamp_map
  )
end