Sha256: 7e0baa87d642fc49e7aac9fa62379c8463133d9d101c8c7eceb28eb4263537a4

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module PUNK
  class SendSmsWorker < Worker
    args :to, :body

    def validate
      validates_type String, :to
      validates_phone :to
      validates_includes PUNK.get.plivo.whitelist, :to if !PUNK.env.test? && !PUNK.get.plivo.mock && PUNK.get.plivo.whitelist.present?
      validates_type String, :body
      validates_length_range 1..1600, :body
    end

    def process
      if PUNK.env.test? || PUNK.get.plivo.mock
        plivo = PUNK.cache.get(:plivo) || []
        message =
          {
            sent: Time.now.utc.to_s,
            from: PUNK.get.plivo.number,
            to: to,
            body: body
          }
        plivo.prepend(message)
        PUNK.cache.set(:plivo, plivo)
        unless PUNK.env.test?
          require "launchy"
          Launchy.open("#{PUNK.get.app.url || "http://localhost:3000"}/plivo.html")
        end
        return
      end

      require "plivo"

      client = Plivo::RestClient.new(PUNK.get.plivo.auth_id, PUNK.get.plivo.auth_token)
      client.messages.create(PUNK.get.plivo.number, [to], body)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
punk-0.4.1 lib/punk/workers/send_sms_worker.rb