Class: Gupshup::Enterprise

Inherits:
Object
  • Object
show all
Defined in:
lib/gupshup.rb

Instance Method Summary collapse

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
# File 'lib/gupshup.rb', line 24

def initialize(opts)
  @api_url = opts[:api_url] || 'http://enterprise.smsgupshup.com/GatewayAPI/rest'
  @api_params = {}
  @api_params[:userid] = opts[:userid]
  @api_params[:password] = opts[:password]
  @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



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gupshup.rb', line 88

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
  puts resp.body.content
end

#call_api(opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gupshup.rb', line 39

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, resp
  else
    return false, "HTTP Error : #{res}"
  end
end

#group_post(opts) ⇒ Object



100
101
102
103
104
105
# File 'lib/gupshup.rb', line 100

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_flash_message(opts) ⇒ Object



72
73
74
# File 'lib/gupshup.rb', line 72

def send_flash_message(opts)
  send_message(opts.merge({ :msg_type => 'FLASH'}))
end

#send_message(opts) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gupshup.rb', line 59

def send_message(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 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



76
77
78
# File 'lib/gupshup.rb', line 76

def send_text_message(opts)
  send_message(opts.merge({ :msg_type => 'TEXT'}))
end

#send_unicode_message(opts) ⇒ Object



84
85
86
# File 'lib/gupshup.rb', line 84

def send_unicode_message(opts)
  send_message(opts.merge({ :msg_type => 'UNICODE_TEXT'}))
end

#send_vcard(opts) ⇒ Object



80
81
82
# File 'lib/gupshup.rb', line 80

def send_vcard(opts)
  send_message(opts.merge({ :msg_type => 'VCARD'}))
end