Class: MoteSMS::SwisscomTransport
- Inherits:
-
Object
- Object
- MoteSMS::SwisscomTransport
- 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
# => ['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
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#from_number ⇒ Object
readonly
Returns the value of attribute from_number.
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.logger ⇒ Object
Public: Logger used to log HTTP requests to mobile technics API endpoint.
-
.logger=(logger) ⇒ Object
Public: Change the logger used to log all HTTP requests to the endpoint.
Instance Method Summary collapse
-
#deliver(message, _options = {}) ⇒ Object
Public: Delivers message using mobile technics HTTP/S API.
-
#initialize(endpoint, api_key, from_number = nil, options = {}) ⇒ SwisscomTransport
constructor
Public: Create a new instance using specified endpoint, api_key and password.
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, = {}) @endpoint = URI.parse(endpoint) @api_key = api_key @from_number = from_number = @http_client = Transports::HttpClient.new(endpoint, proxy_address: [:proxy_address], proxy_port: [:proxy_port], ssl: [:ssl]) end |
Instance Attribute Details
#api_key ⇒ Object (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 |
#endpoint ⇒ Object (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_number ⇒ Object (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_client ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
45 46 47 |
# File 'lib/mote_sms/transports/swisscom_transport.rb', line 45 def end |
Class Method Details
.logger ⇒ Object
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.
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(, = {}) raise ServiceError, "too many recipients, max. is #{MAX_RECIPIENT} (current: #{message.to.length})" if .to.length > MAX_RECIPIENT # Prepare request request = Net::HTTP::Post.new('/messaging/sms').tap do |request| request.body = post_params() 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 .to end |