class SMSCentre::Provider
Public Class Methods
new(login, password)
click to toggle source
# File lib/sms_centre/provider.rb, line 3 def initialize(login, password) @login = login @password = password end
Public Instance Methods
request(opts = {})
click to toggle source
Perform API
call
@option opts [Fixnum] :timeout Timeout for API
call @option opts [String] :path (‘sys/send.php’) Remote script path @option opts [String] :host SMS Centre gateway API
host @option opts [Boolean] :ssl Use HTTPS for API
call
# File lib/sms_centre/provider.rb, line 14 def request(opts = {}) opts = default_opts.merge(opts) params = default_params.merge(opts.fetch(:params, {})) data = URI.encode_www_form(params) resp = perform_request(opts, data) JSON.parse(resp.body || '') rescue Errno::ECONNREFUSED => e raise ConnectionError.new(e.message) rescue JSON::ParserError => e raise ServerError.new("response json parsing failed: #{e.message}") end
Private Instance Methods
default_opts()
click to toggle source
# File lib/sms_centre/provider.rb, line 44 def default_opts { timeout: SMSCentre.configuration.timeout, ssl: SMSCentre.configuration.ssl, host: SMSCentre.configuration.host, port: SMSCentre.configuration.port } end
default_params()
click to toggle source
# File lib/sms_centre/provider.rb, line 29 def default_params params = { login: @login, # Login psw: @password, # Password fmt: 3, # Use JSON format for response charset: 'utf-8' # Message charset } unless SMSCentre.configuration.sender.nil? params[:sender] = SMSCentre.sender end params end
perform_request(opts, data)
click to toggle source
# File lib/sms_centre/provider.rb, line 53 def perform_request(opts, data) Timeout.timeout(opts[:timeout]) do uri = opts[:ssl] ? "https://" : "http://" uri += opts[:host] uri += ":#{opts[:port]}" uri += opts[:path] SMSCentre.logger.debug "call: POST #{uri}; Data: '#{data}'" resp = Net::HTTP.start(opts[:host], opts[:port], use_ssl: opts[:ssl]) do |http| http.post(opts[:path], data) end SMSCentre.logger.debug "resp: (#{resp.code}) #{resp.body.inspect}" resp end end