File: direct_message_event.rb

package info (click to toggle)
ruby-twitter 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,840 kB
  • sloc: ruby: 10,919; makefile: 6
file content (44 lines) | stat: -rw-r--r-- 1,247 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'twitter/creatable'
require 'twitter/entities'
require 'twitter/identity'

module Twitter
  class DirectMessageEvent < Twitter::Identity
    include Twitter::Creatable
    include Twitter::Entities

    attr_reader :created_timestamp

    object_attr_reader :DirectMessage, :direct_message

    def initialize(attrs)
      attrs = read_from_response(attrs)
      text = attrs.dig(:message_create, :message_data, :text)
      urls = attrs.dig(:message_create, :message_data, :entities, :urls)

      text.gsub!(urls[0][:url], urls[0][:expanded_url]) if urls.any?

      attrs[:direct_message] = build_direct_message(attrs, text)
      super
    end

  private

    # @return [Hash] Normalized hash of attrs
    def read_from_response(attrs)
      attrs[:event].nil? ? attrs : attrs[:event]
    end

    def build_direct_message(attrs, text)
      recipient_id = attrs[:message_create][:target][:recipient_id].to_i
      sender_id = attrs[:message_create][:sender_id].to_i
      {id: attrs[:id].to_i,
       created_at: Time.at(attrs[:created_timestamp].to_i / 1000.0),
       sender: {id: sender_id},
       sender_id: sender_id,
       recipient: {id: recipient_id},
       recipient_id: recipient_id,
       text: text}
    end
  end
end