Sha256: 19f066b26669ed6a4ba5989b7dcf1d520bc62f729406fd0efdfe458373e981b9

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require "text_opener/version"
require 'json'
require 'securerandom'
require 'tmpdir'
require 'launchy'
require 'erb'
require 'twilio-ruby'

module TextOpener
  def self.install
    NSA.new.tap(&:intercept)
  end

  class InterceptingMessage < Twilio::REST::Message; end
  class InterceptingMessages < Twilio::REST::Messages
    def create(params)
      tmp_dir = Dir.mktmpdir
      file_path = File.join(tmp_dir, "#{SecureRandom.uuid}.html")
      File.open(file_path, "w") do |file|
        template = ERB.new(<<-TEMPLATE)
          <html>
            <head><title>SMS to #{params[:to]}</title></head>
            <body>
              <dl>
              <dt>To:</dt><dd>#{params[:to]}</dd>
              <dt>From:</dt><dd>#{params[:from]}</dd>
              </dl>
              <p>#{params[:body]}</p>
            </body>
          </html>
        TEMPLATE
        file.write(template.result(binding))
      end
      Launchy.open(file_path)
    end
  end

  class NSA
    def intercept
      @original_messages = Twilio::REST::Messages
      Twilio::REST.send(:remove_const, :Messages)
      Twilio::REST.send(:const_set, :Messages, TextOpener::InterceptingMessages)
    end

    def restore
      Twilio::REST.send(:remove_const, :Messages)
      Twilio::REST.send(:const_set, :Messages, @original_messages)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
text_opener-0.0.1 lib/text_opener.rb