Skip to content

Commit

Permalink
WIP - added universal matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
rpbaltazar committed Aug 6, 2019
1 parent 4e05da1 commit 35a2beb
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions lib/trailblazer/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Endpoint
DEFAULT_MATCHERS = {
created: {
rule: ->(result) { result.success? && result["model.action"] == :new },
resolve: ->(result, representer) do
resolve: lambda do |result, representer|
{
"data": representer.new(result["model"]),
"status": :created
Expand All @@ -12,36 +12,55 @@ class Endpoint
},
success: {
rule: ->(result) { result.success? },
resolve: ->(result, representer) do
resolve: lambda do |result, representer|
{
"data": representer.new(result["model"]),
"status": :ok
}
end
},
unauthenticated: {
rule: ->(result) { result.failure? && result["result.policy.default"]&.failure? },
resolve: ->(_result, _representer) do
rule: lambda do |result|
result.failure? && result["result.policy.default"]&.failure?
end,
resolve: lambda do |_result, _representer|
{
"data": {},
"status": :unauthorized
}
end
},
not_found: {
rule: ->(result) { result.failure? && result["result.model"]&.failure? },
resolve: ->(_result, _representer) do
rule: lambda do |result|
result.failure? && result["result.model"]&.failure?
end,
resolve: lambda do |_result, _representer|
{
"data": {},
"status": :not_found
}
end
},
contract_failure: {
rule: ->(result) { result.failure? && result["result.contract.default"]&.failure? },
resolve: ->(result, _representer) do
rule: lambda do |result|
result.failure? && result["result.contract.default"]&.failure?
end,
resolve: lambda do |result, _representer|
{
"data": { messages: result["result.contract.default"]&.errors&.messages },
"data": {
messages: result["result.contract.default"]&.errors&.messages
},
"status": :unprocessable_entity
}
end
},
fallback: {
rule: ->(_result) { true },
resolve: lambda do |_result, _representer|
{
"data": {
messages: ["Unexpected operation result"]
},
"status": :unprocessable_entity
}
end
Expand All @@ -52,7 +71,6 @@ class Endpoint
# it might have a representer, else will assume the default name
def self.call(operation_result, representer_class = nil, overrides = {})
representer = operation_result["representer.serializer.class"] || representer_class
# TODO: What to do when nothing matches?
endpoint_opts = { result: operation_result, representer: representer }
new.(endpoint_opts, overrides)
end
Expand All @@ -66,7 +84,9 @@ def call(options, overrides)
next
end

return resolve.(options[:result], options[:representer]) if rule.(options[:result])
if rule.(options[:result])
return resolve.(options[:result], options[:representer])
end
end
matching_rules(overrides).each do |_rule_key, rule_description|
if rule_description[:rule].(options[:result])
Expand Down

0 comments on commit 35a2beb

Please sign in to comment.