Class: Slk::Models::SearchResult
- Inherits:
-
Data
- Object
- Data
- Slk::Models::SearchResult
- 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
-
#channel_id ⇒ Object
readonly
Returns the value of attribute channel_id.
-
#channel_name ⇒ Object
readonly
Returns the value of attribute channel_name.
-
#channel_type ⇒ Object
readonly
Returns the value of attribute channel_type.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#permalink ⇒ Object
readonly
Returns the value of attribute permalink.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#thread_ts ⇒ Object
readonly
Returns the value of attribute thread_ts.
-
#ts ⇒ Object
readonly
Returns the value of attribute ts.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.build_attributes(match, channel) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
.determine_channel_type(channel) ⇒ Object
rubocop:enable Metrics/MethodLength.
- .extract_attachment_images(attachment) ⇒ Object
- .extract_attachments(match) ⇒ Object
- .extract_files(match) ⇒ Object
- .extract_thread_ts(match) ⇒ Object
- .extract_uploaded_files(match) ⇒ Object
- .from_api(match) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#channel_id ⇒ Object (readonly)
Returns the value of attribute channel_id
7 8 9 |
# File 'lib/slk/models/search_result.rb', line 7 def channel_id @channel_id end |
#channel_name ⇒ Object (readonly)
Returns the value of attribute channel_name
7 8 9 |
# File 'lib/slk/models/search_result.rb', line 7 def channel_name @channel_name end |
#channel_type ⇒ Object (readonly)
Returns the value of attribute channel_type
7 8 9 |
# File 'lib/slk/models/search_result.rb', line 7 def channel_type @channel_type end |
#files ⇒ Object (readonly)
Returns the value of attribute files
7 8 9 |
# File 'lib/slk/models/search_result.rb', line 7 def files @files end |
#permalink ⇒ Object (readonly)
Returns the value of attribute permalink
7 8 9 |
# File 'lib/slk/models/search_result.rb', line 7 def permalink @permalink end |
#text ⇒ Object (readonly)
Returns the value of attribute text
7 8 9 |
# File 'lib/slk/models/search_result.rb', line 7 def text @text end |
#thread_ts ⇒ Object (readonly)
Returns the value of attribute thread_ts
7 8 9 |
# File 'lib/slk/models/search_result.rb', line 7 def thread_ts @thread_ts end |
#ts ⇒ Object (readonly)
Returns the value of attribute ts
7 8 9 |
# File 'lib/slk/models/search_result.rb', line 7 def ts @ts end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id
7 8 9 |
# File 'lib/slk/models/search_result.rb', line 7 def user_id @user_id end |
#username ⇒ Object (readonly)
Returns the value of attribute 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.() unless ['blocks'] fallback = ['fallback'] return [] unless fallback return [{ name: fallback, type: 'attachment' }] end ['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.(match) return [] unless match['attachments'] match['attachments'].flat_map { |a| (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 += (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.}" 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_channel ⇒ Object
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
102 103 104 |
# File 'lib/slk/models/search_result.rb', line 102 def dm? %w[im mpim].include?(channel_type) end |
#thread? ⇒ Boolean
98 99 100 |
# File 'lib/slk/models/search_result.rb', line 98 def thread? !thread_ts.nil? end |
#timestamp ⇒ Object
94 95 96 |
# File 'lib/slk/models/search_result.rb', line 94 def Time.at(ts.to_f) end |