I've got an API resource to fetch transaction list. There are few filters and params to make it sortable e.g. per_page
, status
etc. which may be passed on but only user_id
is required. Now my method looks like this:
def list(user_id:, page: nil, per_page: nil, status: nil, payment_id: nil, start_date: nil,
end_date: nil, max_amount: nil)
filters = { start_date:, end_date:, max_amount:, status:, payment_id: }.compact_blank!
params = { filters:, page:, per_page: }.compact_blank!
begin
body = get("users/#{user_id}/transactions", params:).body
rescue Errors::NotFoundError
nil
end
resource_list(body, BaseStruct::Transaction)
end
This code produces me Rubocop error of:
Metrics/ParameterLists: Avoid parameter lists longer than 5 parameters. [8/5]
. I know I could get rid of it by # rubocop:disable Metrics/ParameterLists
but I don't think that's the best idea. Is it possible to pass those not required field in a different way to avoid that error?
CodePudding user response:
Since you are passing 3 kinds of parameters to the list
method, you can group them and pass them to the method like below:
def list(user_id, filter_params, page_params)
filters = filter_params.compact_blank!
params = { filter_params, page_params.slice(:page), page_params.slice(:per_page) }.compact_blank!
begin
body = get("users/#{user_id}/transactions", params).body
rescue Errors::NotFoundError
nil
end
resource_list(body, BaseStruct::Transaction)
end
list(user_id, { status: nil, payment_id: nil, start_date: nil, end_date: nil, max_amount: nil }, { page: nil, per_page: nil })
CodePudding user response:
There is CountKeywordArgs option that I found useful to set false.
My rationale is that kwargs add less complexity than positional or optional parameters. It's more effective replacement of old-school Hash options
argument.