Class: Baidusms::Client
- Inherits:
-
Object
- Object
- Baidusms::Client
- Defined in:
- lib/baidusms/client.rb
Instance Method Summary collapse
- #call(service, method, body = nil, base_uri = 'https://api.baidu.com/json/sms/v3') ⇒ Object
- #download(file_path, local_file = nil) ⇒ Object
-
#initialize(attrs = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(attrs = {}) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 |
# File 'lib/baidusms/client.rb', line 7 def initialize(attrs={}) @username = attrs[:username] @password = attrs[:password] @token = attrs[:token] @debug = attrs[:debug] end |
Instance Method Details
#call(service, method, body = nil, base_uri = 'https://api.baidu.com/json/sms/v3') ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/baidusms/client.rb', line 14 def call(service,method,body=nil,base_uri='https://api.baidu.com/json/sms/v3') = { :header => { :username => @username, :token => @token, :password => @password } } [:body] = body unless body.nil? pp if @debug response = {} loop do response = HTTParty.post("#{base_uri}/#{service}/#{method}",:body => JSON(), :headers => {'content-type' => 'application/json;charset=utf-8'}) pp response if @debug break if response['header']['desc'] == 'success' break unless [8904].include? response['header']['failures'][0]['code'] sleep(10) end response end |
#download(file_path, local_file = nil) ⇒ Object
39 40 41 42 43 |
# File 'lib/baidusms/client.rb', line 39 def download(file_path,local_file=nil) file = HTTParty.get(file_path).body.encode('UTF-8','GBK') open(local_file,'w').puts file unless local_file.nil? file end |