Class: MoteSMS::MobileTechnicsTransport
- Inherits:
-
Object
- Object
- MoteSMS::MobileTechnicsTransport
- Defined in:
- lib/mote_sms/transports/mobile_technics_transport.rb
Overview
MoteSMS::MobileTechnicsTransport provides the implementation to send messages using nth.ch bulk SMS HTTP/S API. Each customer has custom endpoint (with port) and username/password.
Examples:
transport = MoteSMS::MobileTechnicsTransport.new 'https://mygateway.nth.ch', 'username', 'password'
transport.deliver
# => ['000-791234', '001-7987324']
Constant Summary collapse
- MAX_RECIPIENT =
Maximum recipients allowed by API
100
- ServiceError =
Custom exception subclass.
Class.new(::Exception)
Class Attribute Summary collapse
-
.logger ⇒ Object
Public: Logger used to log HTTP requests to mobile technics API endpoint.
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Readable attributes.
-
#http_client ⇒ Object
readonly
Readable attributes.
-
#options ⇒ Object
readonly
Readable attributes.
-
#password ⇒ Object
readonly
Readable attributes.
-
#username ⇒ Object
readonly
Readable attributes.
Class Method Summary collapse
-
.defaults ⇒ Object
Public: Global default parameters for sending messages, Procs/lambdas are evaluated on #deliver.
Instance Method Summary collapse
-
#deliver(message, deliver_options = {}) ⇒ Object
Public: Delivers message using mobile technics HTTP/S API.
-
#initialize(endpoint, username, password, options = {}) ⇒ MobileTechnicsTransport
constructor
Public: Create a new instance using specified endpoint, username and password.
Constructor Details
#initialize(endpoint, username, password, options = {}) ⇒ MobileTechnicsTransport
Public: Create a new instance using specified endpoint, username and password.
username - The String with username. password - The String with password. options - The Hash with additional URL params passed to mobile techics endpoint
:endpoint - The String with the URL, defaults to https://mygateway.nth.ch
:ssl - SSL client options
Returns a new instance.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 72 def initialize(endpoint, username, password, = {}) @endpoint = URI.parse(endpoint) @username = username @password = password @options = self.class.defaults.merge() @http_client = Transports::HttpClient.new(endpoint, proxy_address: @options[:proxy_address], proxy_port: @options[:proxy_port], ssl: @options[:ssl]) end |
Class Attribute Details
.logger ⇒ Object
Public: Logger used to log HTTP requests to mobile technics API endpoint.
Returns Logger instance.
48 49 50 |
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 48 def self.logger @logger ||= ::Logger.new($stdout) end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Readable attributes
27 28 29 |
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27 def endpoint @endpoint end |
#http_client ⇒ Object (readonly)
Readable attributes
27 28 29 |
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27 def http_client @http_client end |
#options ⇒ Object (readonly)
Readable attributes
27 28 29 |
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27 def @options end |
#password ⇒ Object (readonly)
Readable attributes
27 28 29 |
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27 def password @password end |
#username ⇒ Object (readonly)
Readable attributes
27 28 29 |
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27 def username @username end |
Class Method Details
.defaults ⇒ Object
Public: Global default parameters for sending messages, Procs/lambdas are evaluated on #deliver. Ensure to use only symbols as keys. Contains ‘allow_adaption: true` as default.
Examples:
MoteSMS::MobileTechnicsTransports.defaults[:messageid] = ->(msg) { "#{msg.from}-#{SecureRandom.hex}" }
Returns Hash with options.
38 39 40 41 42 |
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 38 def self.defaults @options ||= { allow_adaption: true } end |
Instance Method Details
#deliver(message, deliver_options = {}) ⇒ Object
Public: Delivers message using mobile technics HTTP/S API.
message - The MoteSMS::Message instance to send. options - The Hash with service specific options.
Returns Array with sender ids.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 91 def deliver(, = {}) raise ArgumentError, "too many recipients, max. is #{MAX_RECIPIENT} (current: #{.to.length})" if .to.length > MAX_RECIPIENT request = Net::HTTP::Post.new(endpoint.request_uri).tap do |req| req.body = URI.encode_www_form post_params(, .merge()) req.content_type = 'application/x-www-form-urlencoded; charset=utf-8' end self.class.logger.debug "curl -X#{request.method} '#{endpoint}' -d '#{request.body}'" resp = http_client.request(request) raise ServiceError, "endpoint did respond with #{resp.code}" unless resp.code.to_i == 200 raise ServiceError, "unable to deliver message to all recipients (CAUSE: #{resp.body.strip})" unless resp.body.split("\n").all? { |l| l =~ /Result_code: 00/ } resp['X-Nth-SmsId'].split(',') .to end |