<commit_before>require 'open-uri'
class SMSApi
  attr_accessor :client

  def initialize
    @client = Savon.client(wsdl: url)
  end

  def url
    return "" unless end_point_available?
    open(Rails.application.secrets.sms_end_point).base_uri.to_s
  end

  def authorization
    Base64.encode64("#{Rails.application.secrets.sms_username}:#{Rails.application.secrets.sms_password}")
  end

  def sms_deliver(phone, code)
    return stubbed_response unless end_point_available?

    response = client.call(:enviar_sms_simples, message: request(phone, code))
    success?(response)
  end

  def request(phone, code)
    { autorizacion:  authorization,
      destinatarios: { destinatario: phone },
      texto_mensaje: "Clave para verificarte: #{code}. <NAME> - Ayuntamiento de Madrid",
      solicita_notificacion: "All" }
  end

  def success?(response)
    response.body[:respuesta_sms][:respuesta_servicio_externo][:texto_respuesta] == "Success"
  end

  def end_point_available?
    Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?
  end<commit_msg>fixes exception thrown when sms api returns an invalid response<commit_after>require 'open-uri'
class SMSApi
  attr_accessor :client

  def initialize
    @client = Savon.client(wsdl: url)
  end

  def url
    return "" unless end_point_available?
    open(Rails.application.secrets.sms_end_point).base_uri.to_s
  end

  def authorization
    Base64.encode64("#{Rails.application.secrets.sms_username}:#{Rails.application.secrets.sms_password}")
  end

  def sms_deliver(phone, code)
    return stubbed_response unless end_point_available?

    response = client.call(:enviar_sms_simples, message: request(phone, code))
    success?(response)
  end

  def request(phone, code)
    { autorizacion:  authorization,
      destinatarios: { destinatario: phone },
      texto_mensaje: "Clave para verificarte: #{code}. <NAME> - Ayuntamiento de Madrid",
      solicita_notificacion: "All" }
  end

  def success?(response)
    response.body[:respuesta_sms] &&
    response.body[:respuesta_sms][:respuesta_servicio_externo] &&
    response.body[:respuesta_sms][:respuesta_servicio_externo][:texto_respuesta] == "Success"
  end

  def end_point_available?
    Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?
  end
{'fraction_non_alphanumeric': 0.10715835140997831, 'fraction_numerical': 0.0034707158351409977, 'mean_word_length': 4.464454976303317, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2431336', 'n_tokens_mistral': 808, 'n_tokens_neox': 773, 'n_words': 146}