module HaveAPI::Server::ServerHelpers
Public Instance Methods
Source
# File lib/haveapi/server.rb, line 73 def access_control return unless request.env['HTTP_ORIGIN'] && request.env['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] halt 200, { 'access-control-allow-origin' => '*', 'access-control-allow-methods' => 'GET,POST,OPTIONS,PATCH,PUT,DELETE', 'access-control-allow-credentials' => 'false', 'access-control-allow-headers' => settings.api_server.allowed_headers, 'access-control-max-age' => (60 * 60).to_s }, '' end
Source
# File lib/haveapi/server.rb, line 61 def authenticate!(v) require_auth! unless authenticated?(v) end
Source
# File lib/haveapi/server.rb, line 65 def authenticated?(v) return @current_user if @current_user @current_user = settings.api_server.send(:do_authenticate, v, request) settings.api_server.call_hooks_for(:post_authenticated, args: [@current_user]) @current_user end
Source
# File lib/haveapi/server.rb, line 122 def base_url scheme = if request.env['HTTP_X_FORWARDED_SSL'] == 'on' 'https' else request.env['rack.url_scheme'] end "#{scheme}://#{request.env['HTTP_HOST']}" end
Source
# File lib/haveapi/server.rb, line 118 def doc(file) markdown :"../../../doc/#{file}" end
Source
# File lib/haveapi/server.rb, line 133 def host request.env['HTTP_HOST'].split(':').first end
Source
# File lib/haveapi/server.rb, line 113 def logout_url ret = url("#{root}_logout") ret.insert(ret.index('//') + 2, '_log:out@') end
Source
# File lib/haveapi/server.rb, line 89 def pretty_format(obj) ret = '' PP.pp(obj, ret) end
Source
# File lib/haveapi/server.rb, line 102 def report_error(code, headers, msg) @halted = true content_type @formatter.content_type, charset: 'utf-8' halt code, headers, @formatter.format(false, nil, msg, version: false) end
Source
# File lib/haveapi/server.rb, line 94 def require_auth! report_error( 401, { 'www-authenticate' => 'Basic realm="Restricted Area"' }, 'Action requires user to authenticate' ) end
Source
# File lib/haveapi/server.rb, line 48 def setup_formatter return if @formatter @formatter = OutputFormatter.new unless @formatter.supports?(request.accept) @halted = true halt 406, "Not Acceptable\n" end content_type @formatter.content_type, charset: 'utf-8' end
Source
# File lib/haveapi/server.rb, line 141 def sort_hash(hash) hash.sort { |a, b| a[0] <=> b[0] } end