module Smslist::Client::State

Constants

STATE_ERRORS

Public Instance Methods

state(message_ids = []) click to toggle source

Get state for a list of message ids

@param message_ids [Array] Array of message ids @return [Hash] Hash with state, time or error for each message @example Get state for a list of message ids

client = Smslist.new(token: 'secret')

message_ids = %w(10001 10002 10003)
client.state(message_ids)
# File lib/smslist/client/state.rb, line 29
def state(message_ids = [])
  body = build_xml_body do |xml|
    xml.get_state {
      message_ids.each do |id|
        xml.id_sms id
      end
    }
  end

  response = parse_xml(post body.to_xml, :state)
  parse_state_response(response)
end

Private Instance Methods

parse_state_response(response) click to toggle source
# File lib/smslist/client/state.rb, line 44
def parse_state_response(response)
  response_array = response.xpath('response/state').map do |node|
    if node[:err] == '0'
      [
        node['id_sms'],
        {
          state: node.text,
          datetime: (DateTime.strptime(node['time'], '%Y-%m-%d %H:%M:%S') rescue nil)
        }
      ]
    else
      [node['id_sms'], { state: node.text,
        error: STATE_ERRORS[node['err'].to_i] }]
    end
  end

  Hash[*response_array.flatten]
end