Sha256: e960fa87d1d07d5f456a0c522372e4d0967b2cea4c242f838be1de5321ec8305

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require "savon"

module Decidim
  module ActionDelegator
    class SendSmsJobException < StandardError; end
    class SendSmsJob < ApplicationJob
      queue_as :default

      SMSVIRTUAL_WSDL_URL = "https://websms.masmovil.com/api_php/smsvirtual.wsdl"

      def perform(sender, mobile_phone_number, message)
        @sender = sender
        @mobile_phone_number = mobile_phone_number
        @message = message

        send_sms!

        raise SendSmsJobException, response unless success?
      end

      private

      attr_reader :sender, :mobile_phone_number, :message, :response

      def send_sms!
        @response = client.call(:send_sms,
                                message: {
                                  user: ENV["SMS_USER"],
                                  pass: ENV["SMS_PASS"],
                                  src: sender,
                                  dst: mobile_phone_number,
                                  msg: message
                                })
      end

      def success?
        parsed_response[:code] == "200"
      end

      def client
        @client ||= ::Savon.client(wsdl: SMSVIRTUAL_WSDL_URL)
      end

      def parsed_response
        return @parsed_response if @parsed_response

        doc = Nokogiri::XML response.body[:send_sms_response][:result]

        @parsed_response = {
          code: doc.xpath("//codigo").text,
          description: doc.xpath("//descripcion").text
        }

        @parsed_response
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-action_delegator-0.7.2 app/jobs/decidim/action_delegator/send_sms_job.rb
decidim-action_delegator-0.7.1 app/jobs/decidim/action_delegator/send_sms_job.rb
decidim-action_delegator-0.7.0 app/jobs/decidim/action_delegator/send_sms_job.rb
decidim-action_delegator-0.6.0 app/jobs/decidim/action_delegator/send_sms_job.rb
decidim-action_delegator-0.5.0 app/jobs/decidim/action_delegator/send_sms_job.rb
decidim-action_delegator-0.4.1 app/jobs/decidim/action_delegator/send_sms_job.rb
decidim-action_delegator-0.4 app/jobs/decidim/action_delegator/send_sms_job.rb