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.
-
#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.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/sms_ctrl/case.rb', line 6 def initialize name, = {} @name = name = SmsCtrl..merge() self.retry_limit = [:retry_limit] self.expires_in = [:expires_in] self.mobile_regexp = [:mobile_regexp] self.sender = [:sender] if [:errors] self.errors = SmsCtrl.default_errors.merge([:errors]) else self.errors = SmsCtrl.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
62 63 64 65 66 |
# File 'lib/sms_ctrl/case.rb', line 62 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 |
#send_sms(mobile, code, params = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sms_ctrl/case.rb', line 22 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 |