Module: Vortex::Sinatra::Helpers
- Defined in:
- lib/vortex/sinatra.rb
Overview
Helper methods for Sinatra apps using Vortex
Instance Method Summary collapse
- #authenticate_vortex_user ⇒ Object
- #authorize_vortex_operation(operation, user) ⇒ Object
- #parse_json_body ⇒ Object
- #render_bad_request(message) ⇒ Object
- #render_forbidden(message) ⇒ Object
- #render_json(data) ⇒ Object
- #render_not_found(message) ⇒ Object
- #render_server_error(message) ⇒ Object
- #render_unauthorized(message) ⇒ Object
- #vortex_client ⇒ Object
- #with_vortex_error_handling(&block) ⇒ Object
Instance Method Details
#authenticate_vortex_user ⇒ Object
220 221 222 223 |
# File 'lib/vortex/sinatra.rb', line 220 def authenticate_vortex_user # Default implementation - should be overridden in app nil end |
#authorize_vortex_operation(operation, user) ⇒ Object
225 226 227 228 |
# File 'lib/vortex/sinatra.rb', line 225 def (operation, user) # Default implementation - should be overridden in app user != nil end |
#parse_json_body ⇒ Object
240 241 242 243 244 245 246 247 248 |
# File 'lib/vortex/sinatra.rb', line 240 def parse_json_body request.body.rewind body = request.body.read return {} if body.empty? JSON.parse(body) rescue JSON::ParserError halt 400, render_json({ error: 'Invalid JSON in request body' }) end |
#render_bad_request(message) ⇒ Object
263 264 265 |
# File 'lib/vortex/sinatra.rb', line 263 def render_bad_request() halt 400, render_json({ error: }) end |
#render_forbidden(message) ⇒ Object
259 260 261 |
# File 'lib/vortex/sinatra.rb', line 259 def render_forbidden() halt 403, render_json({ error: }) end |
#render_json(data) ⇒ Object
250 251 252 253 |
# File 'lib/vortex/sinatra.rb', line 250 def render_json(data) content_type :json JSON.generate(data) end |
#render_not_found(message) ⇒ Object
267 268 269 |
# File 'lib/vortex/sinatra.rb', line 267 def render_not_found() halt 404, render_json({ error: }) end |
#render_server_error(message) ⇒ Object
271 272 273 |
# File 'lib/vortex/sinatra.rb', line 271 def render_server_error() halt 500, render_json({ error: }) end |
#render_unauthorized(message) ⇒ Object
255 256 257 |
# File 'lib/vortex/sinatra.rb', line 255 def () halt 401, render_json({ error: }) end |
#vortex_client ⇒ Object
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/vortex/sinatra.rb', line 209 def vortex_client @vortex_client ||= begin api_key = settings.vortex_api_key base_url = settings.respond_to?(:vortex_base_url) ? settings.vortex_base_url : nil raise 'Vortex API key not configured' unless api_key Vortex::Client.new(api_key, base_url: base_url) end end |
#with_vortex_error_handling(&block) ⇒ Object
230 231 232 233 234 235 236 237 238 |
# File 'lib/vortex/sinatra.rb', line 230 def with_vortex_error_handling(&block) yield rescue Vortex::VortexError => e logger.error("Vortex error: #{e.}") if respond_to?(:logger) render_server_error("Vortex error: #{e.}") rescue => e logger.error("Unexpected error: #{e.}") if respond_to?(:logger) render_server_error("Internal server error") end |