Class: SmsClub::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sms-club/client.rb

Overview

Direct Known Subclasses

AsyncClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_name, password, from: nil, transliterate: false) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
# File 'lib/sms-club/client.rb', line 10

def initialize(user_name, password, from: nil, transliterate: false)
  @user_name = user_name
  @password = password
  @from = from
  @transliterate = transliterate
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



8
9
10
# File 'lib/sms-club/client.rb', line 8

def from
  @from
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/sms-club/client.rb', line 8

def password
  @password
end

#transliterateObject

Returns the value of attribute transliterate.



8
9
10
# File 'lib/sms-club/client.rb', line 8

def transliterate
  @transliterate
end

#user_nameObject

Returns the value of attribute user_name.



8
9
10
# File 'lib/sms-club/client.rb', line 8

def user_name
  @user_name
end

Instance Method Details

#send_many(message, options = {}) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sms-club/client.rb', line 21

def send_many(message, options = {})
  fail ArgumentError, 'Recepient is not defined' unless options[:to]

  to = options[:to]
  to = to.map(&:to_s).join(';') if to.is_a? Array

  message = translit_message message if transliterate || options[:transliterate]
  msg_from = options[:from] || from

  payload = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.request_sendsms do
      xml.username { xml.cdata @user_name }
      xml.password { xml.cdata @password }
      xml.from { xml.cdata msg_from }
      xml.to { xml.cdata to }
      xml.text_ { xml.cdata message }
    end
  end

  response = connection.post '/hfw_smpp_addon/xmlsendsmspost.php', xmlrequest: payload.to_xml
  doc = Nokogiri::XML(response.body)

  raise SmsClubError, response_error(doc) if response_failed?(doc)

  doc.xpath('//mess').map(&:content)
end

#send_one(message, options = {}) ⇒ Object



17
18
19
# File 'lib/sms-club/client.rb', line 17

def send_one(message, options = {})
  send_many(message, options).first
end

#status_for(smscid) ⇒ Object



48
49
50
# File 'lib/sms-club/client.rb', line 48

def status_for(smscid)
  statuses_for(smscid).first[smscid]
end

#statuses_for(smscid) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sms-club/client.rb', line 52

def statuses_for(smscid)
  smscid = smscid.map(&:to_s).join(';') if smscid.is_a? Array

  payload = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.request_getstate do
      xml.username { xml.cdata @user_name }
      xml.password { xml.cdata @password }
      xml.smscid { xml.cdata smscid }
    end
  end

  response = connection.post '/hfw_smpp_addon/xmlgetsmsstatepost.php', xmlrequest: payload.to_xml

  doc = Nokogiri::XML(response.body)

  raise SmsClubError, response_error(doc) if response_failed?(doc)

  doc.xpath('//entry').map do |entry|
    { entry.xpath('smscid').text => entry.xpath('state').text.downcase.to_sym }
  end
end