class SmsClub::Client

API documentation smsclub.mobi/en/pages/show/api#xml

Attributes

from[RW]
password[RW]
transliterate[RW]
user_name[RW]

Public Class Methods

new(user_name, password, from: nil, transliterate: false) click to toggle source
# 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

Public Instance Methods

send_many(message, options = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/sms-club/client.rb, line 17
def send_one(message, options = {})
  send_many(message, options).first
end
status_for(smscid) click to toggle source
# File lib/sms-club/client.rb, line 48
def status_for(smscid)
  statuses_for(smscid).first[smscid]
end
statuses_for(smscid) click to toggle source
# 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

Protected Instance Methods

connection() click to toggle source
# File lib/sms-club/client.rb, line 76
def connection
  @connection ||= Faraday.new(url: 'https://gate.smsclub.mobi/') do |faraday|
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  end
end
response_error(xml_doc) click to toggle source
# File lib/sms-club/client.rb, line 91
def response_error(xml_doc)
  xml_doc.xpath('//text').text
end
response_failed?(xml_doc) click to toggle source
# File lib/sms-club/client.rb, line 87
def response_failed?(xml_doc)
  xml_doc.xpath('//status').text != 'OK'
end
translit_message(message) click to toggle source
# File lib/sms-club/client.rb, line 83
def translit_message(message)
  Translit.convert message
end