Module: HaveAPI::Server::ServerHelpers

Defined in:
lib/haveapi/server.rb

Instance Method Summary collapse

Instance Method Details

#access_controlObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/haveapi/server.rb', line 163

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

#add_vary_header(name) ⇒ Object



99
100
101
102
103
# File 'lib/haveapi/server.rb', line 99

def add_vary_header(name)
  values = response['Vary'].to_s.split(/\s*,\s*/).reject(&:empty?)
  values << name unless values.include?(name)
  headers 'Vary' => values.join(', ')
end

#api_versionObject



265
266
267
# File 'lib/haveapi/server.rb', line 265

def api_version
  @v
end

#authenticate!(v) ⇒ Object



122
123
124
# File 'lib/haveapi/server.rb', line 122

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

#authenticated?(v) ⇒ Boolean

Returns:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/haveapi/server.rb', line 126

def authenticated?(v)
  if @current_user
    resolve_request_locale
    return @current_user
  end

  begin
    @current_user = settings.api_server.send(:do_authenticate, v, request)
  rescue HaveAPI::Authentication::TokenConflict => e
    unless @formatter
      @formatter = OutputFormatter.new
      @formatter.supports?([])
    end

    report_error(400, {}, e.message)
  end
  settings.api_server.call_hooks_for(:post_authenticated, args: [@current_user])
  resolve_request_locale
  @current_user
end

#authenticated_versionsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/haveapi/server.rb', line 147

def authenticated_versions
  ret = settings.api_server.versions.each_with_object({}) do |v, users|
    users[v] = settings.api_server.send(:do_authenticate, v, request)
  rescue HaveAPI::Authentication::TokenConflict => e
    unless @formatter
      @formatter = OutputFormatter.new
      @formatter.supports?([])
    end

    report_error(400, {}, e.message)
  end

  resolve_request_locale(ret[settings.api_server.default_version] || ret.values.compact.first)
  ret
end

#base_urlObject



242
243
244
245
246
247
248
249
250
251
# File 'lib/haveapi/server.rb', line 242

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

#current_userObject



175
176
177
# File 'lib/haveapi/server.rb', line 175

def current_user
  @current_user
end

#doc(file) ⇒ Object



238
239
240
# File 'lib/haveapi/server.rb', line 238

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

#hostObject



253
254
255
# File 'lib/haveapi/server.rb', line 253

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

#logout_urlObject



233
234
235
236
# File 'lib/haveapi/server.rb', line 233

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

#pretty_format(obj) ⇒ Object



179
180
181
182
# File 'lib/haveapi/server.rb', line 179

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

#report_error(code, headers, msg) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/haveapi/server.rb', line 192

def report_error(code, headers, msg)
  @halted = true
  unless @formatter
    @formatter = OutputFormatter.new
    @formatter.supports?([])
  end

  content_type @formatter.content_type, charset: 'utf-8'
  halt code, headers, @formatter.format(false, nil, msg, version: false)
end

#report_exception(exception, context = nil) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/haveapi/server.rb', line 203

def report_exception(exception, context = nil)
  context ||= Context.new(
    settings.api_server,
    request: self,
    params:,
    endpoint: true
  )

  tmp =
    begin
      settings.api_server.call_hooks_for(
        :request_exception,
        args: [context, exception]
      )
    rescue StandardError => e
      warn "HaveAPI request exception hook failed: #{e.class}: #{e.message}"
      {}
    end

  report_error(
    tmp[:http_status] || 500,
    {},
    tmp[:message] || HaveAPI.message('haveapi.errors.server_error')
  )
end

#require_auth!Object



184
185
186
187
188
189
190
# File 'lib/haveapi/server.rb', line 184

def require_auth!
  report_error(
    401,
    { 'www-authenticate' => 'Basic realm="Restricted Area"' },
    HaveAPI.message('haveapi.authentication.required')
  )
end

#resolve_request_locale(user = current_user) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/haveapi/server.rb', line 82

def resolve_request_locale(user = current_user)
  setup_request_locale
  return @haveapi_locale if @haveapi_locale_requested

  @haveapi_locale = settings.api_server.resolved_locale(
    request:,
    current_user: user
  )
  settings.api_server.activate_locale(@haveapi_locale)
end

#restore_request_localeObject



93
94
95
96
97
# File 'lib/haveapi/server.rb', line 93

def restore_request_locale
  return unless @haveapi_locale_setup

  settings.api_server.activate_locale(@haveapi_previous_locale)
end

#rootObject



229
230
231
# File 'lib/haveapi/server.rb', line 229

def root
  settings.api_server.root
end

#setup_formatterObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/haveapi/server.rb', line 105

def setup_formatter
  return if @formatter

  @formatter = OutputFormatter.new
  accept = request.accept
rescue ArgumentError, EncodingError
  @formatter.supports?([])
  report_error(400, {}, HaveAPI.message('haveapi.errors.bad_accept_header'))
else
  unless @formatter.supports?(accept)
    @halted = true
    halt 406, "Not Acceptable\n"
  end

  content_type @formatter.content_type, charset: 'utf-8'
end

#setup_request_localeObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/haveapi/server.rb', line 61

def setup_request_locale
  return if @haveapi_locale_setup

  server = settings.api_server
  @haveapi_previous_locale = ::I18n.locale
  locale_header_value = server.locale_header_value(request)
  @haveapi_locale_requested = !locale_header_value.nil?
  @haveapi_explicit_locale = server.request_locale(request)
  @haveapi_locale = if @haveapi_locale_requested
                      @haveapi_explicit_locale || server.default_locale
                    else
                      server.resolved_locale(
                        request:,
                        current_user: nil
                      )
                    end
  server.activate_locale(@haveapi_locale)
  add_vary_header(server.locale_header) if server.locale_header
  @haveapi_locale_setup = true
end

#sort_hash(hash) ⇒ Object



261
262
263
# File 'lib/haveapi/server.rb', line 261

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

#urlescape(v) ⇒ Object



257
258
259
# File 'lib/haveapi/server.rb', line 257

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

#versionObject



269
270
271
# File 'lib/haveapi/server.rb', line 269

def version
  HaveAPI::VERSION
end