class Safrano::ServerApp

the main Rack server app. Source: the Rack docu/examples and partly inspired from Sinatra

Constants

DATASERVICEVERSION
METHODS_REGEXP
NOCACHE_HDRS

Public Class Methods

enable_batch() click to toggle source
# File lib/safrano/rack_app.rb, line 138
def self.enable_batch
  @service_base.enable_batch
end
get_service_base() click to toggle source
# File lib/safrano/rack_app.rb, line 146
def self.get_service_base
  @service_base
end
path_prefix(path_pr) click to toggle source
# File lib/safrano/rack_app.rb, line 142
def self.path_prefix(path_pr)
  @service_base.path_prefix path_pr
end
publish_service(&block) click to toggle source
# File lib/safrano/rack_app.rb, line 156
def self.publish_service(&block)
  sbase = Safrano::ServiceBase.new
  sbase.instance_eval(&block) if block_given?
  sbase.finalize_publishing
  set_servicebase(sbase)
end
set_servicebase(sbase) click to toggle source
# File lib/safrano/rack_app.rb, line 150
def self.set_servicebase(sbase)
  @service_base = sbase
  @service_base.enable_v1_service
  @service_base.enable_v2_service
end

Public Instance Methods

_call(env) click to toggle source
# File lib/safrano/rack_app.rb, line 116
def _call(env)
  begin
    @request = Safrano::Request.new(env)
    @response = Safrano::Response.new

    before.tap_error { |err| dispatch_error(err) }
          .tap_valid { |res| dispatch }

  # handle remaining Sequel errors that we couldnt prevent with our
  # own pre-checks
  rescue Sequel::Error => e
    dispatch_error(SequelExceptionError.new(e))
  end
  @response.finish
end
before() click to toggle source
# File lib/safrano/rack_app.rb, line 62
def before
  @request.service_base = self.class.get_service_base

  @request.negotiate_service_version.tap_valid do
    myhdrs = NOCACHE_HDRS.dup
    myhdrs[DATASERVICEVERSION] = @request.service.data_service_version
    headers myhdrs
  end
end
call(env) click to toggle source
# File lib/safrano/rack_app.rb, line 111
def call(env)
  # for thread safety
  dup._call(env)
end
dispatch() click to toggle source
# File lib/safrano/rack_app.rb, line 99
def dispatch
  req_ret = if @request.request_method !~ METHODS_REGEXP
              [404, EMPTY_HASH, ['Did you get lost?']]
            elsif @request.request_method == 'HEAD'
              odata_head
            else
              dispatch_with_walker
            end
  @response.status, rsph, @response.body = req_ret
  headers rsph
end
dispatch_error(err) click to toggle source
# File lib/safrano/rack_app.rb, line 94
def dispatch_error(err)
  @response.status, rsph, @response.body = err.odata_get(@request)
  headers rsph
end
dispatch_with_walker() click to toggle source

dispatch for all methods requiring parsing of the path with walker (ie. allmost all excepted HEAD)

# File lib/safrano/rack_app.rb, line 74
def dispatch_with_walker
  @walker = @request.create_odata_walker
  case @request.request_method
  when 'GET'
    odata_get
  when 'POST'
    odata_post
  when 'DELETE'
    odata_delete
  when 'OPTIONS'
    odata_options
  when 'PUT'
    odata_put
  when 'PATCH', 'MERGE'
    odata_patch
  else
    raise Error
  end
end
headers(hash = nil) click to toggle source

Set multiple response headers with Hash.

# File lib/safrano/rack_app.rb, line 133
def headers(hash = nil)
  @response.headers.merge! hash if hash
  @response.headers
end