module Talktome

Constants

ROOT_FOLDER

Root folder of the project structure

VERSION

Public Class Methods

auto_options(folder) click to toggle source

Infer all client and strategy options from environment variables.

# File lib/talktome.rb, line 40
def auto_options(folder)
  options = {}
  debug_folder = folder/"tmp"

  options[:debugger] = ->(message, user, handler) {
    debug_folder.mkdir_p unless debug_folder.exists?
    (debug_folder/"#{user[:email]}.html").write(message.to_html)
    (debug_folder/"#{user[:email]}.txt").write(message.to_text)
  } if ENV['TALKTOME_DEBUG']

  options[:strategies] = {}

  email_delivery = (ENV['TALKTOME_EMAIL_DELIVERY'] || "test").to_sym
  email_config   = {}

  email_config.merge!({
    address:   ENV['TALKTOME_SMTP_ADDRESS'],
    port:      ENV['TALKTOME_SMTP_PORT'].to_i,
    domain:    ENV['TALKTOME_SMTP_DOMAIN'],
    user_name: ENV['TALKTOME_SMTP_USER'],
    password:  ENV['TALKTOME_SMTP_PASSWORD'],
    enable_starttls_auto: (ENV['TALKTOME_SMTP_STARTTLS_AUTO'] != 'false')
  }) if email_delivery == :smtp

  email_config.merge!({
    location: debug_folder.mkdir_p
  }) if email_delivery == :file

  options[:strategies][:email] = ::Talktome::Strategy::Email.new{|email|
    email.delivery_method(email_delivery, email_config)
    with_env('TALKTOME_EMAIL_DEFAULT_FROM'){|default|
      email.from(default)
    }
    with_env('TALKTOME_EMAIL_DEFAULT_TO'){|default|
      email.to(default)
    }
    with_env('TALKTOME_EMAIL_DEFAULT_REPLYTO'){|default|
      email.reply_to(default)
    }
  }

  if layouts_folder = ENV['TALKTOME_LAYOUTS_FOLDER']
    options[:layouts] = Path(layouts_folder)
  end

  options
end
env(which, default = nil) click to toggle source
# File lib/talktome.rb, line 10
def env(which, default = nil)
  if ENV.has_key?(which)
    got = ENV[which].to_s.strip
    return got unless got.empty?
  end
  default
end
redcarpet() click to toggle source
# File lib/talktome.rb, line 34
def redcarpet
  @redcarpet ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, extensions = {})
end
set_env(which, value, &bl) click to toggle source
# File lib/talktome.rb, line 26
def set_env(which, value, &bl)
  old, ENV[which] = ENV[which], value
  bl.call.tap{
    ENV[which] = old
  }
end
with_env(which, &bl) click to toggle source
# File lib/talktome.rb, line 19
def with_env(which, &bl)
  env(which).tap{|x|
    bl.call(x) unless x.nil?
  }
end

Private Instance Methods

auto_options(folder) click to toggle source

Infer all client and strategy options from environment variables.

# File lib/talktome.rb, line 40
def auto_options(folder)
  options = {}
  debug_folder = folder/"tmp"

  options[:debugger] = ->(message, user, handler) {
    debug_folder.mkdir_p unless debug_folder.exists?
    (debug_folder/"#{user[:email]}.html").write(message.to_html)
    (debug_folder/"#{user[:email]}.txt").write(message.to_text)
  } if ENV['TALKTOME_DEBUG']

  options[:strategies] = {}

  email_delivery = (ENV['TALKTOME_EMAIL_DELIVERY'] || "test").to_sym
  email_config   = {}

  email_config.merge!({
    address:   ENV['TALKTOME_SMTP_ADDRESS'],
    port:      ENV['TALKTOME_SMTP_PORT'].to_i,
    domain:    ENV['TALKTOME_SMTP_DOMAIN'],
    user_name: ENV['TALKTOME_SMTP_USER'],
    password:  ENV['TALKTOME_SMTP_PASSWORD'],
    enable_starttls_auto: (ENV['TALKTOME_SMTP_STARTTLS_AUTO'] != 'false')
  }) if email_delivery == :smtp

  email_config.merge!({
    location: debug_folder.mkdir_p
  }) if email_delivery == :file

  options[:strategies][:email] = ::Talktome::Strategy::Email.new{|email|
    email.delivery_method(email_delivery, email_config)
    with_env('TALKTOME_EMAIL_DEFAULT_FROM'){|default|
      email.from(default)
    }
    with_env('TALKTOME_EMAIL_DEFAULT_TO'){|default|
      email.to(default)
    }
    with_env('TALKTOME_EMAIL_DEFAULT_REPLYTO'){|default|
      email.reply_to(default)
    }
  }

  if layouts_folder = ENV['TALKTOME_LAYOUTS_FOLDER']
    options[:layouts] = Path(layouts_folder)
  end

  options
end
env(which, default = nil) click to toggle source
# File lib/talktome.rb, line 10
def env(which, default = nil)
  if ENV.has_key?(which)
    got = ENV[which].to_s.strip
    return got unless got.empty?
  end
  default
end
redcarpet() click to toggle source
# File lib/talktome.rb, line 34
def redcarpet
  @redcarpet ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, extensions = {})
end
set_env(which, value, &bl) click to toggle source
# File lib/talktome.rb, line 26
def set_env(which, value, &bl)
  old, ENV[which] = ENV[which], value
  bl.call.tap{
    ENV[which] = old
  }
end
with_env(which, &bl) click to toggle source
# File lib/talktome.rb, line 19
def with_env(which, &bl)
  env(which).tap{|x|
    bl.call(x) unless x.nil?
  }
end