-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from sineed/sineed-custom-parameter-method
Custom parameter method
- Loading branch information
Showing
5 changed files
with
121 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'rspec_api_documentation/dsl/endpoint/set_param' | ||
|
||
module RspecApiDocumentation | ||
module DSL | ||
module Endpoint | ||
class Params | ||
attr_reader :example_group, :example | ||
|
||
def initialize(example_group, example, extra_params) | ||
@example_group = example_group | ||
@example = example | ||
@extra_params = extra_params | ||
end | ||
|
||
def call | ||
parameters = example.metadata.fetch(:parameters, {}).inject({}) do |hash, param| | ||
SetParam.new(self, hash, param).call | ||
end | ||
parameters.deep_merge!(extra_params) | ||
parameters | ||
end | ||
|
||
private | ||
|
||
attr_reader :extra_params | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module RspecApiDocumentation | ||
module DSL | ||
module Endpoint | ||
class SetParam | ||
def initialize(parent, hash, param) | ||
@parent = parent | ||
@hash = hash | ||
@param = param | ||
end | ||
|
||
def call | ||
return hash if path_params.include?(path_name) | ||
return hash unless method_name | ||
|
||
hash.deep_merge build_param_hash(key_scope || [key]) | ||
end | ||
|
||
private | ||
|
||
attr_reader :parent, :hash, :param | ||
delegate :example_group, :example, to: :parent | ||
|
||
def key | ||
@key ||= param[:name] | ||
end | ||
|
||
def key_scope | ||
@key_scope ||= param[:scope] && Array(param[:scope]).dup.push(key) | ||
end | ||
|
||
def scoped_key | ||
@scoped_key ||= key_scope && key_scope.join('_') | ||
end | ||
|
||
def custom_method_name | ||
param[:method] | ||
end | ||
|
||
def path_name | ||
scoped_key || key | ||
end | ||
|
||
def path_params | ||
example.metadata[:route].scan(/:(\w+)/).flatten | ||
end | ||
|
||
def method_name | ||
@method_name ||= begin | ||
[custom_method_name, scoped_key, key].find do |name| | ||
name && example_group.respond_to?(name) | ||
end | ||
end | ||
end | ||
|
||
def build_param_hash(keys) | ||
value = keys[1] ? build_param_hash(keys[1..-1]) : example_group.send(method_name) | ||
{ keys[0].to_s => value } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters