Class: Gupshup::Enterprise
- Inherits:
-
Object
- Object
- Gupshup::Enterprise
- Defined in:
- lib/parity-gupshup.rb
Instance Method Summary collapse
- #bulk_file_upload(file_path, file_type = 'csv', mime_type = 'text/csv', opts = {}) ⇒ Object
- #call_api(opts = {}) ⇒ Object
- #group_post(opts) ⇒ Object
-
#initialize(opts) ⇒ Enterprise
constructor
A new instance of Enterprise.
- #send_bulk_message(opts) ⇒ Object
- #send_flash_message(opts) ⇒ Object
- #send_message(opts) ⇒ Object
- #send_text_message(opts) ⇒ Object
- #send_unicode_message(opts) ⇒ Object
- #send_vcard(opts) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Enterprise
Returns a new instance of Enterprise.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/parity-gupshup.rb', line 24 def initialize(opts) @api_url = opts[:api_url] || 'https://enterprise.smsgupshup.com/GatewayAPI/rest' @api_params = {} @api_params[:userid] = opts[:userid] @api_params[:password] = opts[:password] @api_params[:mask] = opts[:mask] if opts[:mask] @api_params[:override_dnd] = opts[:override_dnd] || false @api_params[:v] = opts[:v] || '1.1' @api_params[:auth_scheme] = opts[:auth_scheme] || 'PLAIN' unless opts[:token].blank? @api_params[:auth_scheme] = 'TOKEN' @api_params[:token] = opts[:token] @api_params.delete(:password) end raise "Invalid credentials" if opts[:userid].blank? || (opts[:password].blank? && opts[:token].blank?) end |
Instance Method Details
#bulk_file_upload(file_path, file_type = 'csv', mime_type = 'text/csv', opts = {}) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/parity-gupshup.rb', line 110 def bulk_file_upload(file_path,file_type = 'csv',mime_type = 'text/csv', opts = {}) msg_params = {} msg_params[:method] = 'xlsUpload' msg_params[:filetype] = file_type.to_s file = File.new(file_path,"r") def file.mime_type; "text/csv"; end msg_params[:xlsFile] = file resp = HTTPClient.post(@api_url,msg_params.merge(@api_params).merge(opts)) file.close end |
#call_api(opts = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/parity-gupshup.rb', line 41 def call_api(opts = {}) res = Net::HTTP.post_form( URI.parse(@api_url), @api_params.merge(opts) ) resp = res.body puts "GupShup Response: #{resp}" case res when Net::HTTPSuccess if resp.nil? || resp.include?("success") == false puts "API call '#{opts[:method]}' failed: #{resp}" return false, resp end return true, nil else return false, "HTTP Error : #{res}" end end |
#group_post(opts) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/parity-gupshup.rb', line 121 def group_post(opts) return false,"Invalid group name" if opts[:group_name].blank? return false,"Invalid message" if opts[:msg].blank? return false,"Invalid message type" if opts[:msg_type].blank? call_api opts.merge({:method => 'post_group'}) end |
#send_bulk_message(opts) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/parity-gupshup.rb', line 75 def (opts) msg = opts[:msg] numbers = opts[:send_to] msg_type = opts[:msg_type] || 'TEXT' @api_params[:v] = '1.0' # Only available in 1.0 API of GupShup. return false, "Sending to 100 numbers max allowed at a time" if numbers.split(",").size > 100 numbers.split(",").each do |number| return false, 'Phone Number is too short' if number.to_s.length < 12 return false, 'Phone Number is too long' if number.to_s.length > 12 #return false, 'Phone Number should start with "91"' if number.to_s.start_with? "91" return false, 'Phone Number should be numerical value' unless number.to_i.to_s == number.to_s end return false, 'Message cannot be empty' if msg.to_s.length == 0 return false, 'Message should be less than 725 characters long' if msg.to_s.length > 724 call_api opts.merge({ :method => 'sendMessage' }) end |
#send_flash_message(opts) ⇒ Object
94 95 96 |
# File 'lib/parity-gupshup.rb', line 94 def (opts) (opts.merge({ :msg_type => 'FLASH'})) end |
#send_message(opts) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/parity-gupshup.rb', line 61 def (opts) msg = opts[:msg] number = opts[:send_to] msg_type = opts[:msg_type] || 'TEXT' return false, 'Phone Number is too short' if number.to_s.length < 12 return false, 'Phone Number is too long' if number.to_s.length > 12 #return false, 'Phone Number should start with "91"' if number.to_s.start_with? "91" return false, 'Phone Number should be numerical value' unless number.to_i.to_s == number.to_s return false, 'Message cannot be empty' if msg.to_s.length == 0 return false, 'Message should be less than 725 characters long' if msg.to_s.length > 724 call_api opts.merge({ :method => 'sendMessage' }) end |
#send_text_message(opts) ⇒ Object
98 99 100 |
# File 'lib/parity-gupshup.rb', line 98 def (opts) (opts.merge({ :msg_type => 'TEXT'})) end |
#send_unicode_message(opts) ⇒ Object
106 107 108 |
# File 'lib/parity-gupshup.rb', line 106 def (opts) (opts.merge({ :msg_type => 'UNICODE_TEXT'})) end |
#send_vcard(opts) ⇒ Object
102 103 104 |
# File 'lib/parity-gupshup.rb', line 102 def send_vcard(opts) (opts.merge({ :msg_type => 'VCARD'})) end |