<commit_before># frozen_string_literal: true

require 'rack'

module ActionSms
  module Providers
    class LinkMobilityProvider < Provider
      DELIVERED_OK = 'SMS sent'
      DELIVER_EXCEPTION = 'Exception: %s'
      SMS_GATEWAY_ERROR = 'SMS Gateway Error Code: %s'
      FORMAT = 'UNICODE'

      def self.deliver(message)
        all_ok = false

        begin
          url = 'https://wsx.sp247.net/linkdk/'

          unless message.phone_number.starts_with?('+')
            phone_number = '+' + message.phone_number
          end
          from = message.from.presence || ActionSms.options(:from)

          body = {
            username: ActionSms.options(:username),
            password: <PASSWORD>(:password),
            to: phone_number,
            from: from,
            message: message.body,
            charset: 'UTF-8',
            format: FORMAT
          }

          if message.status_report_url.present?
            body[:status] = true
            body[:statusurl] = message.status_report_url
          end

          response = RestClient.post(url, body.to_json, content_type: 'application/json; charset=utf-8')

          all_ok = response.code == 201
          if all_ok
            parsed_response = JSON.parse(response.body)
            message.message_id = parsed_response['details']['batchid']
            message.after_send(true, DELIVERED_OK)
          else
            message.after_send(false, SMS_GATEWAY_ERROR % response.code)
          end
        rescue URI::InvalidURIError => e
          message.after_send(false, DELIVER_EXCEPTION % e.to_s)
        rescue StandardError => e
          message.after_send(false, DELIVER_EXCEPTION % e.to_s)
        end

        all_ok<commit_msg>LinkMobility: remove + in phone numbers and use GET request<commit_after># frozen_string_literal: true

require 'rack'

module ActionSms
  module Providers
    class LinkMobilityProvider < Provider
      DELIVERED_OK = 'SMS sent'
      DELIVER_EXCEPTION = 'Exception: %s'
      SMS_GATEWAY_ERROR = 'SMS Gateway Error Code: %s'
      FORMAT = 'UNICODE'

      def self.deliver(message)
        all_ok = false

        begin
          url = 'https://wsx.sp247.net/linkdk/'

          phone_number = message.phone_number.gsub('+', '')
          from = message.from.presence || ActionSms.options(:from)

          body = {
            username: ActionSms.options(:username),
            password: <PASSWORD>(:password),
            to: phone_number,
            from: from,
            message: message.body,
            charset: 'UTF-8',
            format: FORMAT
          }

          if message.status_report_url.present?
            body[:status] = true
            body[:statusurl] = message.status_report_url
          end

          response = RestClient.get(url, params: body)

          all_ok = response.code == 200
          if all_ok
            parsed_response = JSON.parse(response.body)
            message.message_id = parsed_response['details']['batchid']
            message.after_send(true, DELIVERED_OK)
          else
            message.after_send(false, SMS_GATEWAY_ERROR % response.code)
          end
        rescue URI::InvalidURIError => e
          message.after_send(false, DELIVER_EXCEPTION % e.to_s)
        rescue StandardError => e
          message.after_send(false, DELIVER_EXCEPTION % e.to_s)
        end

        all_ok
{'fraction_non_alphanumeric': 0.09389810426540285, 'fraction_numerical': 0.004443127962085308, 'mean_word_length': 2.0151785714285713, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '2481207', 'n_tokens_mistral': 1020, 'n_tokens_neox': 972, 'n_words': 200}