Class: MoteSMS::SwisscomTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/mote_sms/transports/swisscom_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::SwisscomTransport.new 'https://api.swisscom.com/', 'ApIkEy'
transport.deliver message
# => ['000-791234', '001-7987324']

Constant Summary collapse

MAX_RECIPIENT =

Maximum recipients allowed by API

1
ServiceError =

Custom exception subclass.

Class.new(::Exception)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, api_key, from_number = nil, options = {}) ⇒ SwisscomTransport

Public: Create a new instance using specified endpoint, api_key and password.

endpoint - The swisscom base url of the API api_key - The String with the API key. from_number - The phone number to send from (mandatory @ swisscom) options - The Hash with additional URL params passed to mobile techics endpoint

Returns a new instance.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 56

def initialize(endpoint, api_key, from_number = nil, options = {})
  @endpoint = URI.parse(endpoint)
  @api_key = api_key
  @from_number = from_number
  @options = options

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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



45
46
47
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 45

def api_key
  @api_key
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



45
46
47
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 45

def endpoint
  @endpoint
end

#from_numberObject (readonly)

Returns the value of attribute from_number.



45
46
47
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 45

def from_number
  @from_number
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



45
46
47
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 45

def http_client
  @http_client
end

#optionsObject (readonly)

Returns the value of attribute options.



45
46
47
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 45

def options
  @options
end

Class Method Details

.loggerObject

Public: Logger used to log HTTP requests to mobile technics API endpoint.

Returns Logger instance.



31
32
33
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 31

def self.logger
  @@logger ||= ::Logger.new($stdout)
end

.logger=(logger) ⇒ Object

Public: Change the logger used to log all HTTP requests to the endpoint.

logger - The Logger instance, should at least respond to #debug, #error.

Returns nothing.



41
42
43
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 41

def self.logger=(logger)
  @@logger = logger
end

Instance Method Details

#deliver(message, _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:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 74

def deliver(message, _options = {})
  raise ServiceError, "too many recipients, max. is #{MAX_RECIPIENT} (current: #{message.to.length})" if message.to.length > MAX_RECIPIENT

  # Prepare request
  request = Net::HTTP::Post.new('/messaging/sms').tap do |request|
    request.body = post_params(message)
    request.content_type = 'application/json'
    request['Accept'] = 'application/json'
    request['client_id'] = api_key
    request['SCS-Version'] = 2
  end

  # Log as `curl` request
  self.class.logger.debug "curl -X#{request.method} '#{endpoint}' -d '#{request.body}'"

  # Perform request
  resp = http_client.request(request)

  # Handle errors
  raise ServiceError, "endpoint did respond with #{resp.code} and #{resp.body}" unless resp.code.to_i == 201
  self.class.logger.debug resp.body

  # Return numbers message send to
  message.to
end