Class: MitakeSms::Client
- Inherits:
-
Object
- Object
- MitakeSms::Client
- Defined in:
- lib/mitake_sms/client.rb
Defined Under Namespace
Classes: AuthenticationError, Error, InvalidRequestError, ServerError
Instance Method Summary collapse
-
#advanced_batch_send(messages, options = {}) ⇒ MitakeSms::Response+
Send multiple SMS in a single request using advanced format.
-
#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.
-
#batch_send(messages, options = {}) ⇒ MitakeSms::Response+
Send multiple SMS in a single request.
-
#batch_send_with_limit(messages, limit = 500, options = {}) ⇒ MitakeSms::Response+
Send multiple SMS in a single request with a limit per request.
-
#initialize(config = nil) ⇒ Client
constructor
Initialize a new MitakeSms::Client.
-
#send_sms(to:, text:, destname: nil, response_url: nil, client_id: nil, charset: 'UTF8', **options) ⇒ MitakeSms::Response
Send a single SMS.
Constructor Details
#initialize(config = nil) ⇒ Client
Initialize a new MitakeSms::Client
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
119 120 121 122 123 |
# File 'lib/mitake_sms/client.rb', line 119 def advanced_batch_send(, = {}) # 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(, 500, ) 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
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(, limit = 500, = {}) charset = [:charset] || 'UTF8' # If messages count is within the limit, use the regular batch send return send_advanced_batch(, charset, ) if .size <= limit # Otherwise, split into batches of the specified limit responses = [] .each_slice(limit) do |batch| responses << send_advanced_batch(batch, charset, ) end # Return array of responses responses end |
#batch_send(messages, options = {}) ⇒ MitakeSms::Response+
Send multiple SMS in a single request
75 76 77 78 79 |
# File 'lib/mitake_sms/client.rb', line 75 def batch_send(, = {}) # 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(, 500, ) end |
#batch_send_with_limit(messages, limit = 500, options = {}) ⇒ MitakeSms::Response+
Send multiple SMS in a single request with a limit per request
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(, limit = 500, = {}) charset = [:charset] || 'UTF8' # If messages count is within the limit, use the regular batch send return send_batch(, charset, ) if .size <= limit # Otherwise, split into batches of the specified limit responses = [] .each_slice(limit) do |batch| responses << send_batch(batch, charset, ) 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
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', **) require 'uri' # Create options hash with only non-nil values = {} [:destname] = destname if destname [:response_url] = response_url if response_url [: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().merge() # 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 |