class Rahyab::SMS

Attributes

errors[R]

Return Errors

Public Class Methods

new(url, user, password, company) click to toggle source

Constructor of Rahyab SMS Gateway

# File lib/rahyab.rb, line 17
def initialize(url, user, password, company)
  @url = url
  @user = user
  @password = password
  @company = company
end

Public Instance Methods

get_balance() click to toggle source

Check the credit that how many sms can be send

# File lib/rahyab.rb, line 95
def get_balance
  builder = Builder::XmlMarkup.new(indent: 2)
  builder.instruct! :xml, version: "1.0"
  builder.getUserBalance(company: @company)
  result = send_xml(builder.target!)
  source = XML::Parser.string(result)
  content = source.parse
  return content.find_first('/userBalance').content.strip.to_f
end
get_delivery(batchID) click to toggle source

Check delivery status of several sms

# File lib/rahyab.rb, line 82
def get_delivery(batchID)
  builder = Builder::XmlMarkup.new(indent: 2)
  builder.instruct! :xml, version: "1.0"
  builder.declare! :DOCTYPE, :smsStatusPoll, :PUBLIC, "-//PERVASIVE//DTD CPAS 1.0//EN", "http://www.ubicomp.ir/dtd/Cpas.dtd"
  builder.smsStatusPoll(company: @company) do |b|
    b.batch(batchID: batchID)
  end
  out_xml = builder.target!
  result = send_xml(out_xml)
  Hash.from_libxml(result)
end
send_batch(sender, numbers, text) click to toggle source
# File lib/rahyab.rb, line 78
def send_batch(sender, numbers, text)
end
send_sms(sender, numbers, text, **params) click to toggle source

Will send one or more sms to specified numbers

# File lib/rahyab.rb, line 25
def send_sms(sender, numbers, text, **params)
  # Create the send XMLmarkup
  if estimate_cost(numbers, text) < get_balance
    identity         = "#{Time.now.to_i}#{rand(1000000000..9999999999)}"
    batchID          = @company + "+" + identity
    is_persian_text  = is_persian(text)
    msgClass         = params["flash"] ? "0" : "1"
    dcs              = is_persian_text ? "8" : "0"
    binary           = is_persian_text ? "true" : "false"
    text             = text.to_h if is_persian_text

    builder          = Builder::XmlMarkup.new()
    builder.instruct! :xml, version: "1.0", encoding: "UTF-8"
    builder.declare! :DOCTYPE, :smsBatch, :PUBLIC, "-//PERVASIVE//DTD CPAS 1.0//EN", "http://www.ubicomp.ir/dtd/Cpas.dtd"
    builder.smsBatch(company: @company, batchID: batchID) do |b|
      b.sms(msgClass: msgClass, binary: binary, dcs: dcs) do |t|
        numbers.each do |number|
          t.destAddr() do |f|
            f.declare! "[CDATA[%s]]" % number
          end
        end
        t.origAddr() do |f|
          f.declare! "[CDATA[%s]]" % sender
        end
        t.message() do |f|
          f.declare! "[CDATA[#{text}]]"
        end
      end
    end
    out_xml = builder.target!

    result = send_xml(out_xml)
    source = XML::Parser.string(result)
    content = source.parse

    if content.find_first('ok')
      if  content.find_first('ok').content.include? 'CHECK_OK'
        batchID
      else
        @errors = "Something going wrong"
        nil
      end
    else
      @errors = content.find_first('message').content.strip
      nil
    end
  else
    @errors = 'Not enough balance'
    nil
  end
end

Private Instance Methods

estimate_cost(numbers, text) click to toggle source

Cost estimates

# File lib/rahyab.rb, line 124
def estimate_cost(numbers, text)
  cost = 0
  if is_persian(text)
    sms_length = (text.length / 67.0).ceil
  else
    sms_length = (text.length / 157.0).ceil
  end
  numbers.each do |number|
    if is_persian(text)
      cost = cost + sms_length * 1.5
    else
      cost = cost + sms_length
    end
  end
  cost
end
is_persian(str) click to toggle source

Check does the input contents Farsi Character

# File lib/rahyab.rb, line 108
def is_persian(str)
  str =~ /\p{Arabic}/
end
send_xml(out_xml) click to toggle source

Send XMLmarkup to Web Service

# File lib/rahyab.rb, line 113
def send_xml(out_xml)
  uri = URI.parse(@url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.basic_auth @user, @password
  request.body = out_xml
  response = http.request(request)
  return response.body
end