Module: HaveAPI::Server::ServerHelpers

Defined in:
lib/haveapi/server.rb

Instance Method Summary collapse

Instance Method Details

#access_controlObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/haveapi/server.rb', line 32

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



20
21
22
# File 'lib/haveapi/server.rb', line 20

def authenticate!(v)
  require_auth! unless authenticated?(v)
end

#authenticated?(v) ⇒ Boolean

Returns:



24
25
26
27
28
29
30
# File 'lib/haveapi/server.rb', line 24

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_userObject



44
45
46
# File 'lib/haveapi/server.rb', line 44

def current_user
  @current_user
end

#doc(file) ⇒ Object



73
74
75
# File 'lib/haveapi/server.rb', line 73

def doc(file)
  markdown :"../../../doc/#{file}"
end

#logout_urlObject



68
69
70
71
# File 'lib/haveapi/server.rb', line 68

def logout_url
  ret = url("#{root}_logout")
  ret.insert(ret.index('//') + 2, '_log:out@')
end

#pretty_format(obj) ⇒ Object



48
49
50
51
# File 'lib/haveapi/server.rb', line 48

def pretty_format(obj)
  ret = ''
  PP.pp(obj, ret)
end

#report_error(code, headers, msg) ⇒ Object



58
59
60
61
62
# File 'lib/haveapi/server.rb', line 58

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

#require_auth!Object



53
54
55
56
# File 'lib/haveapi/server.rb', line 53

def require_auth!
  report_error(401, {'WWW-Authenticate' => 'Basic realm="Restricted Area"'},
               'Action requires user to authenticate')
end

#rootObject



64
65
66
# File 'lib/haveapi/server.rb', line 64

def root
  settings.api_server.root
end

#versionObject



77
78
79
# File 'lib/haveapi/server.rb', line 77

def version
  HaveAPI::VERSION
end