Class: MitakeSms::Client

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

Defined Under Namespace

Classes: AuthenticationError, Error, InvalidRequestError, ServerError

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Client

Initialize a new MitakeSms::Client

Parameters:



17
18
19
20
# File 'lib/mitake_sms/client.rb', line 17

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

Instance Method Details

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

Send multiple SMS in a single request using advanced format

Parameters:

  • messages (Array<Hash>)

    array of message hashes with advanced options Each hash can contain the following keys:

    • :client_id [String] client reference ID (required)

    • :to [String] recipient phone number (required)

    • :dlvtime [String] delivery time in format YYYYMMDDHHMMSS (optional)

    • :vldtime [String] valid until time in format YYYYMMDDHHMMSS (optional)

    • :dest_name [String] recipient name (optional)

    • :response [String] callback URL for delivery reports (optional)

    • :text [String] message content (required)

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

    additional options

Options Hash (options):

  • :charset (String)

    character encoding, defaults to ‘UTF8’

  • :skip_encoding (Boolean)

    skip URL encoding (for tests)

Returns:



119
120
121
122
123
# File 'lib/mitake_sms/client.rb', line 119

def advanced_batch_send(messages, options = {})
  # Mitake SMS API has a limit of 500 messages per request
  # Automatically split larger batches into multiple requests of 500 messages each
  advanced_batch_send_with_limit(messages, 500, options)
end

#advanced_batch_send_with_limit(messages, limit = 500, options = {}) ⇒ MitakeSms::Response+

Send multiple SMS in a single request with a limit per request using advanced format

Parameters:

  • messages (Array<Hash>)

    array of message hashes with advanced options

  • limit (Integer) (defaults to: 500)

    maximum number of messages per request (default: 500)

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

    additional options

Options Hash (options):

  • :charset (String)

    character encoding, defaults to ‘UTF8’

  • :skip_encoding (Boolean)

    skip URL encoding (for tests)

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mitake_sms/client.rb', line 132

def advanced_batch_send_with_limit(messages, limit = 500, options = {})
  charset = options[:charset] || 'UTF8'

  # If messages count is within the limit, use the regular batch send
  return send_advanced_batch(messages, charset, options) if messages.size <= limit

  # Otherwise, split into batches of the specified limit
  responses = []
  messages.each_slice(limit) do |batch|
    responses << send_advanced_batch(batch, charset, options)
  end

  # Return array of responses
  responses
end

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

Send multiple SMS in a single request

Parameters:

  • messages (Array<Hash>)

    array of message hashes Each hash should contain :to and :text keys, and can include :from, :response_url, :client_id

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

    additional options

Options Hash (options):

  • :charset (String)

    character encoding, defaults to ‘UTF8’

  • :skip_encoding (Boolean)

    skip URL encoding (for tests)

Returns:



75
76
77
78
79
# File 'lib/mitake_sms/client.rb', line 75

def batch_send(messages, options = {})
  # Mitake SMS API has a limit of 500 messages per request
  # Automatically split larger batches into multiple requests of 500 messages each
  batch_send_with_limit(messages, 500, options)
end

#batch_send_with_limit(messages, limit = 500, options = {}) ⇒ MitakeSms::Response+

Send multiple SMS in a single request with a limit per request

Parameters:

  • messages (Array<Hash>)

    array of message hashes Each hash should contain :to and :text keys, and can include :from, :response_url, :client_id

  • limit (Integer) (defaults to: 500)

    maximum number of messages per request (default: 500)

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

    additional options

Options Hash (options):

  • :charset (String)

    character encoding, defaults to ‘UTF8’

  • :skip_encoding (Boolean)

    skip URL encoding (for tests)

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mitake_sms/client.rb', line 89

def batch_send_with_limit(messages, limit = 500, options = {})
  charset = options[:charset] || 'UTF8'

  # If messages count is within the limit, use the regular batch send
  return send_batch(messages, charset, options) if messages.size <= limit

  # Otherwise, split into batches of the specified limit
  responses = []
  messages.each_slice(limit) do |batch|
    responses << send_batch(batch, charset, options)
  end

  # Return array of responses
  responses
end

#send_sms(to:, text:, destname: nil, response_url: nil, client_id: nil, charset: 'UTF8', **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)

  • charset (String) (defaults to: 'UTF8')

    character encoding, defaults to ‘UTF8’ (optional)

  • options (Hash)

    additional options (optional)

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mitake_sms/client.rb', line 31

def send_sms(to:, text:, destname: nil, response_url: nil, client_id: nil, charset: 'UTF8', **options)
  require 'uri'

  # Create options hash with only non-nil values
  param_options = {}
  param_options[:destname] = destname if destname
  param_options[:response_url] = response_url if response_url
  param_options[:client_id] = client_id if client_id

  # Replace any newline characters with ASCII code 6 (ACK)
  # This is required by the Mitake API to represent line breaks
  processed_text = text.to_s.gsub("\n", 6.chr)

  # Prepare query parameters - only CharsetURL is sent as query parameter
  query_params = {
    CharsetURL: charset
  }

  # Prepare form parameters - all other parameters are sent in the POST body
  form_params = {
    username: @config.username,
    password: @config.password,
    dstaddr: to,
    smbody: processed_text
  }.merge(param_options).merge(options)

  # Construct the endpoint URL
  endpoint = "SmSend"

  response = @connection.post(endpoint) do |req|
    req.params = query_params
    req.body = form_params
  end

  handle_response(response)
end