Class: OpenRosa::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/open_rosa/middleware.rb

Overview

Rack middleware for OpenRosa endpoints

Provides endpoints for:

  • GET /formList - List available forms
  • GET /forms/:id - Download a specific form as XForm XML
  • POST /submission - Receive form submissions
  • HEAD /submission - Pre-flight check for submissions

Example usage:

use OpenRosa::Middleware do |config|
config.forms = [MyForm, AnotherForm]
config.mount_path = "/openrosa"

# Handle submissions
config.on_submission do |submission|
  # Save to database, process files, etc.
  MySubmission.create!(
    form_id: submission.form_id,
    data: submission.data,
    instance_id: submission.instance_id
  )
  "Thank you for your submission!"
end
end

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

OPENROSA_VERSION =

rubocop:disable Metrics/ClassLength

"1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, &block) ⇒ Middleware

Returns a new instance of Middleware.



35
36
37
38
39
# File 'lib/open_rosa/middleware.rb', line 35

def initialize(app = nil, &block)
  @app = app
  @config = Configuration.new
  block&.call(@config)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



33
34
35
# File 'lib/open_rosa/middleware.rb', line 33

def app
  @app
end

#configObject (readonly)

Returns the value of attribute config.



33
34
35
# File 'lib/open_rosa/middleware.rb', line 33

def config
  @config
end

Instance Method Details

#call(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/open_rosa/middleware.rb', line 41

def call(env)
  request = Rack::Request.new(env)

  return delegate_to_app(env) unless handle_request?(request)

  # Check authentication if configured
  return unauthorized_response unless authenticated?(env, request)

  route_request(request)
end