Class: MitakeSms::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mitake_sms/client.rb

Defined Under Namespace

Classes: AuthenticationError, Error, InvalidRequestError, ServerError

Constant Summary collapse

LINE_BREAK =

The Mitake API represents a line break inside smbody as ASCII code 6.

6.chr
BATCH_LIMIT =
500
CHARSET =

The API also accepts Big5, but it only labels the payload and never converts it, so sending anything other than UTF8 just invites mojibake.

'UTF8'
FIELD_SEPARATOR =
'$$'
STRUCTURAL_FIELDS =

Every SmBulkSend field except smbody, in wire order. A '$$' or a line break in any of these shifts the remaining fields of the row.

%i[client_id to dlvtime vldtime destname response_url].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Client

Initialize a new MitakeSms::Client

Parameters:



32
33
34
35
# File 'lib/mitake_sms/client.rb', line 32

def initialize(config = nil)
  @config = config || MitakeSms.config
  @connection = build_connection
end

Instance Method Details

#batch_send(messages, options = {}) ⇒ MitakeSms::Response+

Send multiple SMS in a single request, splitting at the API's 500 message limit

Parameters:

  • messages (Array<Hash>)

    array of message hashes Each hash requires :to and :text, and may include :client_id, :dlvtime, :vldtime, :destname and :response_url

  • options (Hash) (defaults to: {})

    any other documented SmBulkSend field, such as :objectID or :smsPointFlag

Returns:

Raises:

  • (ArgumentError)

    if a field other than :text contains '$$' or a line break



70
71
72
73
74
# File 'lib/mitake_sms/client.rb', line 70

def batch_send(messages, options = {})
  messages.each_with_index { |msg, index| validate_row!(msg, index) }

  batch_send_with_limit(messages, BATCH_LIMIT, options)
end

#send_sms(to:, text:, destname: nil, response_url: nil, client_id: nil, **options) ⇒ MitakeSms::Response

Send a single SMS

Parameters:

  • to (String)

    recipient phone number

  • text (String)

    message content

  • destname (String) (defaults to: nil)

    recipient name or key value for system integration (optional)

  • response_url (String) (defaults to: nil)

    callback URL for delivery reports (optional)

  • client_id (String) (defaults to: nil)

    client reference ID (optional)

  • options (Hash)

    any other documented SmSend field, such as :dlvtime, :vldtime, :objectID or :smsPointFlag

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mitake_sms/client.rb', line 46

def send_sms(to:, text:, destname: nil, response_url: nil, client_id: nil, **options)
  form_params = {
    username: @config.username,
    password: @config.password,
    dstaddr: to,
    smbody: normalize_body(text)
  }
  form_params[:destname] = destname if destname
  form_params[:response] = response_url if response_url
  form_params[:clientid] = client_id if client_id

  perform_request('SmSend', params: { CharsetURL: CHARSET }) do |req|
    req.body = form_params.merge(options)
  end
end