Module: HaveAPI::Server::ServerHelpers
- Defined in:
- lib/haveapi/server.rb
Instance Method Summary collapse
- #access_control ⇒ Object
- #authenticate!(v) ⇒ Object
- #authenticated?(v) ⇒ Boolean
- #current_user ⇒ Object
- #doc(file) ⇒ Object
- #logout_url ⇒ Object
- #pretty_format(obj) ⇒ Object
- #report_error(code, headers, msg) ⇒ Object
- #require_auth! ⇒ Object
- #root ⇒ Object
- #version ⇒ Object
Instance Method Details
#access_control ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/haveapi/server.rb', line 25 def access_control if 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 end |
#authenticate!(v) ⇒ Object
13 14 15 |
# File 'lib/haveapi/server.rb', line 13 def authenticate!(v) require_auth! unless authenticated?(v) end |
#authenticated?(v) ⇒ Boolean
17 18 19 20 21 22 23 |
# File 'lib/haveapi/server.rb', line 17 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 |
#current_user ⇒ Object
37 38 39 |
# File 'lib/haveapi/server.rb', line 37 def current_user @current_user end |
#doc(file) ⇒ Object
66 67 68 |
# File 'lib/haveapi/server.rb', line 66 def doc(file) markdown :"../../../doc/#{file}" end |
#logout_url ⇒ Object
61 62 63 64 |
# File 'lib/haveapi/server.rb', line 61 def logout_url ret = url("#{root}_logout") ret.insert(ret.index('//') + 2, '_log:out@') end |
#pretty_format(obj) ⇒ Object
41 42 43 44 |
# File 'lib/haveapi/server.rb', line 41 def pretty_format(obj) ret = '' PP.pp(obj, ret) end |
#report_error(code, headers, msg) ⇒ Object
51 52 53 54 55 |
# File 'lib/haveapi/server.rb', line 51 def report_error(code, headers, msg) @halted = true content_type @formatter.content_type, charset: 'utf-8' halt code, headers, @formatter.format(false, nil, msg) end |
#require_auth! ⇒ Object
46 47 48 49 |
# File 'lib/haveapi/server.rb', line 46 def require_auth! report_error(401, {'WWW-Authenticate' => 'Basic realm="Restricted Area"'}, 'Action requires user to authenticate') end |
#root ⇒ Object
57 58 59 |
# File 'lib/haveapi/server.rb', line 57 def root settings.api_server.root end |