class Sms16Client

Constants

Version

Attributes

login[RW]
password[RW]

Public Class Methods

new(login, password) click to toggle source
# File lib/sms16client.rb, line 21
def initialize(login, password)
  @login = login
  @password = password
end

Public Instance Methods

get_balance() click to toggle source
# File lib/sms16client.rb, line 26
def get_balance
  query = ""
  
  xml = Builder::XmlMarkup.new(:target => query)
  xml.request {
    xml.security {
      xml.login(:value => login)
      xml.password(:value => password)
    }
  }

  return parse_response(execute_query(query, 'https://my.sms16.ru/xml/balance.php'))
end
get_message_state(message_id) click to toggle source
# File lib/sms16client.rb, line 59
def get_message_state(message_id)
  query = ""

  xml = Builder::XmlMarkup.new(:target => query)
  xml.request {
    xml.security {
      xml.login(:value => login)
      xml.password(:value => password)
    }
    xml.get_state {
      xml.id_sms message_id
    }
  }

  return parse_response(execute_query(query, 'https://my.sms16.ru/xml/state.php'))
end
send_message(message, destination, source) click to toggle source
# File lib/sms16client.rb, line 40
def send_message(message, destination, source)
  query = ""

  xml = Builder::XmlMarkup.new(:target => query)
  xml.request {
    xml.security {
      xml.login(:value => login)
      xml.password(:value => password)
    }
    xml.message(:type => "sms") {
      xml.sender source
      xml.text message
      xml.abonent(:phone => destination, :number_sms => "1")
    }
  }

  return parse_response(execute_query(query, 'https://my.sms16.ru/xml/'))
end

Private Instance Methods

balance_response(response) click to toggle source
# File lib/sms16client.rb, line 101
def balance_response(response)
  {
    :money => {
      :currency => response.money.currency,
      :amount => response.money.text
    },
    :sms => [
      {
        :area => response.sms[0].area,
        :amount => response.sms[0].text
      }
    ] 
  }
end
error_response(response) click to toggle source
# File lib/sms16client.rb, line 95
def error_response(response)
  {
    :error => response.error
  }
end
execute_query(query, uri) click to toggle source
# File lib/sms16client.rb, line 78
def execute_query(query, uri)
  url = URI.parse(uri)
  headers = {"Content-Type" => "text/xml"}
  request = Net::HTTP.new(url.host, url.port)
  request.use_ssl = true
  request.post(url.path, query, headers).body
end
information_response(response) click to toggle source
# File lib/sms16client.rb, line 116
def information_response(response)
  {
    :information => {
      :number_sms => response.information.number_sms,
      :id_sms => response.information.id_sms,
      :parts => response.information.parts,
      :response => response.information.text
    }
  }
end
parse_response(response) click to toggle source
# File lib/sms16client.rb, line 86
def parse_response(response)
  response = Response.parse(response)

  return error_response(response) if response.error? 
  return balance_response(response) if response.money?
  return information_response(response) if response.information?
  return state_response(response) if response.state?
end
state_response(response) click to toggle source
# File lib/sms16client.rb, line 127
def state_response(response)
  {
    :state => {
      :id_sms => response.state.id_sms,
      :time => response.state.time,
      :err => response.state.err,
      :response => response.state.text
    }
  }
end