class Verb::Message

Constants

API_URL

Attributes

context[R]
files[R]

Public Class Methods

new(params = {}, debug = false) click to toggle source
# File lib/verb.rb, line 33
def initialize(params = {}, debug = false)
  @context = {}
  @files = []
  @context[:tags] = []
  @debug = debug
  @api_key = nil

  params.each do |k, v|
    if k != :api_key
      @context[k] = v
    else 
      @context[:pid] = v.split('-').last # We add the project ID to the ctx

      @api_key = v.split('-').first
    end
  end
end

Public Instance Methods

attach(files) click to toggle source

Attach files to the message (if possible)

# File lib/verb.rb, line 52
def attach(files)
  if @context[:type] == :sms
    raise 'This message type cannot have file attachments'
  end

  parsed_files(files).each do |f|
    @files << f
  end
end
send(args = {}) click to toggle source

Send the message to the Verb API

# File lib/verb.rb, line 74
def send(args = {})
  payload = {
    files: []
  }

  payload.merge!(args)
  payload.merge!(@context)

  # Attach Files

  @files.each_with_index do |f, idx|
    payload[:files] << File.open(f)
  end

  log(payload)

  begin
    RestClient.post API_URL, payload, {
      'x-api-key': @api_key
    }
  rescue RestClient::ExceptionWithResponse => e
    e.response
  end
end
tag(tags) click to toggle source

Add tags to the internal tag array

# File lib/verb.rb, line 63
def tag(tags)
  if tags.is_a? Array
    tags.each do |t|
      @context[:tags] << t
    end
  else
    @context[:tags] << tags
  end
end

Private Instance Methods

canonical_path(f) click to toggle source

Ensure that we get the path to the file instead of the File objects

# File lib/verb.rb, line 117
def canonical_path(f)
  buffer = nil

  if f.kind_of?(String)
    if File.exist?(f)
      buffer = f
    end
  end

  if f.kind_of?(File)
    buffer = f.path
  end

  buffer
end
log(s) click to toggle source

Perform the message logging

# File lib/verb.rb, line 134
def log(s)
  if @debug
    require 'logger'
    Logger.new(STDOUT).debug(s)
  end
end
parsed_files(files) click to toggle source

Parse the files and return an array of files that exist

# File lib/verb.rb, line 102
def parsed_files(files)
  buffer = []

  if files.kind_of?(Array)
    files.each do |f|
      buffer << canonical_path(f)
    end
  else
    buffer << canonical_path(files)
  end

  buffer.compact
end