Class: Alidayu::Sms

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(options)
  @template  = options[:sms_template]
  @signature = options[: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

#signatureObject (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

Yields:



13
14
15
16
# File 'lib/alidayu/sms.rb', line 13

def configuration
  @setting = OpenStruct.new
  yield @setting
end

.settingObject



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, setting_error_message(:rails)
    end
  else
    raise SettingError, setting_error_message
  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