Class: Slk::Models::SearchResult

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

Overview

Value object for search.messages API results Search results include channel info inline, unlike regular messages

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#channel_idObject (readonly)

Returns the value of attribute channel_id

Returns:

  • (Object)

    the current value of channel_id



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def channel_id
  @channel_id
end

#channel_nameObject (readonly)

Returns the value of attribute channel_name

Returns:

  • (Object)

    the current value of channel_name



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def channel_name
  @channel_name
end

#channel_typeObject (readonly)

Returns the value of attribute channel_type

Returns:

  • (Object)

    the current value of channel_type



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def channel_type
  @channel_type
end

#filesObject (readonly)

Returns the value of attribute files

Returns:

  • (Object)

    the current value of files



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def files
  @files
end

Returns the value of attribute permalink

Returns:

  • (Object)

    the current value of permalink



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def permalink
  @permalink
end

#textObject (readonly)

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def text
  @text
end

#thread_tsObject (readonly)

Returns the value of attribute thread_ts

Returns:

  • (Object)

    the current value of thread_ts



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def thread_ts
  @thread_ts
end

#tsObject (readonly)

Returns the value of attribute ts

Returns:

  • (Object)

    the current value of ts



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def ts
  @ts
end

#user_idObject (readonly)

Returns the value of attribute user_id

Returns:

  • (Object)

    the current value of user_id



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def user_id
  @user_id
end

#usernameObject (readonly)

Returns the value of attribute username

Returns:

  • (Object)

    the current value of username



7
8
9
# File 'lib/slk/models/search_result.rb', line 7

def username
  @username
end

Class Method Details

.build_attributes(match, channel) ⇒ Object

rubocop:disable Metrics/MethodLength



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/slk/models/search_result.rb', line 25

def self.build_attributes(match, channel)
  {
    ts: match['ts'],
    user_id: match['user'] || match['username'],
    username: match['username'],
    text: match['text'] || '',
    channel_id: channel['id'],
    channel_name: channel['name'],
    channel_type: determine_channel_type(channel),
    thread_ts: extract_thread_ts(match),
    permalink: match['permalink'],
    files: extract_files(match)
  }
end

.determine_channel_type(channel) ⇒ Object

rubocop:enable Metrics/MethodLength



74
75
76
77
78
79
# File 'lib/slk/models/search_result.rb', line 74

def self.determine_channel_type(channel)
  return 'im' if channel['is_im']
  return 'mpim' if channel['is_mpim']

  'channel'
end

.extract_attachment_images(attachment) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/slk/models/search_result.rb', line 58

def self.extract_attachment_images(attachment)
  unless attachment['blocks']
    fallback = attachment['fallback']
    return [] unless fallback

    return [{ name: fallback, type: 'attachment' }]
  end

  attachment['blocks'].filter_map do |block|
    next unless block['type'] == 'image'

    { name: block.dig('title', 'text') || 'Image', type: 'attachment' }
  end
end

.extract_attachments(match) ⇒ Object



52
53
54
55
56
# File 'lib/slk/models/search_result.rb', line 52

def self.extract_attachments(match)
  return [] unless match['attachments']

  match['attachments'].flat_map { |a| extract_attachment_images(a) }
end

.extract_files(match) ⇒ Object



40
41
42
43
44
# File 'lib/slk/models/search_result.rb', line 40

def self.extract_files(match)
  files = extract_uploaded_files(match)
  files += extract_attachments(match)
  files
end

.extract_thread_ts(match) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/slk/models/search_result.rb', line 81

def self.extract_thread_ts(match)
  return nil unless match['permalink']

  # Extract thread_ts from permalink URL if present
  uri = URI.parse(match['permalink'])
  params = URI.decode_www_form(uri.query || '')
  params.find { |k, _| k == 'thread_ts' }&.last
rescue URI::InvalidURIError => e
  # Log for debugging - malformed permalinks from API should be rare
  warn "Invalid permalink URI: #{match['permalink']}: #{e.message}" if ENV['DEBUG']
  nil
end

.extract_uploaded_files(match) ⇒ Object



46
47
48
49
50
# File 'lib/slk/models/search_result.rb', line 46

def self.extract_uploaded_files(match)
  return [] unless match['files']

  match['files'].map { |f| { name: f['name'], type: f['filetype'] } }
end

.from_api(match) ⇒ Object



19
20
21
22
# File 'lib/slk/models/search_result.rb', line 19

def self.from_api(match)
  channel = match['channel'] || {}
  new(**build_attributes(match, channel))
end

Instance Method Details

#display_channelObject



106
107
108
109
110
111
112
# File 'lib/slk/models/search_result.rb', line 106

def display_channel
  if dm?
    "@#{channel_name}"
  else
    "##{channel_name}"
  end
end

#dm?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/slk/models/search_result.rb', line 102

def dm?
  %w[im mpim].include?(channel_type)
end

#thread?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/slk/models/search_result.rb', line 98

def thread?
  !thread_ts.nil?
end

#timestampObject



94
95
96
# File 'lib/slk/models/search_result.rb', line 94

def timestamp
  Time.at(ts.to_f)
end