Skip to content

Commit

Permalink
Merge c95367b into ccd7efb
Browse files Browse the repository at this point in the history
  • Loading branch information
aldesantis committed Dec 23, 2018
2 parents ccd7efb + c95367b commit eac87b9
Show file tree
Hide file tree
Showing 2 changed files with 52 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

0 comments on commit eac87b9

Please sign in to comment.