Class: Sendly::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/sendly/types.rb

Overview

Represents an SMS message

Constant Summary collapse

STATUSES =

Message status constants (sending removed - doesn’t exist in database)

%w[queued sent delivered failed bounced retrying].freeze
SENDER_TYPES =

Sender type constants

%w[number_pool alphanumeric sandbox].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Message

Returns a new instance of Message.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sendly/types.rb', line 72

def initialize(data)
  @id = data["id"]
  @to = data["to"]
  @from = data["from"]
  @text = data["text"]
  @status = data["status"]
  @direction = data["direction"] || "outbound"
  @error = data["error"]
  @segments = data["segments"] || 1
  @credits_used = data["creditsUsed"] || 0
  @is_sandbox = data["isSandbox"] || false
  @sender_type = data["senderType"]
  @telnyx_message_id = data["telnyxMessageId"]
  @warning = data["warning"]
  @sender_note = data["senderNote"]
  @created_at = parse_time(data["createdAt"])
  @delivered_at = parse_time(data["deliveredAt"])
  @error_code = data["errorCode"]
  @retry_count = data["retryCount"] || 0
  @metadata = data["metadata"]
  @ai_metadata = data["aiMetadata"]
end

Instance Attribute Details

#ai_metadataHash? (readonly)

Returns AI classification metadata for inbound messages.

Returns:

  • (Hash, nil)

    AI classification metadata for inbound messages



64
65
66
# File 'lib/sendly/types.rb', line 64

def 
  @ai_metadata
end

#created_atTime? (readonly)

Returns Creation timestamp.

Returns:

  • (Time, nil)

    Creation timestamp



49
50
51
# File 'lib/sendly/types.rb', line 49

def created_at
  @created_at
end

#credits_usedInteger (readonly)

Returns Credits used.

Returns:

  • (Integer)

    Credits used



31
32
33
# File 'lib/sendly/types.rb', line 31

def credits_used
  @credits_used
end

#delivered_atTime? (readonly)

Returns Delivery timestamp.

Returns:

  • (Time, nil)

    Delivery timestamp



52
53
54
# File 'lib/sendly/types.rb', line 52

def delivered_at
  @delivered_at
end

#directionString (readonly)

Returns Message direction (outbound or inbound).

Returns:

  • (String)

    Message direction (outbound or inbound)



22
23
24
# File 'lib/sendly/types.rb', line 22

def direction
  @direction
end

#errorString? (readonly)

Returns Error message if failed.

Returns:

  • (String, nil)

    Error message if failed



25
26
27
# File 'lib/sendly/types.rb', line 25

def error
  @error
end

#error_codeString? (readonly)

Returns Error code if delivery failed.

Returns:

  • (String, nil)

    Error code if delivery failed



55
56
57
# File 'lib/sendly/types.rb', line 55

def error_code
  @error_code
end

#fromString? (readonly)

Returns Sender ID or phone number.

Returns:

  • (String, nil)

    Sender ID or phone number



13
14
15
# File 'lib/sendly/types.rb', line 13

def from
  @from
end

#idString (readonly)

Returns Unique message identifier.

Returns:

  • (String)

    Unique message identifier



7
8
9
# File 'lib/sendly/types.rb', line 7

def id
  @id
end

#is_sandboxBoolean (readonly)

Returns Whether sent in sandbox mode.

Returns:

  • (Boolean)

    Whether sent in sandbox mode



34
35
36
# File 'lib/sendly/types.rb', line 34

def is_sandbox
  @is_sandbox
end

#metadataHash? (readonly)

Returns Custom metadata attached to the message.

Returns:

  • (Hash, nil)

    Custom metadata attached to the message



61
62
63
# File 'lib/sendly/types.rb', line 61

def 
  @metadata
end

#retry_countInteger (readonly)

Returns Number of delivery retry attempts.

Returns:

  • (Integer)

    Number of delivery retry attempts



58
59
60
# File 'lib/sendly/types.rb', line 58

def retry_count
  @retry_count
end

#segmentsInteger (readonly)

Returns Number of SMS segments.

Returns:

  • (Integer)

    Number of SMS segments



28
29
30
# File 'lib/sendly/types.rb', line 28

def segments
  @segments
end

#sender_noteString? (readonly)

Returns Note about sender behavior.

Returns:

  • (String, nil)

    Note about sender behavior



46
47
48
# File 'lib/sendly/types.rb', line 46

def sender_note
  @sender_note
end

#sender_typeString? (readonly)

Returns How the message was sent (number_pool, alphanumeric, sandbox).

Returns:

  • (String, nil)

    How the message was sent (number_pool, alphanumeric, sandbox)



37
38
39
# File 'lib/sendly/types.rb', line 37

def sender_type
  @sender_type
end

#statusString (readonly)

Returns Delivery status.

Returns:

  • (String)

    Delivery status



19
20
21
# File 'lib/sendly/types.rb', line 19

def status
  @status
end

#telnyx_message_idString? (readonly)

Returns Carrier message ID for tracking.

Returns:

  • (String, nil)

    Carrier message ID for tracking



40
41
42
# File 'lib/sendly/types.rb', line 40

def telnyx_message_id
  @telnyx_message_id
end

#textString (readonly)

Returns Message content.

Returns:

  • (String)

    Message content



16
17
18
# File 'lib/sendly/types.rb', line 16

def text
  @text
end

#toString (readonly)

Returns Recipient phone number.

Returns:

  • (String)

    Recipient phone number



10
11
12
# File 'lib/sendly/types.rb', line 10

def to
  @to
end

#warningString? (readonly)

Returns Warning message.

Returns:

  • (String, nil)

    Warning message



43
44
45
# File 'lib/sendly/types.rb', line 43

def warning
  @warning
end

Instance Method Details

#delivered?Boolean

Check if message was delivered

Returns:

  • (Boolean)


97
98
99
# File 'lib/sendly/types.rb', line 97

def delivered?
  status == "delivered"
end

#failed?Boolean

Check if message failed

Returns:

  • (Boolean)


103
104
105
# File 'lib/sendly/types.rb', line 103

def failed?
  status == "failed"
end

#pending?Boolean

Check if message is pending

Returns:

  • (Boolean)


109
110
111
# File 'lib/sendly/types.rb', line 109

def pending?
  %w[queued sending sent].include?(status)
end

#to_hHash

Convert to hash

Returns:

  • (Hash)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sendly/types.rb', line 115

def to_h
  {
    id: id,
    to: to,
    from: from,
    text: text,
    status: status,
    direction: direction,
    error: error,
    segments: segments,
    credits_used: credits_used,
    is_sandbox: is_sandbox,
    sender_type: sender_type,
    telnyx_message_id: telnyx_message_id,
    warning: warning,
    sender_note: sender_note,
    created_at: created_at&.iso8601,
    delivered_at: delivered_at&.iso8601,
    error_code: error_code,
    retry_count: retry_count,
    metadata: ,
    ai_metadata: 
  }.compact
end