Class: FlowChat::Http::Middleware::ChoiceMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_chat/http/middleware/choice_mapper.rb

Overview

Maps a web client's reply back to the choice it belongs to.

A web client renders the choices itself, so what it sends back is whatever it decided to send: the key it was given, or the label it actually put on the button. Sending the label is the better of the two, because a transcript then reads back as what the visitor pressed rather than as an internal id, but the flow branches on keys.

So both are accepted. Nothing is truncated here - the client decides its own widths - so a set of distinct labels is passed through exactly as the flow wrote it.

Matching is exact, with no normalization on either side. A client echoes back the string it was handed rather than a person typing it, so nothing drifts on the way - and every transform that could have absorbed such a drift can also merge two choices into one entry, which is how the second of a pair became unpickable in the first place.

The one thing that does get rewritten is a set whose labels are identical. Two choices both labelled "Savings" cannot be told apart by a visitor reading them either, so FlowChat::ChoiceTitles numbers the whole set. Previously the second simply lost to the first (mapping[label] ||= key) and could not be picked at all.

Constant Summary collapse

SESSION_KEY =
"http.choice_mapping"
UNCAPPED =

The client renders these, so there is no platform width to fit.

Float::INFINITY

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ChoiceMapper

Returns a new instance of ChoiceMapper.



33
34
35
36
# File 'lib/flow_chat/http/middleware/choice_mapper.rb', line 33

def initialize(app)
  @app = app
  FlowChat.logger.debug { "Http::ChoiceMapper: Initialized HTTP choice mapping middleware" }
end

Instance Method Details

#call(context) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/flow_chat/http/middleware/choice_mapper.rb', line 38

def call(context)
  resolve_input(context)

  type, prompt, choices, media = @app.call(context)

  choices = remember(context, choices)

  [type, prompt, choices, media]
end