Class: Alidayu::Sms
- Inherits:
-
Object
- Object
- Alidayu::Sms
- Defined in:
- lib/alidayu/sms.rb,
lib/alidayu/sms/version.rb
Constant Summary collapse
- SMS_ENDPOINT =
'http://dysmsapi.aliyuncs.com'
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options) ⇒ Sms
constructor
A new instance of Sms.
- #send_to(*args) ⇒ Object (also: #send_sms)
- #send_to!(reciever, params = {}) ⇒ Object (also: #send_sms!)
Constructor Details
#initialize(options) ⇒ Sms
Returns a new instance of Sms.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/alidayu/sms.rb', line 43 def initialize() @template = [:sms_template] @signature = [:sms_sign] endpoint = URI setting.endpoint || SMS_ENDPOINT @conn = Net::HTTP.new(endpoint.host, endpoint.port).tap do |http| http.open_timeout = 3 http.read_timeout = 3 end end |
Instance Attribute Details
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
10 11 12 |
# File 'lib/alidayu/sms.rb', line 10 def signature @signature end |
Class Method Details
.configuration {|@setting| ... } ⇒ Object
13 14 15 16 |
# File 'lib/alidayu/sms.rb', line 13 def configuration @setting = OpenStruct.new yield @setting end |
.setting ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/alidayu/sms.rb', line 18 def setting return @setting if @setting && !@setting.to_h.nil? if defined?(Rails) config = YAML.load_file("#{Rails.root}/config/alidayu.yml")[Rails.env] if !config.nil? then @setting = OpenStruct.new config else raise SettingError, (:rails) end else raise SettingError, end end |
Instance Method Details
#send_to(*args) ⇒ Object Also known as: send_sms
66 67 68 |
# File 'lib/alidayu/sms.rb', line 66 def send_to(*args) send!(*args) rescue false end |
#send_to!(reciever, params = {}) ⇒ Object Also known as: send_sms!
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/alidayu/sms.rb', line 54 def send_to!(reciever, params = {}) reciever = Array(reciever).join(',') sms_params = default_params(reciever, params) sig_params = params_with_sign(setting.access_key_secret, sms_params) response = @conn.request_post '/', params_string(sig_params) result = JSON.parse response.body if result['Code'] != 'OK' raise RequestError, "#{result['Code']} #{result['Message']}" end true end |