Module: Bugsage::JsonEndpoint

Included in:
AiChat, AiPanel, FixApplicator, InlineConsole, SessionClear
Defined in:
lib/bugsage/json_endpoint.rb

Overview

Shared Rack JSON helpers for BugSage HTTP endpoints. Extend on endpoint classes: extend JsonEndpoint

Instance Method Summary collapse

Instance Method Details

#error_response(message) ⇒ Object



26
27
28
# File 'lib/bugsage/json_endpoint.rb', line 26

def error_response(message)
  { ok: false, error: message.to_s }
end

#json_response(payload = nil, status: 200, **attrs) ⇒ Object



21
22
23
24
# File 'lib/bugsage/json_endpoint.rb', line 21

def json_response(payload = nil, status: 200, **attrs)
  body = payload.nil? ? attrs : payload
  [status, { "Content-Type" => "application/json" }, [JSON.generate(body)]]
end

#not_foundObject



30
31
32
# File 'lib/bugsage/json_endpoint.rb', line 30

def not_found
  [404, { "Content-Type" => "text/plain" }, [Bugsage.t("common.not_found")]]
end

#parse_request_body(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bugsage/json_endpoint.rb', line 9

def parse_request_body(env)
  body = env["rack.input"]
  raw = body.respond_to?(:read) ? body.read : body.to_s
  body.rewind if body.respond_to?(:rewind)

  return {} if raw.to_s.strip.empty?

  JSON.parse(raw)
rescue JSON::ParserError
  {}
end