Class: MoteSMS::MobileTechnicsTransport

Inherits:
Object
  • Object
show all
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 message
# => ['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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @endpoint = URI.parse(endpoint)
  @username = username
  @password = password

  @options = self.class.defaults.merge(options)

  @http_client = Transports::HttpClient.new(endpoint,
                                            proxy_address: @options[:proxy_address],
                                            proxy_port: @options[:proxy_port],
                                            ssl: @options[:ssl])
end

Class Attribute Details

.loggerObject

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

#endpointObject (readonly)

Readable attributes



27
28
29
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27

def endpoint
  @endpoint
end

#http_clientObject (readonly)

Readable attributes



27
28
29
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27

def http_client
  @http_client
end

#optionsObject (readonly)

Readable attributes



27
28
29
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27

def options
  @options
end

#passwordObject (readonly)

Readable attributes



27
28
29
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27

def password
  @password
end

#usernameObject (readonly)

Readable attributes



27
28
29
# File 'lib/mote_sms/transports/mobile_technics_transport.rb', line 27

def username
  @username
end

Class Method Details

.defaultsObject

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.

Raises:

  • (ArgumentError)


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(message, deliver_options = {})
  raise ArgumentError, "too many recipients, max. is #{MAX_RECIPIENT} (current: #{message.to.length})" if message.to.length > MAX_RECIPIENT

  request = Net::HTTP::Post.new(endpoint.request_uri).tap do |req|
    req.body = URI.encode_www_form post_params(message, options.merge(deliver_options))
    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(',')

  message.to
end