Class: V1::ApiController

Inherits:
ApplicationController show all
Includes:
Metadata, Pagination, Parameters
Defined in:
app/controllers/v1/api_controller.rb,
app/controllers/v1/api_controller/metadata.rb,
app/controllers/v1/api_controller/pagination.rb,
app/controllers/v1/api_controller/parameters.rb

Overview

API Controller Provides information about the API

Defined Under Namespace

Modules: Metadata, Pagination, Parameters

Constant Summary

Constants included from Parameters

Parameters::PARAMS

Instance Method Summary collapse

Methods included from Metadata

#meta_attributes, #meta_pagination

Methods included from Pagination

#compile_page, #select_page

Methods included from Parameters

#check_params, #expand_resources?, #numeric?, #true?

Instance Method Details

#check_params_required(*values) ⇒ Object


75
76
77
78
79
80
81
82
83
# File 'app/controllers/v1/api_controller.rb', line 75

def check_params_required(*values)
  values.each do |param|
    unless params.has_key? param.to_s
      render_error_exchange(:param_not_found, :bad_request, {param_required: "You need to set param #{param.to_s}"})
      return false
    end
  end
  true
end

#return_response(collection, state = :ok, metadata = {}, role = nil) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/v1/api_controller.rb', line 56

def return_response(collection, state = :ok,  = {}, role = nil)
  if check_params
    result = select_page(collection) if collection.class.name.include?('ActiveRecord')
    args = {
        json: result || collection,
        namespace: self.class.to_s.split("::").first.constantize,
        status: state,
        meta: meta_attributes(result || collection, ),
        adapter: :json,
        root: 'data'
    }
    args[:expand] = true if expand_resources?
    args[role.to_sym] = true if role
    render *[args]
  else
    render json: { errors: @error_object }
  end
end

#versionObject

@api get / Request version information

@api {get} /api/version Get API Version
@apiVersion 1.0.0
@apiName GetVersion
@apiPermission none
@apiGroup Api
@apiSuccess {String} version Version of the API
@apiSuccess {Object[]} providers Authentication endpoints
@apiSuccess {String=["github"]} providers.type Type of provider oauth
@apiSuccess {Boolean} providers.enabled Whether the provide is enabled or not
@apiSuccess {String} providers.id_application Id of the application in the oauth provider
@apiSuccess {String} providers.server Server Address
@apiSuccess {String} providers.version API Prefix
@apiSuccess {Boolean} providers.verify Does it need verify?
@apiSuccessExample {json} Success

{

"data": {
    "version": "1.0",
    "providers": {
        "github.com": {
            "type": "github",
            "enabled": true,
            "id_application": "ID_OF_APPLICATION",
            "server": "api.github.com",
            "version": "v3",
            "verify": true
        }
    }
}

}


44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/v1/api_controller.rb', line 44

def version
  logger.debug 'Creating session, verifying code'
  providers = {}
  Rails.application.secrets.oauth_providers.each do |pr|
    pro = pr.clone
    pro.delete(:secret)
    name = pro.delete(:name)
    providers[name] = pro if pro[:enabled]
  end
  render json: { data: { version: Rails.application.config.api_version, providers: providers } }, status: :ok
end