Sha256: b6b05f6aebdd6be584f7cfc503af9a2791c30002ee8af6cb6f46791439ee175d

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

module Ims
  class Sender
    def deliver(options = {text:nil, attachment:nil, contacts: []})
      if options[:text].nil? && options[:attachment].nil?
        raise "You must specific a message, either a text or file attachment."
      end

      if options[:contacts].empty?
        raise "You must specific at least one contact"
      end

      options[:contacts].each do |contact|
        _deliver(options[:text], options[:attachment], contact)
      end
    end

    private

    def _deliver(text, attachment, contact)
      deliver_text(text, contact) if text
      deliver_attachment(attachment, contact) if attachment
    end

    def deliver_attachment(attachment, contact)
      script = <<-SCRIPT
      on run argv
              set toAddress to first item of argv
              set theFilePath to second item of argv
              set theFile to POSIX file theFilePath
              tell application "System Events"
                      if exists file theFilePath then
                              tell application "Messages"
                                      send theFile to buddy toAddress of (service 1 whose service type is iMessage)
                              end tell
                      else
                              error "File not exist."
                      end if
              end tell
      end run
      SCRIPT
      `osascript -e'#{script}' '#{contact}' '#{attachment}'`
    end

    def deliver_text(text, contact)
      script = <<-SCRIPT
      on run argv
              set toAddress to first item of argv
              set message to second item of argv
              tell application "Messages"
                      send message to buddy toAddress of (service 1 whose service type is iMessage)
              end tell
      end run
      SCRIPT
      `osascript -e '#{script}' '#{contact}' '#{text}'`
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ims-0.8.2 lib/ims/sender.rb
ims-0.8.1 lib/ims/sender.rb
ims-0.8.0 lib/ims/sender.rb