Module: HaveAPI::Server::ServerHelpers

Defined in:
lib/haveapi/server.rb

Instance Method Summary collapse

Instance Method Details

#access_controlObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/haveapi/server.rb', line 47

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

#api_versionObject



115
116
117
# File 'lib/haveapi/server.rb', line 115

def api_version
  @v
end

#authenticate!(v) ⇒ Object



35
36
37
# File 'lib/haveapi/server.rb', line 35

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

#authenticated?(v) ⇒ Boolean

Returns:



39
40
41
42
43
44
45
# File 'lib/haveapi/server.rb', line 39

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

#base_urlObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/haveapi/server.rb', line 92

def base_url
  if request.env['HTTP_X_FORWARDED_SSL'] == 'on'
    scheme = 'https'

  else
    scheme = request.env['rack.url_scheme']
  end

  "#{scheme}://#{request.env['HTTP_HOST']}"
end

#current_userObject



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

def current_user
  @current_user
end

#doc(file) ⇒ Object



88
89
90
# File 'lib/haveapi/server.rb', line 88

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

#hostObject



103
104
105
# File 'lib/haveapi/server.rb', line 103

def host
  request.env['HTTP_HOST'].split(':').first
end

#logout_urlObject



83
84
85
86
# File 'lib/haveapi/server.rb', line 83

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

#pretty_format(obj) ⇒ Object



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

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

#report_error(code, headers, msg) ⇒ Object



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

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



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

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

#rootObject



79
80
81
# File 'lib/haveapi/server.rb', line 79

def root
  settings.api_server.root
end

#sort_hash(hash) ⇒ Object



111
112
113
# File 'lib/haveapi/server.rb', line 111

def sort_hash(hash)
  hash.sort { |a, b| a[0] <=> b[0] }
end

#urlescape(v) ⇒ Object



107
108
109
# File 'lib/haveapi/server.rb', line 107

def urlescape(v)
  CGI.escape(v)
end

#versionObject



119
120
121
# File 'lib/haveapi/server.rb', line 119

def version
  HaveAPI::VERSION
end