lib/zenvia/sms.rb in zenvia-rb-0.0.11 vs lib/zenvia/sms.rb in zenvia-rb-0.1.0

- old
+ new

@@ -2,56 +2,128 @@ require 'json' require 'timeout' module Zenvia class SMS - attr_writer :from, :number, :message + + def self.lookup(id) + begin + res = self.get_status id + JSON.parse res.body + rescue => e + puts "Error!\n#{e}" + end + end # function to send the message - def self.send_message(number, message, from = nil) + def self.send_message(number, message, options = {}) begin - @from = from.nil? ? Zenvia.config.from : from + @responses = [] + @from = options[:from].nil? ? Zenvia.config.from : options[:from] @message = message - # create numbers array and push onto it number(s) from parameters + @options = options numbers = number.is_a?(Array) ? number : number.split + numbers.each do |nb| @number = nb - response = self.send_sms - response = JSON.parse(response.body) - puts "Response for #{nb}: #{response['sendSmsResponse']['detailDescription']}" + response = self.send_one_request + @responses.push JSON.parse(response.body) end + + treat_response rescue => e - puts 'Error!' - raise e + puts "Error!\n#{e}" end end + def self.send_multiple_messages(list = [], aggregate_id = nil) + begin + @list = list + @aggregate_id = aggregate_id + validate_list! + + if @errors[:errors].empty? + response = self.send_multiple_requests + JSON.parse response.body + else + @errors + end + rescue => e + puts "Error!\n#{e}" + end + end + private - def self.send_sms - # convert number to string (if isn't yet) and insert the country code (standard: BR, 55) - # if not found - @number = @number.to_s unless @number.is_a? String - patterns = ['(', ')', ' ', '-'] - patterns.each {|p| @number = @number.gsub(p, '')} - @number.insert(0, '55') unless /^55/.match(@number) - puts "THE NUMBER: #{@number}" - # retrieve auth value set in Config class - @auth = Zenvia.config.auth - endpoint = 'https://api-rest.zenvia360.com.br/services/send-sms' - HTTParty.post(endpoint, - body: { - sendSmsRequest: { - from: @from, - to: @number, - msg: @message, - callbackOption: 'NONE' + def self.send_one_request + @number = treat_number @number + endpoint = 'https://api-rest.zenvia360.com.br/services/send-sms' + HTTParty.post(endpoint, + body: { + sendSmsRequest: { + from: @from, + to: @number, + msg: @message, + id: @options[:id], + schedule: @options[:schedule], + aggregateId: @options[:aggregate_id], + callbackOption: 'NONE' + } + }.to_json, + headers: self.headers + ) + end + + def self.send_multiple_requests + endpoint = 'https://api-rest.zenvia360.com.br/services/send-sms-multiple' + HTTParty.post(endpoint, + body: { + sendSmsMultiRequest: { + aggregateId: @aggregate_id, + sendSmsRequestList: @list } - }.to_json, - headers: { + }.to_json, + headers: self.headers + ) + end + + def self.get_status(id) + endpoint = "https://api-rest.zenvia360.com.br/services/get-sms-status/#{id}" + HTTParty.get(endpoint, headers: { + 'Authorization' => "Basic #{Zenvia.config.auth}" + } + ) + end + + def self.treat_number(number) + number = number.to_s unless number.is_a? String + patterns = ['(', ')', ' ', '-'] + patterns.each {|p| number = number.gsub(p, '')} + number.insert(0, '55') unless /^55/.match(number) + number + end + + def self.validate_list! + @errors = {errors: []} + + @list.each do |data| + @list[@list.index(data)][:to] = treat_number data[:to] + + @errors[:errors].push to: 'required' unless data.include?(:to) + @errors[:errors].push msg: 'required' unless data.include?(:msg) + + break if @errors[:errors].any? + end + end + + def self.treat_response + @responses.count == 1 ? @responses.first : @responses + end + + def self.headers + { 'Content-Type' => 'application/json', - 'Authorization' => "Basic #{@auth}", + 'Authorization' => "Basic #{Zenvia.config.auth}", 'Accept' => 'application/json' } - ) - end + end end end \ No newline at end of file