Skip to content

Commit

Permalink
Merge 56f46d2 into ccd7efb
Browse files Browse the repository at this point in the history
  • Loading branch information
aldesantis committed Dec 23, 2018
2 parents ccd7efb + 56f46d2 commit 376f764
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Implemented `#respond` and `#error` helpers

## [2.2.0]

### Added
Expand Down
48 changes: 48 additions & 0 deletions lib/pragma/operation/base.rb
Expand Up @@ -21,6 +21,54 @@ def operation_name
.downcase
.to_sym
end

def respond(skill, *args)
if args.size == 1 && args.first.is_a?(Hash)
options = args[0]
response = Pragma::Operation::Response.new(status: options.fetch(:status, 200))
elsif args.size >= 1 && args.size <= 2
response = args[0]
options = args[1] || {}

unless response.is_a?(Pragma::Operation::Response)
response = Pragma::Operation::Response.const_get(response).new
end
else
fail ArgumentError, "Expected 2..3 arguments, #{args.size + 1} given"
end

response.entity = options[:entity] if options.key?(:entity)
response.headers = response.headers.merge(options[:headers]) if options[:headers]

response.decorate_with(options[:decorator]) if options[:decorator]

skill['result.response'] = response
end

def error(skill, *args)
if args.size == 1 && args.first.is_a?(Hash)
options = args[0]

error = Pragma::Operation::Error.new(
error_type: options.fetch(:error_type),
error_message: options.fetch(:error_message),
meta: options.fetch(:meta, {})
)
elsif args.size >= 1 && args.size <= 2
error = args[0]
options = args[1] || {}
else
fail ArgumentError, "Expected 2..3 arguments, #{args.size + 1} given"
end

if defined?(Pragma::Decorator::Error) && !options.key?(:decorator)
options[:decorator] = Pragma::Decorator::Error
end

response = Pragma::Operation::Response.const_get(error.error_type).new(entity: error)

respond(skill, response, options)
end
end
end
end
Expand Down
48 changes: 48 additions & 0 deletions spec/pragma/operation/base_spec.rb
Expand Up @@ -21,4 +21,52 @@ def process!(_options, **)
it 'runs correctly' do
expect(result).to be_success
end

describe '#respond' do
context 'with an options hash' do
it 'builds a response from the options'
end

context 'with a symbol' do
it 'treats the argument as a response template name'
end

context 'with a response' do
it 'responds with the provided response'
end

context 'with a symbol and an options hash' do
it 'treats the argument as a response template name'
it 'overrides response parameters with the provided options'
end

context 'with a response and an options hash' do
it 'responds with the provided response'
it 'overrides response parameters with the provided options'
end
end

context '#error' do
context 'with an options hash' do
it 'builds an error from the options'
end

context 'with a symbol' do
it 'treats the argument as a response template name'
end

context 'with an error' do
it 'responds with the provided error'
end

context 'with a symbol and an options hash' do
it 'treats the argument as a response template name'
it 'overrides response parameters with the provided options'
end

context 'with an error and an options hash' do
it 'responds with the provided error'
it 'overrides response parameters with the provided options'
end
end
end

0 comments on commit 376f764

Please sign in to comment.