class RubySms::Gateway::Sms77

Constants

DEFAULT_PIN_SIZE
SMS77_GATEWAY_URL
SUCCESS_CODE

Attributes

api_key[RW]
user[RW]

Public Class Methods

new(options) click to toggle source
# File lib/gateway/sms77.rb, line 14
def initialize(options)
  return if options.nil?
  options.symbolize_keys!
  return if options[:user].nil?
  return if options[:api_key].nil?

  self.user = options[:user]
  self.api_key = options[:api_key]
end

Public Instance Methods

send(options) click to toggle source
# File lib/gateway/sms77.rb, line 24
def send(options)
  return if options.nil?
  options.symbolize_keys!

  response = Sms77Response.new
  (response.add_error(:empty_options) and (return response)) if options.nil?
  code = post_request(options)
  return response if code == SUCCESS_CODE
  response.add_error(code)
  response
end
send_pin(options) click to toggle source

generate a simple random 4 digits pin

# File lib/gateway/sms77.rb, line 37
def send_pin(options)
  return if options.nil?
  options.symbolize_keys!

  response = Sms77Response.new
  (response.add_error(:empty_options) and (return response)) if options.nil?
  # we may use SecureRandom.hex(size) if you need a more secure pin
  response.pin = rand.to_s[2..(DEFAULT_PIN_SIZE + 1)]
  options[:text] = options[:text].gsub('%PIN%', response.pin)
  code = post_request(options)
  return response if code == SUCCESS_CODE
  response.add_error(code)
  response
end

Private Instance Methods

https(uri) click to toggle source
# File lib/gateway/sms77.rb, line 73
def https(uri)
  Net::HTTP.new(uri.host, uri.port).tap do |http|
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
end
post_request(options) click to toggle source
# File lib/gateway/sms77.rb, line 54
def post_request(options)
  uri = URI.parse(SMS77_GATEWAY_URL)
  request = Net::HTTP::Post.new(uri.to_s)
  request.set_form_data(
    text:  options[:text].force_encoding('utf-8'),
    to:    options[:to],
    delay: options[:delay] || 0,
    debug: 0,
    utf8:  1,
    u: user,
    p: api_key
  )
  response = https(uri).request(request)
  response.body
rescue StandardError => e
  RubySms.logger.error("Error while sending post request => #{e}")
  :connection_error
end