Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added shared_params helper #5

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions guides/building_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ API is an important part of web applications. They provide interfaces for commun
* Each API resource class (e.g. `UsersResource`) defines a single resource (e.g. `resource :users do ... end`).
* Entities are stored in `entities/` directory and inherited from `Grape::Entity` class.
* Each API resource, entity class or helpers module class are wrapped into corresponding module (e.g. `AppV1API::Resources`, `AppV1API::Entities`, `AppV1API::Helpers`).
* For duplicate params across the project use special `SharedParamsHelper` helper.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This sentence is not very English ;)
  2. It is useful for sharing params between several API resources. If params are shared between "actions" of the same resources then it is better to keep them inside the resource.

Probbly, something like this will sound a bit better:
"SharedParamsHelper helper may be used to share params between resources."

* Entity class name corresponds with the model it is related to (e.g. `UserEntity` for `User` model, `OrderEntity` for `Order` model).
* Specs directory structure follows same directory names (e.g. `spec/apis/app_v1_api/`, `spec/apis/mobile_v1_api/`).

Expand All @@ -30,6 +31,7 @@ apis/
helpers/
api_helper.rb
users_api_helper.rb
shared_params_helper.rb
api.rb
mobile_v1_api/ - API for mobile apps (version 1)
...
Expand Down Expand Up @@ -109,6 +111,28 @@ module AppV1API::Helpers
end
end

# apis/app_v1_api/helpers/shared_params_helper.rb
module AppV1API::Helpers
module SharedParamsHelper
extend Grape::API::Helpers

params :user do
# List of API params, for reusing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, is not needed.

end
end
end

module AppV1API::Resources
class UsersResource < Grape::API
helpers AppV1API::Helpers::SharedParamsHelper

resource :user do
params do
use :user # Call `params` which set at SharedParamsHelper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Call -> invokes
  • which set - > which is defined

end
end
end

# spec/apis/app_v1_api/resources/sessions_resource_spec.rb
require 'rails_helper'

Expand Down