Module: ApiHelper

Extended by:
Grape::API::Helpers
Defined in:
app/helpers/api_helper.rb

Overview

Helper methods used in Grape's API endpoints.

Constant Summary collapse

DEFAULT_LIMIT =
20
DEFAULT_OFFSET =
0

Instance Method Summary collapse

Instance Method Details

#api_formatObject


34
35
36
# File 'app/helpers/api_helper.rb', line 34

def api_format
  env['api.format'].to_sym
end

#authenticate!Object


38
39
40
# File 'app/helpers/api_helper.rb', line 38

def authenticate!
  env['warden'].authenticate!
end

#authorize!(auth_class) ⇒ Object


51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/api_helper.rb', line 51

def authorize!(auth_class)
  # Hash with route parameter values plus :route_info from Grape.
  route_params = env['rack.routing_args']
  route_method = route.route_method.downcase.to_sym  # Matching URL template from Grape for current request, e.g. '/rooms/:kos_id/events'.

  uri = route_params[:route_info].route_namespace  # Hash with route parameter values for matching route, e.g. { kos_id: 'T9:105' }.

  params = route_params.except(:route_info)
  auth_class.new(current_user).authorize_request!(route_method, uri, params)
end

#current_userString?

Scope of authenticated user derived from access token


47
48
49
# File 'app/helpers/api_helper.rb', line 47

def current_user
  env['warden'].user
end

#not_found!Object


70
71
72
# File 'app/helpers/api_helper.rb', line 70

def not_found!
  error!({message: 'Resource not found'}, 404)
end

#params_hObject


30
31
32
# File 'app/helpers/api_helper.rb', line 30

def params_h
  params.to_h.symbolize_keys
end

#represent_paginated(dataset, representer) ⇒ Object


62
63
64
65
66
67
68
# File 'app/helpers/api_helper.rb', line 62

def represent_paginated(dataset, representer)
  paginated = Interactors::Api::Paginate.perform(dataset: dataset, params: params)
  representer
    .for_collection
    .new(paginated.dataset.all)
    .to_hash('meta' => paginated.meta)
end