Class: FlowChat::Http::Gateway::Simple
- Inherits:
-
Object
- Object
- FlowChat::Http::Gateway::Simple
- Includes:
- GatewayAsyncSupport, Instrumentation
- Defined in:
- lib/flow_chat/http/gateway/simple.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Attributes included from GatewayAsyncSupport
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?
Methods included from Instrumentation
#instrument, instrument, report_api_error
Constructor Details
#initialize(app, user_params) ⇒ Simple
Returns a new instance of Simple.
12 13 14 15 16 17 |
# File 'lib/flow_chat/http/gateway/simple.rb', line 12 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.
10 11 12 |
# File 'lib/flow_chat/http/gateway/simple.rb', line 10 def context @context end |
Instance Method Details
#call(context) ⇒ Object
19 20 21 22 23 24 25 26 27 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 |
# File 'lib/flow_chat/http/gateway/simple.rb', line 19 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 || "" # Instrument message received when user provides input if context.input.present? 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 |