Class: FlowChat::Http::Gateway::Simple
- Inherits:
-
Object
- Object
- FlowChat::Http::Gateway::Simple
- Includes:
- GatewayAsyncSupport, Instrumentation
- Defined in:
- lib/flow_chat/http/gateway/simple.rb
Constant Summary
Constants included from Instrumentation
Instrumentation::DELIVERED_MESSAGE_ID_KEY, Instrumentation::DELIVERY_DURATION_KEY
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Attributes included from GatewayAsyncSupport
Class Method Summary collapse
-
.configure_middleware_stack(builder, custom_middleware) ⇒ Object
Configure HTTP specific middleware stack.
Instance Method Summary collapse
- #call(context) ⇒ Object
-
#initialize(app, user_params) ⇒ Simple
constructor
A new instance of Simple.
Methods included from GatewayAsyncSupport
#async_supported?, #enqueue_async_job, #extract_body_for_background, #extract_headers_for_background, #extract_host, #extract_path, #extract_remote_ip, #in_background?, #should_enqueue_async?, #side_events_already_published?
Methods included from Instrumentation
#elapsed_ms_since, #inbound_message?, #instrument, instrument, #platform_message_id_from, report_api_error, #report_delivery_failure, #report_delivery_success, #report_to_app, #report_to_subscribers
Constructor Details
#initialize(app, user_params) ⇒ Simple
Returns a new instance of Simple.
21 22 23 24 25 26 |
# File 'lib/flow_chat/http/gateway/simple.rb', line 21 def initialize(app, user_params) @app = app @user_params = user_params validate_user_params! end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
8 9 10 |
# File 'lib/flow_chat/http/gateway/simple.rb', line 8 def context @context end |
Class Method Details
.configure_middleware_stack(builder, custom_middleware) ⇒ Object
Configure HTTP specific middleware stack
11 12 13 14 15 16 17 18 19 |
# File 'lib/flow_chat/http/gateway/simple.rb', line 11 def self.configure_middleware_stack(builder, custom_middleware) FlowChat.logger.debug { "Simple: Configuring HTTP middleware stack" } builder.use custom_middleware FlowChat.logger.debug { "Simple: Added custom middleware" } builder.use FlowChat::Http::Middleware::ChoiceMapper FlowChat.logger.debug { "Simple: Added Http::Middleware::ChoiceMapper" } end |
Instance Method Details
#call(context) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/flow_chat/http/gateway/simple.rb', line 28 def call(context) @context = context @controller = context.controller params = @controller.request.params request = @controller.request # Validate request method unless request.get? || request.post? @controller.head :bad_request return end # Set request information from user_params context["request.id"] = @user_params[:session_id] context["request.user_id"] = @user_params[:user_id] context["request.user_name"] = @user_params[:name] if @user_params[:name] context["request.msisdn"] = @user_params[:msisdn] if @user_params[:msisdn] context["request.email"] = @user_params[:email] if @user_params[:email] context["request.message_id"] = SecureRandom.uuid context["request.timestamp"] = Time.current.iso8601 context["request.gateway"] = :http_simple context["request.platform"] = :http context["request.body"] = (params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params.to_h).transform_keys(&:to_s) # HTTP-specific request metadata context["http.method"] = request.method context["http.path"] = request.path context["http.user_agent"] = request.user_agent context.input = params["input"].presence || "" # Inbound media (optional): callers may submit a media URL if params["media_url"].present? media_type = params["media_type"].presence&.to_sym media_type = :document unless FlowChat::Media::CANONICAL_TYPES.include?(media_type) context["request.media"] = { type: media_type, url: params["media_url"], mime_type: params["mime_type"].presence } end # Instrument message received when user provides input (text or media) if (context) instrument(Events::MESSAGE_RECEIVED, { from: context["request.user_id"], message: context.input, timestamp: context["request.timestamp"] }) end # Determine routing: async enqueue, background execute, or inline if should_enqueue_async? # HTTP request with async enabled → enqueue job and return immediately enqueue_async_job @controller.render json: {status: "processing"} else # Background OR inline → process message # Process the request response = @app.call(context) # Handle nil response (e.g., from middleware that handles the response itself) unless response return @controller.render json: { type: :skip, session_id: context["request.id"], user_id: context["request.user_id"], timestamp: context["request.timestamp"] } end type, prompt, choices, media = response # Instrument message sent instrument(Events::MESSAGE_SENT, { to: context["request.user_id"], session_id: context["request.id"], message: context.input || "", message_type: (type == :prompt) ? "prompt" : "terminal", gateway: :http_simple, platform: :http, content_length: prompt.to_s.length, timestamp: context["request.timestamp"] }) # Render response as JSON response_data = render_response(type, prompt, choices, media) @controller.render json: response_data end end |