Class: IqSMS::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login:, password:, **options) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
# File 'lib/iqsms/client.rb', line 20

def initialize(login:, password:, **options)
  with_value_must_be_present!() { @login =  }
  with_value_must_be_present!(password) { @password = password }

  @options = options.reverse_merge(default_options)

  check_status_queue_name!
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



17
18
19
# File 'lib/iqsms/client.rb', line 17

def connection
  @connection
end

#status_queue_nameObject



90
91
92
# File 'lib/iqsms/client.rb', line 90

def status_queue_name
  @status_queue_name ||= @options[:status_queue_name]
end

Class Method Details

.connection_pool_for(url) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/iqsms/client.rb', line 5

def self.connection_pool_for(url)
  @mutex.synchronize do
    @connection_pool ||= {}
    if @connection_pool[url].blank?
      @connection_pool[url] = ConnectionPool.new(size: 5, timeout: 5) do
        HTTP.persistent(url)
      end
    end
    @connection_pool[url]
  end
end

Instance Method Details

#balanceObject



72
73
74
75
76
# File 'lib/iqsms/client.rb', line 72

def balance
  request(:post, 'messages/v2/balance'.freeze) do |response|
    Response::Balance.new(response)
  end
end

#ping?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/iqsms/client.rb', line 84

def ping?
  request(:post, 'messages/v2/balance'.freeze) do |response|
    response.status.success?
  end
end

#send_sms(messages) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/iqsms/client.rb', line 29

def send_sms(messages)
  messages = Array(messages)
  check_messages_size!(messages)

  options = {
    statusQueueName: status_queue_name,
    messages: messages.map { |message| Message.message_to_hash(message) }
  }

  request(:post, 'send'.freeze, options) do |response|
    Response::SendSms.new(response)
  end
end

#sendersObject



78
79
80
81
82
# File 'lib/iqsms/client.rb', line 78

def senders
  request(:post, 'senders'.freeze) do |response|
    Response::Senders.new(response)
  end
end

#status(messages) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/iqsms/client.rb', line 43

def status(messages)
  messages = Array(messages)
  check_messages_size!(messages)

  options = {
    statusQueueName: status_queue_name,
    messages: messages.map { |message| Message.message_to_hash_for_status(message) }
  }

  request(:post, 'status'.freeze, options) do |response|
    Response::Status.new(response)
  end
end

#status_queue(status_queue_limit = 5) ⇒ Object



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

def status_queue(status_queue_limit = 5)
  if status_queue_name.blank?
    raise ArgumentError, 'status_queue_name must be set to use status_queue endpoint'
  end

  options = {
    statusQueueName: status_queue_name,
    statusQueueLimit: status_queue_limit
  }

  request(:post, 'statusQueue'.freeze, options) do |response|
    Response::StatusQueue.new(response)
  end
end