Class: Sms4jawaly::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/sms4jawaly_sms.rb,
lib/sms4jawaly/gateway.rb

Constant Summary collapse

API_BASE_URL =
'https://api-sms.4jawaly.com/api/v1'

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret) ⇒ Gateway

Returns a new instance of Gateway.



10
11
12
13
14
# File 'lib/sms4jawaly_sms.rb', line 10

def initialize(api_key, api_secret)
  @api_key = api_key
  @api_secret = api_secret
  @headers = create_headers
end

Instance Method Details

#get_balanceObject

دالة الاستعلام عن الرصيد



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sms4jawaly_sms.rb', line 17

def get_balance
  url = URI.parse("#{API_BASE_URL}/account/area/me/packages?is_active=1")
  response = send_request(url, :get)
  
  if response[:success]
    response[:data]
  else
    {
      success: false,
      error: response[:error]
    }
  end
end

#get_sendersObject

دالة جلب أسماء المرسلين



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

def get_senders
  url = URI.parse("#{API_BASE_URL}/account/area/senders")
  all_senders = []
  default_senders = []
  page = 1
  
  loop do
    response = send_request(url, :get, { page: page })
    break unless response[:success]
    
    data = response[:data]
    items = data['items']
    
    items['data'].each do |item|
      sender_name = item['sender_name']
      all_senders << sender_name
      default_senders << sender_name if item['is_default'] == 1
    end
    
    break if page >= items['last_page']
    page += 1
  end

  {
    success: true,
    all_senders: all_senders,
    default_senders: default_senders,
    message: "تم"
  }
end

#send_sms(message, numbers, sender) ⇒ Object

دالة إرسال الرسائل



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sms4jawaly_sms.rb', line 64

def send_sms(message, numbers, sender)
  result = {
    success: true,
    total_success: 0,
    total_failed: 0,
    job_ids: [],
    errors: {}
  }

  messages = {
    messages: [
      {
        text: message,
        numbers: numbers,
        sender: sender
      }
    ]
  }

  url = URI("#{API_BASE_URL}/account/area/sms/send")
  response = send_request(url, :post, messages)
  
  if response[:success]
    result[:total_success] = numbers.length
    result[:job_ids] << response[:data]["job_id"] if response[:data]["job_id"]
  else
    result[:total_failed] = numbers.length
    result[:errors][response[:error]] = numbers
    result[:success] = false
  end

  result
end