Class: Plivo::Resources::MessagesInterface

Inherits:
Base::ResourceInterface show all
Defined in:
lib/plivo/resources/messages.rb

Constant Summary

Constants included from Utils

Utils::TYPE_WHITELIST

Instance Method Summary collapse

Methods included from Utils

expected_type?, expected_value?, raise_invalid_request, valid_account?, valid_mainaccount?, valid_param?, valid_signature?, valid_subaccount?

Constructor Details

#initialize(client, resource_list_json = nil) ⇒ MessagesInterface

Returns a new instance of MessagesInterface.



31
32
33
34
35
36
# File 'lib/plivo/resources/messages.rb', line 31

def initialize(client, resource_list_json = nil)
  @_name = 'Message'
  @_resource_type = Message
  @_identifier_string = 'message_uuid'
  super
end

Instance Method Details

#create(src, dst, text, options = nil, powerpack_uuid = nil) ⇒ Object

Parameters:

  • src (String)
  • dst (Array)
  • text (String)
  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :type (String)

    The type of message. Should be ‘sms` for a text message. Defaults to `sms`.

  • :url (String)

    The URL to which with the status of the message is sent. The following parameters are sent to the URL:

    • To - Phone number of the recipient

    • From - Phone number of the sender

    • Status - Status of the message including “queued”, “sent”, “failed”, “delivered”, “undelivered” or “rejected”

    • MessageUUID - The unique ID for the message

    • ParentMessageUUID - ID of the parent message (see notes about long SMS below)

    • PartInfo - Specifies the sequence of the message (useful for long messages split into multiple text messages; see notes about long SMS below)

    • TotalRate - Total rate per sms

    • TotalAmount - Total cost of sending the sms (TotalRate * Units)

    • Units - Number of units into which a long SMS was split

    • MCC - Mobile Country Code (see here https://en.wikipedia.org/wiki/Mobile_country_code for more details)

    • MNC - Mobile Network Code (see here https://en.wikipedia.org/wiki/Mobile_country_code for more details)

    • ErrorCode - Delivery Response code returned by the carrier attempting the delivery. See Supported error codes https://www.plivo.com/docs/api/message/#standard-plivo-error-codes.

  • :method (String)

    The method used to call the url. Defaults to POST.

  • :log (String)

    If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.

  • :trackable (String)

    set to false



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/plivo/resources/messages.rb', line 64

def create(src, dst, text, options = nil, powerpack_uuid = nil)
  valid_param?(:src, src, [Integer, String, Symbol], false)
  valid_param?(:text, text, [String, Symbol], true)
  valid_param?(:dst, dst, Array, true)
  valid_param?(:powerpack_uuid, powerpack_uuid, [String, Symbol], false)
  dst.each do |dst_num|
    valid_param?(:dst_num, dst_num, [Integer, String, Symbol], true)
  end

  if dst.include? src
    raise InvalidRequestError, 'src and dst cannot be same'
  end

  if src.nil? && powerpack_uuid.nil?
    raise InvalidRequestError, 'src and powerpack uuid both cannot be nil'
  end

  if !src.nil? && !powerpack_uuid.nil?
    raise InvalidRequestError, 'src and powerpack uuid both cannot be present'
  end

  params = {
    src: src,
    dst: dst.join('<'),
    text: text,
    powerpack_uuid: powerpack_uuid
  }

  return perform_create(params) if options.nil?
  valid_param?(:options, options, Hash, true)

  if options.key?(:type) &&
     valid_param?(:type, options[:type], String, true, 'sms')
    params[:type] = options[:type]
  end

  if options.key?(:url) && valid_param?(:url, options[:url], String, true)
    params[:url] = options[:url]
    if options.key?(:method) &&
       valid_param?(:method, options[:method], String, true, %w[POST GET])
      params[:method] = options[:method]
    else
      params[:method] = 'POST'
    end
  end

  if options.key?(:log) &&
     valid_param?(:log, options[:log], [TrueClass, FalseClass], true)
    params[:log] = options[:log]
  end

  if options.key?(:trackable) &&
    valid_param?(:trackable, options[:trackable], [TrueClass, FalseClass], true)
   params[:trackable] = options[:trackable]
  end
  perform_create(params)
end

#eachObject



183
184
185
186
187
188
189
190
191
# File 'lib/plivo/resources/messages.rb', line 183

def each
  offset = 0
  loop do
    message_list = list(offset: offset)
    message_list[:objects].each { |message| yield message }
    offset += 20
    return unless message_list.length == 20
  end
end

#get(message_uuid) ⇒ Object

Parameters:

  • message_uuid (String)


39
40
41
# File 'lib/plivo/resources/messages.rb', line 39

def get(message_uuid)
  perform_get(message_uuid)
end

#list(options = nil) ⇒ Object

Parameters:

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :subaccount (String)

    The id of the subaccount, if message details of the subaccount is needed.

  • :message_direction (String)

    Filter the results by message direction. The valid inputs are inbound and outbound.

  • :message_time (String)

    Filter out messages according to the time of completion. The filter can be used in the following five forms:

    • message_time: The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received at 2012-03-21 11:47, use message_time=2012-03-21 11:47

    • message_time__gt: gt stands for greater than. The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received after 2012-03-21 11:47, use message_time__gt=2012-03-21 11:47

    • message_time__gte: gte stands for greater than or equal. The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received after or exactly at 2012-03-21 11:47, use message_time__gte=2012-03-21 11:47

    • message_time__lt: lt stands for lesser than. The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received before 2012-03-21 11:47, use message_time__lt=2012-03-21 11:47

    • message_time__lte: lte stands for lesser than or equal. The format expected is YYYY-MM-DD HH:MM[:ss]. Eg:- To get all messages that were sent/received before or exactly at 2012-03-21 11:47, use message_time__lte=2012-03-21 11:47

    • Note: The above filters can be combined to get messages that were sent/received in a particular time range. The timestamps need to be UTC timestamps.

  • :message_state (String)

    Status value of the message, is one of “queued”, “sent”, “failed”, “delivered”, “undelivered” or “rejected”

  • :limit (Int)

    Used to display the number of results per page. The maximum number of results that can be fetched is 20.

  • :offset (Int)

    Denotes the number of value items by which the results should be offset. Eg:- If the result contains a 1000 values and limit is set to 10 and offset is set to 705, then values 706 through 715 are displayed in the results. This parameter is also used for pagination of the results.

  • :error_code (String)

    Delivery Response code returned by the carrier attempting the delivery. See Supported error codes https://www.plivo.com/docs/api/message/#standard-plivo-error-codes.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/plivo/resources/messages.rb', line 136

def list(options = nil)
  return perform_list if options.nil?
  valid_param?(:options, options, Hash, true)

  params = {}
  params_expected = %i[
    subaccount message_time message_time__gt message_time__gte
    message_time__lt message_time__lte error_code
  ]
  params_expected.each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [String, Symbol], true)
      params[param] = options[param]
    end
  end

  %i[offset limit].each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [Integer, Integer], true)
      params[param] = options[param]
    end
  end

  if options.key?(:message_direction) &&
     valid_param?(:message_direction, options[:message_direction],
                  [String, Symbol], true, %w[inbound outbound])
    params[:message_direction] = options[:message_direction]
  end

  if options.key?(:message_state) &&
     valid_param?(:message_state, options[:message_state],
                  [String, Symbol], true, %w[queued sent failed delivered
                                             undelivered rejected])
    params[:message_state] = options[:message_state]
  end

  if options.key?(:limit) &&
     (options[:limit] > 20 || options[:limit] <= 0)
    raise_invalid_request('The maximum number of results that can be '\
    "fetched is 20. limit can't be more than 20 or less than 1")
  end

  raise_invalid_request("Offset can't be negative") if options.key?(:offset) && options[:offset] < 0

  perform_list(params)
end