Class: SmsCtrl::Case
- Inherits:
-
Object
- Object
- SmsCtrl::Case
- Defined in:
- lib/sms_ctrl/case.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#expires_in ⇒ Object
Returns the value of attribute expires_in.
-
#mobile_regexp ⇒ Object
Returns the value of attribute mobile_regexp.
-
#retry_limit ⇒ Object
Returns the value of attribute retry_limit.
-
#sender ⇒ Object
Returns the value of attribute sender.
Instance Method Summary collapse
-
#check_code(mobile, code) ⇒ Object
check match.
- #default_errors ⇒ Object
- #default_options ⇒ Object
-
#initialize(name, options = {}) ⇒ Case
constructor
A new instance of Case.
- #send_sms(mobile, code, params = {}) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Case
Returns a new instance of Case.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sms_ctrl/case.rb', line 23 def initialize name, = {} @name = name = .merge() self.retry_limit = [:retry_limit] self.expires_in = [:expires_in] self.mobile_regexp = [:mobile_regexp] self.sender = [:sender] if [:errors] self.errors = default_errors.merge([:errors]) else self.errors = default_errors end end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
3 4 5 |
# File 'lib/sms_ctrl/case.rb', line 3 def errors @errors end |
#expires_in ⇒ Object
Returns the value of attribute expires_in.
3 4 5 |
# File 'lib/sms_ctrl/case.rb', line 3 def expires_in @expires_in end |
#mobile_regexp ⇒ Object
Returns the value of attribute mobile_regexp.
3 4 5 |
# File 'lib/sms_ctrl/case.rb', line 3 def mobile_regexp @mobile_regexp end |
#retry_limit ⇒ Object
Returns the value of attribute retry_limit.
3 4 5 |
# File 'lib/sms_ctrl/case.rb', line 3 def retry_limit @retry_limit end |
#sender ⇒ Object
Returns the value of attribute sender.
3 4 5 |
# File 'lib/sms_ctrl/case.rb', line 3 def sender @sender end |
Instance Method Details
#check_code(mobile, code) ⇒ Object
check match
79 80 81 82 83 |
# File 'lib/sms_ctrl/case.rb', line 79 def check_code mobile, code cache_code = SmsCtrl.cache.read(code_cache_key(mobile.to_s)) return false if cache_code.nil? cache_code.to_s == code.to_s end |
#default_errors ⇒ Object
16 17 18 19 20 21 |
# File 'lib/sms_ctrl/case.rb', line 16 def default_errors { illegal_mobile: '请输入正确的手机号码', retry_limit: '操作太频繁,请稍后再试' } end |
#default_options ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/sms_ctrl/case.rb', line 7 def { retry_limit: 55, expires_in: 30 * 60, mobile_regexp: /^1[3|4|5|7|8]\d{9}$/, sender: -> (mobile, code, params) { warn "No message sender set for case #{@name}" }, } end |
#send_sms(mobile, code, params = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sms_ctrl/case.rb', line 39 def send_sms mobile, code, params = {} mobile = mobile.to_s unless mobile =~ mobile_regexp return { success: false, error: errors[:illegal_mobile], error_type: :illegal_mobile } end if retry_limit > 0 && SmsCtrl.cache.read(retry_limit_key(mobile)) return { success: false, error: errors[:retry_limit], error_type: :retry_limit } end if SmsCtrl.debug code = '123456' data = 'debug send ok' else data = @sender.call(mobile, code, params) end result = { success: true, data: data } if retry_limit > 0 SmsCtrl.cache.write(retry_limit_key(mobile), 'true', expires_in: retry_limit) end SmsCtrl.cache.write(code_cache_key(mobile), code, expires_in: expires_in) result end |