Class: Rahyab::SMS

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user, password, company) ⇒ SMS

Constructor of Rahyab SMS Gateway



17
18
19
20
21
22
# File 'lib/rahyab.rb', line 17

def initialize(url, user, password, company)
  @url = url
  @user = user
  @password = password
  @company = company
end

Instance Attribute Details

#errorsObject (readonly)

Return Errors



14
15
16
# File 'lib/rahyab.rb', line 14

def errors
  @errors
end

Instance Method Details

#get_balanceObject

Check the credit that how many sms can be send



95
96
97
98
99
100
101
102
103
# 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) ⇒ Object

Check delivery status of several sms



82
83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Object



78
79
# File 'lib/rahyab.rb', line 78

def send_batch(sender, numbers, text)
end

#send_sms(sender, numbers, text, **params) ⇒ Object

Will send one or more sms to specified numbers



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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