module AliDayuSms

Constants

VERSION

Attributes

configuration[W]

Public Class Methods

configuration() click to toggle source
# File lib/AliDayuSms.rb, line 28
def self.configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/AliDayuSms.rb, line 32
def self.configure
  yield configuration
end
validate_code(phone, code) click to toggle source
# File lib/AliDayuSms/validation.rb, line 2
def self.validate_code(phone, code)
  get_code(phone) == code
end

Public Instance Methods

delete_code(phone) click to toggle source
# File lib/AliDayuSms/redis_client.rb, line 18
def delete_code(phone)
  redis_client.del("AliDayuSms:#{self.configuration.app_key}:#{phone}:code")
end
get_code(phone) click to toggle source
# File lib/AliDayuSms/redis_client.rb, line 14
def get_code(phone)
  redis_client.get("AliDayuSms:#{self.configuration.app_key}:#{phone}:code")
end
redis_client() click to toggle source
# File lib/AliDayuSms/redis_client.rb, line 4
def redis_client
  @redis_client ||= Redis.new(self.configuration.redis_config || {})
end
send(phone, sms_template_code, sms_params, extend = nil) click to toggle source
# File lib/AliDayuSms/send_sms.rb, line 14
def send(phone, sms_template_code, sms_params, extend = nil)
  sign_params = {
      method: configuration.method_str,
      app_key: configuration.app_key,
      timestamp: Time.now.getlocal('+08:00').strftime('%F %T'),
      v: '2.0',
      sign_method: 'md5',
      sms_type: 'normal',
      sms_free_sign_name: configuration.sign_name,
      rec_num: phone,
      format: 'json',
      simplify: configuration.simplify,
      sms_template_code: sms_template_code,
      sms_param: sms_params.to_json,
      extend: extend,
      target_app_key: configuration.target_app_key,
      session: configuration.session,
      partner_id: configuration.partner_id
  }.reject { |_k, v| v.nil? }

  sp = sign_params.merge(sign: sign(sign_params))

  Net::HTTP.post_form(URI.parse(configuration.url), sp)
end
send_code(phone, sms_template_code, sms_params = {}, length = 6, expire_in = 600) click to toggle source
# File lib/AliDayuSms/send_sms.rb, line 9
def send_code(phone, sms_template_code, sms_params = {}, length = 6, expire_in = 600)
  code = set_code(phone, expire_in, nil, length)
  send(phone, sms_template_code, sms_params.merge(code: code))
end
set_code(phone, expire_time, value = nil, length = 6) click to toggle source
# File lib/AliDayuSms/redis_client.rb, line 8
def set_code(phone, expire_time, value = nil, length = 6)
  value ||= random_code(length)
  redis_client.set("AliDayuSms:#{self.configuration.app_key}:#{phone}:code", value, ex: expire_time)
  value
end

Private Instance Methods

random_code(length) click to toggle source
# File lib/AliDayuSms/redis_client.rb, line 24
def random_code(length)
  SecureRandom.random_number(('9' * length).to_i).to_s.rjust(length, '0')
end
sign(params) click to toggle source
# File lib/AliDayuSms/send_sms.rb, line 41
def sign(params)
  Digest::MD5.hexdigest(wrap_with_secret(sorted_option_string(params))).upcase
end
sorted_option_string(options) click to toggle source
# File lib/AliDayuSms/send_sms.rb, line 50
def sorted_option_string(options)
  options.map { |k, v| "#{k}#{v}" }.sort.join
end
wrap_with_secret(s) click to toggle source
# File lib/AliDayuSms/send_sms.rb, line 45
def wrap_with_secret(s)
  secret = self.configuration.app_secret
  "#{secret}#{s}#{secret}"
end