inquirex 0.4.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +203 -52
- data/docs/badges/coverage_badge.svg +2 -2
- data/examples/01_readme_example.rb +55 -0
- data/examples/02_readme_mermaid.png +0 -0
- data/examples/02_readme_mermaid.rb +36 -0
- data/examples/03_send_email_actions.rb +97 -0
- data/examples/README.md +33 -0
- data/justfile +3 -2
- data/lib/inquirex/accumulator.rb +29 -0
- data/lib/inquirex/actions/action.rb +68 -0
- data/lib/inquirex/actions/base.rb +41 -0
- data/lib/inquirex/actions/custom.rb +31 -0
- data/lib/inquirex/actions/outbox.rb +57 -0
- data/lib/inquirex/actions/runner.rb +52 -0
- data/lib/inquirex/actions/send_email.rb +174 -0
- data/lib/inquirex/actions/template.rb +95 -0
- data/lib/inquirex/actions/webhook.rb +139 -0
- data/lib/inquirex/actions.rb +57 -0
- data/lib/inquirex/answers.rb +13 -0
- data/lib/inquirex/completion_metadata.rb +82 -0
- data/lib/inquirex/definition.rb +67 -5
- data/lib/inquirex/dsl/action_builder.rb +53 -0
- data/lib/inquirex/dsl/flow_builder.rb +49 -6
- data/lib/inquirex/engine/state_serializer.rb +6 -4
- data/lib/inquirex/engine.rb +97 -5
- data/lib/inquirex/errors.rb +5 -0
- data/lib/inquirex/graph/mermaid_exporter.rb +2 -0
- data/lib/inquirex/node.rb +2 -0
- data/lib/inquirex/rules/all.rb +12 -0
- data/lib/inquirex/rules/any.rb +11 -0
- data/lib/inquirex/rules/base.rb +6 -0
- data/lib/inquirex/rules/contains.rb +12 -0
- data/lib/inquirex/rules/equals.rb +12 -0
- data/lib/inquirex/rules/greater_than.rb +12 -0
- data/lib/inquirex/rules/less_than.rb +12 -0
- data/lib/inquirex/rules/not_empty.rb +12 -0
- data/lib/inquirex/validation/adapter.rb +1 -0
- data/lib/inquirex/validation/null_adapter.rb +5 -0
- data/lib/inquirex/version.rb +1 -1
- data/lib/inquirex/widget_registry.rb +1 -0
- data/lib/inquirex.rb +13 -0
- metadata +35 -5
data/lib/inquirex/accumulator.rb
CHANGED
|
@@ -8,9 +8,21 @@ module Inquirex
|
|
|
8
8
|
# @attr_reader name [Symbol] accumulator identifier (e.g. :price)
|
|
9
9
|
# @attr_reader type [Symbol] one of Node::TYPES (typically :currency, :integer, :decimal)
|
|
10
10
|
# @attr_reader default [Numeric] starting value (default: 0)
|
|
11
|
+
#
|
|
12
|
+
# @example Declare a running price total and contribute to it from a step
|
|
13
|
+
# Inquirex.define do
|
|
14
|
+
# accumulator :price, type: :currency, default: 0
|
|
15
|
+
# ask :dependents do
|
|
16
|
+
# type :integer
|
|
17
|
+
# accumulate :price, per_unit: 50
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
11
20
|
class Accumulator
|
|
12
21
|
attr_reader :name, :type, :default
|
|
13
22
|
|
|
23
|
+
# @param name [Symbol, String] accumulator identifier
|
|
24
|
+
# @param type [Symbol, String] value type, one of Node::TYPES
|
|
25
|
+
# @param default [Numeric] starting value before any contributions
|
|
14
26
|
def initialize(name:, type: :decimal, default: 0)
|
|
15
27
|
@name = name.to_sym
|
|
16
28
|
@type = type.to_sym
|
|
@@ -18,10 +30,19 @@ module Inquirex
|
|
|
18
30
|
freeze
|
|
19
31
|
end
|
|
20
32
|
|
|
33
|
+
# Serializes the accumulator to its wire format. The name is omitted —
|
|
34
|
+
# Definition#to_h keys the accumulators map by name.
|
|
35
|
+
#
|
|
36
|
+
# @return [Hash{String => Object}] e.g. { "type" => "currency", "default" => 0 }
|
|
21
37
|
def to_h
|
|
22
38
|
{ "type" => @type.to_s, "default" => @default }
|
|
23
39
|
end
|
|
24
40
|
|
|
41
|
+
# Deserializes an Accumulator from its wire format.
|
|
42
|
+
#
|
|
43
|
+
# @param name [Symbol, String] accumulator identifier (the map key)
|
|
44
|
+
# @param hash [Hash] type/default attributes (string or symbol keys)
|
|
45
|
+
# @return [Accumulator]
|
|
25
46
|
def self.from_h(name, hash)
|
|
26
47
|
new(
|
|
27
48
|
name: name,
|
|
@@ -44,10 +65,15 @@ module Inquirex
|
|
|
44
65
|
# @attr_reader shape [Symbol] one of :lookup, :per_selection, :per_unit, :flat
|
|
45
66
|
# @attr_reader payload [Object] shape-specific data (Hash or Numeric)
|
|
46
67
|
class Accumulation
|
|
68
|
+
# Valid accumulation shapes; exactly one must be declared per entry.
|
|
47
69
|
SHAPES = %i[lookup per_selection per_unit flat].freeze
|
|
48
70
|
|
|
49
71
|
attr_reader :target, :shape, :payload
|
|
50
72
|
|
|
73
|
+
# @param target [Symbol, String] accumulator name to contribute to
|
|
74
|
+
# @param shape [Symbol, String] one of SHAPES
|
|
75
|
+
# @param payload [Hash, Numeric] shape-specific data (Hash for :lookup/:per_selection)
|
|
76
|
+
# @raise [Errors::DefinitionError] when shape is not one of SHAPES
|
|
51
77
|
def initialize(target:, shape:, payload:)
|
|
52
78
|
@target = target.to_sym
|
|
53
79
|
@shape = shape.to_sym
|
|
@@ -73,6 +99,9 @@ module Inquirex
|
|
|
73
99
|
end
|
|
74
100
|
end
|
|
75
101
|
|
|
102
|
+
# Serializes to the single-key shape Hash that .from_h accepts.
|
|
103
|
+
#
|
|
104
|
+
# @return [Hash{String => Object}] e.g. { "per_unit" => 50 }
|
|
76
105
|
def to_h
|
|
77
106
|
{ @shape.to_s => serialize_payload }
|
|
78
107
|
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Inquirex
|
|
4
|
+
module Actions
|
|
5
|
+
# A named post-completion unit: an optional rule gating execution and an
|
|
6
|
+
# ordered list of effects. Declared with the DSL word `action`:
|
|
7
|
+
#
|
|
8
|
+
# action :client_receipt, if: not_empty(:email) do
|
|
9
|
+
# send_email to: "{{email}}", subject: "Thanks {{name}}!", text: "..."
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# Rules reuse the same serializable AST as transitions, so conditions
|
|
13
|
+
# survive the JSON round-trip. Non-serializable effects (run blocks) are
|
|
14
|
+
# stripped on serialization; an action left with no serializable effects
|
|
15
|
+
# is omitted from JSON entirely.
|
|
16
|
+
class Action
|
|
17
|
+
attr_reader :id, :rule, :effects
|
|
18
|
+
|
|
19
|
+
# @param id [Symbol] action identifier
|
|
20
|
+
# @param effects [Array<Actions::Base>] executed in declaration order
|
|
21
|
+
# @param rule [Rules::Base, nil] gate — action runs only when true
|
|
22
|
+
def initialize(id:, effects:, rule: nil)
|
|
23
|
+
@id = id.to_sym
|
|
24
|
+
@effects = effects.freeze
|
|
25
|
+
@rule = rule
|
|
26
|
+
freeze
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @param answers_hash [Hash] step_id => value context for rule evaluation
|
|
30
|
+
# @return [Boolean]
|
|
31
|
+
def applicable?(answers_hash)
|
|
32
|
+
@rule.nil? || @rule.evaluate(answers_hash)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @return [Boolean] whether anything survives JSON serialization
|
|
36
|
+
def serializable?
|
|
37
|
+
@effects.any?(&:serializable?)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @return [Hash] wire format; run blocks are stripped
|
|
41
|
+
def to_h
|
|
42
|
+
hash = { "id" => @id.to_s }
|
|
43
|
+
hash["if"] = @rule.to_h if @rule
|
|
44
|
+
hash["effects"] = @effects.select(&:serializable?).map(&:to_h)
|
|
45
|
+
hash
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# @param hash [Hash] string or symbol keys
|
|
49
|
+
# @return [Action]
|
|
50
|
+
def self.from_h(hash)
|
|
51
|
+
id = hash["id"] || hash[:id]
|
|
52
|
+
rule_data = hash["if"] || hash[:if]
|
|
53
|
+
effects_data = hash["effects"] || hash[:effects] || []
|
|
54
|
+
|
|
55
|
+
effects = effects_data.map do |effect_hash|
|
|
56
|
+
type = effect_hash["type"] || effect_hash[:type]
|
|
57
|
+
Actions.lookup(type).from_h(effect_hash)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
new(
|
|
61
|
+
id: id.to_sym,
|
|
62
|
+
effects: effects,
|
|
63
|
+
rule: rule_data ? Rules::Base.from_h(rule_data) : nil
|
|
64
|
+
)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Inquirex
|
|
4
|
+
module Actions
|
|
5
|
+
# Abstract base for action effects — the executable units inside an
|
|
6
|
+
# `action` block. Subclasses implement #call and, when serializable,
|
|
7
|
+
# #to_h / .from_h following the same round-trip pattern as Rules::Base.
|
|
8
|
+
#
|
|
9
|
+
# Effects that wrap Ruby procs (Actions::Custom) return false from
|
|
10
|
+
# #serializable? and are stripped from JSON, consistent with how
|
|
11
|
+
# lambdas are handled everywhere else in Inquirex.
|
|
12
|
+
class Base
|
|
13
|
+
# Executes the effect. Email-building effects append Mail::Message
|
|
14
|
+
# objects to the outbox; custom effects may do anything server-side.
|
|
15
|
+
#
|
|
16
|
+
# @param answers [Answers] completed answers
|
|
17
|
+
# @param outbox [Outbox] collector for built messages
|
|
18
|
+
# @return [void]
|
|
19
|
+
def call(answers, outbox)
|
|
20
|
+
raise NotImplementedError, "#{self.class}#call must be implemented"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @return [Boolean] whether this effect survives JSON serialization
|
|
24
|
+
def serializable? = true
|
|
25
|
+
|
|
26
|
+
# Hook for effects that must be checked against the definition carrying
|
|
27
|
+
# them (e.g. Webhook vs allowed_domains). Runs inside
|
|
28
|
+
# Definition#validate!, which both DSL-built and JSON-rehydrated
|
|
29
|
+
# definitions pass through — so violations fail at load time.
|
|
30
|
+
#
|
|
31
|
+
# @param _definition [Definition]
|
|
32
|
+
# @raise [Errors::DefinitionError] on violation
|
|
33
|
+
def validate_against(_definition); end
|
|
34
|
+
|
|
35
|
+
# @return [Hash]
|
|
36
|
+
def to_h
|
|
37
|
+
raise NotImplementedError, "#{self.class}#to_h must be implemented"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Inquirex
|
|
4
|
+
module Actions
|
|
5
|
+
# Escape-hatch effect wrapping an arbitrary Ruby block, declared in the
|
|
6
|
+
# DSL as `run { |answers, outbox| ... }`. Full language power — build a
|
|
7
|
+
# Mail::Message by hand with outbox.add_message, call a service, record
|
|
8
|
+
# metrics — at the cost of serialization: like every lambda in Inquirex,
|
|
9
|
+
# the block is stripped from JSON and exists only in Ruby-authored
|
|
10
|
+
# definitions.
|
|
11
|
+
class Custom < Base
|
|
12
|
+
# @param block [Proc] receives (answers, outbox)
|
|
13
|
+
def initialize(block)
|
|
14
|
+
super()
|
|
15
|
+
@block = block
|
|
16
|
+
freeze
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Invokes the wrapped block with the collected answers and the outbox.
|
|
20
|
+
#
|
|
21
|
+
# @param answers [Answers] completed answers
|
|
22
|
+
# @param outbox [Outbox] collector for messages the block may build
|
|
23
|
+
# @return [Object] whatever the block returns (recorded, not interpreted)
|
|
24
|
+
def call(answers, outbox)
|
|
25
|
+
@block.call(answers, outbox)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def serializable? = false
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Inquirex
|
|
4
|
+
module Actions
|
|
5
|
+
# Collected output of running a definition's actions: the Mail::Message
|
|
6
|
+
# objects built by send_email effects, plus a per-execution result trail.
|
|
7
|
+
#
|
|
8
|
+
# The outbox is attached to Answers#outbox and is intentionally excluded
|
|
9
|
+
# from Answers serialization — mail objects never leak into persisted
|
|
10
|
+
# answer data. Delivery is the host application's job:
|
|
11
|
+
#
|
|
12
|
+
# answers.outbox.each do |message|
|
|
13
|
+
# ActionMailer::Base.wrap_delivery_behavior(message)
|
|
14
|
+
# message.deliver
|
|
15
|
+
# end
|
|
16
|
+
class Outbox
|
|
17
|
+
include Enumerable
|
|
18
|
+
|
|
19
|
+
# One entry per effect execution (or per skipped action).
|
|
20
|
+
# status is :ok, :skipped (action rule was false), or :failed.
|
|
21
|
+
Result = Data.define(:action_id, :status, :error)
|
|
22
|
+
|
|
23
|
+
attr_reader :messages, :results
|
|
24
|
+
|
|
25
|
+
def initialize
|
|
26
|
+
@messages = []
|
|
27
|
+
@results = []
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @param mail [Mail::Message]
|
|
31
|
+
# @return [Mail::Message]
|
|
32
|
+
def add_message(mail)
|
|
33
|
+
@messages << mail
|
|
34
|
+
mail
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @param action_id [Symbol]
|
|
38
|
+
# @param status [Symbol] :ok, :skipped, or :failed
|
|
39
|
+
# @param error [StandardError, nil]
|
|
40
|
+
def record(action_id, status, error: nil)
|
|
41
|
+
@results << Result.new(action_id:, status:, error:)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Iterates over built Mail::Message objects.
|
|
45
|
+
def each(&) = @messages.each(&)
|
|
46
|
+
|
|
47
|
+
# @return [Integer] number of built messages
|
|
48
|
+
def size = @messages.size
|
|
49
|
+
|
|
50
|
+
# @return [Boolean]
|
|
51
|
+
def empty? = @messages.empty?
|
|
52
|
+
|
|
53
|
+
# @return [Array<Result>] executions that raised
|
|
54
|
+
def failures = @results.select { |result| result.status == :failed }
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Inquirex
|
|
4
|
+
module Actions
|
|
5
|
+
# Executes a definition's actions against completed answers, in
|
|
6
|
+
# declaration order, populating Answers#outbox with built messages and a
|
|
7
|
+
# result trail.
|
|
8
|
+
#
|
|
9
|
+
# Deliberately separate from the Engine: in the cross-site architecture
|
|
10
|
+
# the frontend collects answers and a different server process
|
|
11
|
+
# post-processes them, so the runner is a pure function of
|
|
12
|
+
# (definition, answers) — which also makes rebuilding messages inside a
|
|
13
|
+
# background job trivial.
|
|
14
|
+
#
|
|
15
|
+
# A failing effect records a :failed result and never aborts the other
|
|
16
|
+
# actions; the host inspects outbox.failures and decides what to do.
|
|
17
|
+
class Runner
|
|
18
|
+
# @param definition [Definition]
|
|
19
|
+
def initialize(definition)
|
|
20
|
+
@definition = definition
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @param answers [Answers, Hash] completed answers (a Hash is wrapped)
|
|
24
|
+
# @return [Answers] the (possibly wrapped) answers with #outbox populated
|
|
25
|
+
def call(answers)
|
|
26
|
+
answers = Answers.new(answers) unless answers.is_a?(Answers)
|
|
27
|
+
context = answers.to_h
|
|
28
|
+
|
|
29
|
+
@definition.actions.each do |action|
|
|
30
|
+
unless action.applicable?(context)
|
|
31
|
+
answers.outbox.record(action.id, :skipped)
|
|
32
|
+
next
|
|
33
|
+
end
|
|
34
|
+
execute(action, answers)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
answers
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def execute(action, answers)
|
|
43
|
+
action.effects.each do |effect|
|
|
44
|
+
effect.call(answers, answers.outbox)
|
|
45
|
+
answers.outbox.record(action.id, :ok)
|
|
46
|
+
rescue StandardError => e
|
|
47
|
+
answers.outbox.record(action.id, :failed, error: e)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Inquirex
|
|
4
|
+
module Actions
|
|
5
|
+
# Declarative email effect. Builds a Mail::Message (the object ActionMailer
|
|
6
|
+
# itself wraps) from {{field}} templates and appends it to the outbox.
|
|
7
|
+
# Nothing is delivered — the host application sends the messages.
|
|
8
|
+
#
|
|
9
|
+
# Scalar fields (to, from, cc, bcc, reply_to, subject) and the text body
|
|
10
|
+
# render values verbatim; the html body HTML-escapes every interpolated
|
|
11
|
+
# value automatically. Both bodies given => multipart/alternative.
|
|
12
|
+
#
|
|
13
|
+
# Bodies accept an inline template String or { file: "path" }, which is
|
|
14
|
+
# read once at definition time and inlined — a definition rehydrated from
|
|
15
|
+
# JSON never touches the filesystem.
|
|
16
|
+
#
|
|
17
|
+
# Images: reference external URLs in the html body. Attachments are
|
|
18
|
+
# deliberately unsupported.
|
|
19
|
+
#
|
|
20
|
+
# The mail gem is a soft dependency, required only when a message is
|
|
21
|
+
# actually built. Rails hosts always have it (ActionMailer depends on it).
|
|
22
|
+
class SendEmail < Base
|
|
23
|
+
# Scalar header fields rendered verbatim via Template.render_text in #to_mail and #to_h.
|
|
24
|
+
SCALAR_FIELDS = %i[to from cc bcc reply_to subject].freeze
|
|
25
|
+
|
|
26
|
+
# @return [String] required recipient / subject templates ({{field}} placeholders allowed)
|
|
27
|
+
attr_reader :to, :subject
|
|
28
|
+
|
|
29
|
+
# @return [String, nil] optional address templates ({{field}} placeholders allowed)
|
|
30
|
+
attr_reader :from, :cc, :bcc, :reply_to
|
|
31
|
+
|
|
32
|
+
# @return [String, nil] body template, inlined at definition time when { file: } was given
|
|
33
|
+
attr_reader :text, :html
|
|
34
|
+
|
|
35
|
+
# @return [Hash{String => String}] extra headers (values support {{field}})
|
|
36
|
+
attr_reader :headers
|
|
37
|
+
|
|
38
|
+
# @param to [String] recipient template (required)
|
|
39
|
+
# @param subject [String] subject template (required)
|
|
40
|
+
# @param text [String, Hash, nil] plain-text body template or { file: }
|
|
41
|
+
# @param html [String, Hash, nil] HTML body template or { file: }
|
|
42
|
+
# @param headers [Hash] extra headers (values support {{field}})
|
|
43
|
+
# @raise [Errors::DefinitionError] when required fields are missing
|
|
44
|
+
def initialize(to:, subject:, from: nil, cc: nil, bcc: nil, reply_to: nil,
|
|
45
|
+
text: nil, html: nil, headers: {})
|
|
46
|
+
super()
|
|
47
|
+
@to = to
|
|
48
|
+
@from = from
|
|
49
|
+
@cc = cc
|
|
50
|
+
@bcc = bcc
|
|
51
|
+
@reply_to = reply_to
|
|
52
|
+
@subject = subject
|
|
53
|
+
@text = resolve_body(text)
|
|
54
|
+
@html = resolve_body(html)
|
|
55
|
+
@headers = headers.transform_keys(&:to_s).freeze
|
|
56
|
+
validate!
|
|
57
|
+
freeze
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Builds the message and appends it to the outbox.
|
|
61
|
+
#
|
|
62
|
+
# @param answers [Answers] completed answers
|
|
63
|
+
# @param outbox [Outbox] receives the built Mail::Message
|
|
64
|
+
# @return [void]
|
|
65
|
+
def call(answers, outbox)
|
|
66
|
+
outbox.add_message(to_mail(answers))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Builds a Mail::Message from the templates and the given answers.
|
|
70
|
+
# Pure function — safe to call from a background job to rebuild
|
|
71
|
+
# messages from persisted answers.
|
|
72
|
+
#
|
|
73
|
+
# @param answers [Answers]
|
|
74
|
+
# @return [Mail::Message]
|
|
75
|
+
def to_mail(answers)
|
|
76
|
+
require_mail!
|
|
77
|
+
mail = ::Mail.new
|
|
78
|
+
SCALAR_FIELDS.each do |field|
|
|
79
|
+
value = public_send(field)
|
|
80
|
+
mail.public_send(:"#{field}=", Template.render_text(value, answers)) if value
|
|
81
|
+
end
|
|
82
|
+
@headers.each { |name, value| mail.header[name] = Template.render_text(value.to_s, answers) }
|
|
83
|
+
attach_bodies(mail, answers)
|
|
84
|
+
mail
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# @return [Hash] wire format, same shape .from_h accepts
|
|
88
|
+
def to_h
|
|
89
|
+
hash = { "type" => "send_email" }
|
|
90
|
+
SCALAR_FIELDS.each do |field|
|
|
91
|
+
value = public_send(field)
|
|
92
|
+
hash[field.to_s] = value if value
|
|
93
|
+
end
|
|
94
|
+
hash["text"] = @text if @text
|
|
95
|
+
hash["html"] = @html if @html
|
|
96
|
+
hash["headers"] = @headers unless @headers.empty?
|
|
97
|
+
hash
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# @param hash [Hash] string or symbol keys
|
|
101
|
+
# @return [SendEmail]
|
|
102
|
+
def self.from_h(hash)
|
|
103
|
+
fetch = ->(key) { hash[key.to_s] || hash[key.to_sym] }
|
|
104
|
+
new(
|
|
105
|
+
to: fetch.call(:to),
|
|
106
|
+
from: fetch.call(:from),
|
|
107
|
+
cc: fetch.call(:cc),
|
|
108
|
+
bcc: fetch.call(:bcc),
|
|
109
|
+
reply_to: fetch.call(:reply_to),
|
|
110
|
+
subject: fetch.call(:subject),
|
|
111
|
+
text: fetch.call(:text),
|
|
112
|
+
html: fetch.call(:html),
|
|
113
|
+
headers: fetch.call(:headers) || {}
|
|
114
|
+
)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
private
|
|
118
|
+
|
|
119
|
+
def attach_bodies(mail, answers)
|
|
120
|
+
text = @text && Template.render_text(@text, answers)
|
|
121
|
+
html = @html && Template.render_html(@html, answers)
|
|
122
|
+
if text && html
|
|
123
|
+
mail.text_part = build_part("text/plain; charset=UTF-8", text)
|
|
124
|
+
mail.html_part = build_part("text/html; charset=UTF-8", html)
|
|
125
|
+
elsif html
|
|
126
|
+
mail.content_type = "text/html; charset=UTF-8"
|
|
127
|
+
mail.body = html
|
|
128
|
+
else
|
|
129
|
+
mail.body = text
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def build_part(content_type, body)
|
|
134
|
+
part = ::Mail::Part.new
|
|
135
|
+
part.content_type = content_type
|
|
136
|
+
part.body = body
|
|
137
|
+
part
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Inline template string, or { file: "path" } read once at definition time.
|
|
141
|
+
def resolve_body(value)
|
|
142
|
+
return value if value.nil? || value.is_a?(String)
|
|
143
|
+
|
|
144
|
+
path = value.is_a?(Hash) && (value[:file] || value["file"])
|
|
145
|
+
return File.read(File.expand_path(path)) if path.is_a?(String)
|
|
146
|
+
|
|
147
|
+
raise Errors::DefinitionError,
|
|
148
|
+
"send_email body must be a template String or { file: \"path\" }, got #{value.inspect}"
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def validate!
|
|
152
|
+
raise Errors::DefinitionError, "send_email requires to:" if blank?(@to)
|
|
153
|
+
raise Errors::DefinitionError, "send_email requires subject:" if blank?(@subject)
|
|
154
|
+
return unless @text.nil? && @html.nil?
|
|
155
|
+
|
|
156
|
+
raise Errors::DefinitionError, "send_email requires a text: or html: body"
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def blank?(value) = value.nil? || value.to_s.strip.empty?
|
|
160
|
+
|
|
161
|
+
def require_mail!
|
|
162
|
+
return if defined?(::Mail)
|
|
163
|
+
|
|
164
|
+
require "mail"
|
|
165
|
+
rescue LoadError
|
|
166
|
+
raise Errors::ActionError,
|
|
167
|
+
"send_email requires the mail gem — add `gem \"mail\"` to your Gemfile " \
|
|
168
|
+
"(Rails applications already have it via ActionMailer)"
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
register(:send_email, SendEmail)
|
|
173
|
+
end
|
|
174
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "cgi/escape"
|
|
4
|
+
|
|
5
|
+
module Inquirex
|
|
6
|
+
module Actions
|
|
7
|
+
# Renders {{field}} placeholders against collected answers.
|
|
8
|
+
#
|
|
9
|
+
# Placeholders are dot-notation keys resolved via Answers#to_flat_h
|
|
10
|
+
# ("{{email}}", "{{business.count}}"). The syntax is deliberately inert —
|
|
11
|
+
# no code execution — so flow definitions stored in a database can be
|
|
12
|
+
# rendered server-side safely and a visual editor can preview templates
|
|
13
|
+
# client-side. It matches the {{field}} placeholder convention used by
|
|
14
|
+
# inquirex-llm prompts.
|
|
15
|
+
#
|
|
16
|
+
# Rendering modes:
|
|
17
|
+
# - render_text — values inserted verbatim
|
|
18
|
+
# - render_html — every interpolated value is HTML-escaped
|
|
19
|
+
#
|
|
20
|
+
# The built-in {{answers_summary}} placeholder expands to all collected
|
|
21
|
+
# answers: "key: value" lines in text mode, a simple table in HTML mode.
|
|
22
|
+
# Unknown fields render as empty strings; array values join with ", ".
|
|
23
|
+
module Template
|
|
24
|
+
# Matches one {{field}} placeholder; capture 1 is the dot-notation key.
|
|
25
|
+
PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/
|
|
26
|
+
# Reserved placeholder key that expands to a summary of all collected answers.
|
|
27
|
+
SUMMARY_KEY = "answers_summary"
|
|
28
|
+
|
|
29
|
+
module_function
|
|
30
|
+
|
|
31
|
+
# @param string [String] template with {{field}} placeholders
|
|
32
|
+
# @param answers [Answers]
|
|
33
|
+
# @return [String]
|
|
34
|
+
def render_text(string, answers)
|
|
35
|
+
render(string, answers, html: false)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @example Interpolated values are HTML-escaped
|
|
39
|
+
# answers = Inquirex::Answers.new(name: "Bob & Sons")
|
|
40
|
+
# Inquirex::Actions::Template.render_html("<p>{{name}}</p>", answers)
|
|
41
|
+
# # => "<p>Bob & Sons</p>"
|
|
42
|
+
#
|
|
43
|
+
# @param string [String] template with {{field}} placeholders
|
|
44
|
+
# @param answers [Answers]
|
|
45
|
+
# @return [String]
|
|
46
|
+
def render_html(string, answers)
|
|
47
|
+
render(string, answers, html: true)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Shared implementation behind render_text and render_html.
|
|
51
|
+
#
|
|
52
|
+
# @param string [String] template with {{field}} placeholders
|
|
53
|
+
# @param answers [Answers]
|
|
54
|
+
# @param html [Boolean] whether to HTML-escape interpolated values
|
|
55
|
+
# @return [String]
|
|
56
|
+
def render(string, answers, html:)
|
|
57
|
+
flat = answers.to_flat_h
|
|
58
|
+
string.gsub(PLACEHOLDER) do
|
|
59
|
+
key = Regexp.last_match(1)
|
|
60
|
+
next(html ? summary_html(flat) : summary_text(flat)) if key == SUMMARY_KEY
|
|
61
|
+
|
|
62
|
+
value = format_value(flat[key])
|
|
63
|
+
html ? CGI.escapeHTML(value) : value
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Formats a single answer value for interpolation.
|
|
68
|
+
#
|
|
69
|
+
# @param value [Object] raw answer value (nil renders as "")
|
|
70
|
+
# @return [String] arrays joined with ", ", everything else via #to_s
|
|
71
|
+
def format_value(value)
|
|
72
|
+
value.is_a?(Array) ? value.join(", ") : value.to_s
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Expands {{answers_summary}} in text mode.
|
|
76
|
+
#
|
|
77
|
+
# @param flat [Hash{String => Object}] flat answers, dot-notation keys
|
|
78
|
+
# @return [String] one "key: value" line per answer
|
|
79
|
+
def summary_text(flat)
|
|
80
|
+
flat.map { |key, value| "#{key}: #{format_value(value)}" }.join("\n")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Expands {{answers_summary}} in HTML mode.
|
|
84
|
+
#
|
|
85
|
+
# @param flat [Hash{String => Object}] flat answers, dot-notation keys
|
|
86
|
+
# @return [String] a simple HTML table with one row per answer, fully escaped
|
|
87
|
+
def summary_html(flat)
|
|
88
|
+
rows = flat.map do |key, value|
|
|
89
|
+
"<tr><th>#{CGI.escapeHTML(key)}</th><td>#{CGI.escapeHTML(format_value(value))}</td></tr>"
|
|
90
|
+
end
|
|
91
|
+
%(<table class="inquirex-answers">#{rows.join}</table>)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|