Merge pull request #41 from github/twp/accept-json-encoded-request-body
Refactor params handling
This commit is contained in:
Коммит
d866ec0d04
6
Rakefile
6
Rakefile
|
@ -14,11 +14,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|||
rdoc.rdoc_files.include('lib/**/*.rb')
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Bundler::GemHelper.install_tasks
|
||||
|
||||
require 'rake/testtask'
|
||||
|
@ -30,5 +25,4 @@ Rake::TestTask.new(:test) do |t|
|
|||
t.verbose = false
|
||||
end
|
||||
|
||||
|
||||
task default: :test
|
||||
|
|
|
@ -27,10 +27,7 @@ module Chatops
|
|||
end
|
||||
|
||||
def process(*args)
|
||||
scrubbed_params = jsonrpc_params.except(
|
||||
:user, :mention_slug, :method, :controller, :action, :params, :room_id)
|
||||
|
||||
scrubbed_params.each { |k, v| params[k] = v }
|
||||
setup_params!
|
||||
|
||||
if params[:chatop].present?
|
||||
params[:action] = params[:chatop]
|
||||
|
@ -40,7 +37,7 @@ module Chatops
|
|||
end
|
||||
end
|
||||
|
||||
super *args
|
||||
super(*args)
|
||||
rescue AbstractController::ActionNotFound
|
||||
return jsonrpc_method_not_found
|
||||
end
|
||||
|
@ -52,8 +49,27 @@ module Chatops
|
|||
|
||||
protected
|
||||
|
||||
def setup_params!
|
||||
json_body.each do |key, value|
|
||||
next if params.has_key? key
|
||||
params[key] = value
|
||||
end
|
||||
|
||||
@jsonrpc_params = params.delete(:params) if params.has_key? :params
|
||||
|
||||
self.params = params.permit(:action, :chatop, :controller, :id, :mention_slug, :method, :room_id, :user)
|
||||
end
|
||||
|
||||
def jsonrpc_params
|
||||
params["params"] || {}
|
||||
@jsonrpc_params ||= ActionController::Parameters.new
|
||||
end
|
||||
|
||||
def json_body
|
||||
hash = {}
|
||||
if request.content_type =~ %r/\Aapplication\/json\Z/i
|
||||
hash = ActiveSupport::JSON.decode(request.raw_post) || {}
|
||||
end
|
||||
hash.with_indifferent_access
|
||||
end
|
||||
|
||||
# `options` supports any of the optional fields documented
|
||||
|
|
|
@ -50,14 +50,14 @@ module Chatops::Controller::TestCaseHelpers
|
|||
|
||||
named_params, command = extract_named_params(message)
|
||||
|
||||
matcher = matchers.find { |matcher| matcher["regex"].match(command) }
|
||||
matcher = matchers.find { |m| m["regex"].match(command) }
|
||||
|
||||
raise NoMatchingCommandRegex.new("No command matches '#{command}'") unless matcher
|
||||
|
||||
match_data = matcher["regex"].match(command)
|
||||
jsonrpc_params = named_params.dup
|
||||
matcher["params"].each do |param|
|
||||
jsonrpc_params[param] = match_data[param.to_sym]
|
||||
jsonrpc_params[param] ||= match_data[param.to_sym]
|
||||
end
|
||||
jsonrpc_params.merge!(user: user, room_id: room_id, mention_slug: user)
|
||||
chatop matcher["name"].to_sym, jsonrpc_params
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module ChatopsController
|
||||
VERSION = "3.1.1"
|
||||
VERSION = "3.2.0"
|
||||
end
|
||||
|
|
|
@ -14,8 +14,8 @@ describe ActionController::Base, type: :controller do
|
|||
chatop :wcid,
|
||||
/(?:where can i deploy|wcid)(?: (?<app>\S+))?/,
|
||||
"where can i deploy?" do
|
||||
return jsonrpc_invalid_params("I need nope, sorry") if params[:app] == "nope"
|
||||
jsonrpc_success "You can deploy #{params["app"]} just fine."
|
||||
return jsonrpc_invalid_params("I need nope, sorry") if jsonrpc_params[:app] == "nope"
|
||||
jsonrpc_success "You can deploy #{jsonrpc_params["app"]} just fine."
|
||||
end
|
||||
|
||||
chatop :foobar,
|
||||
|
@ -35,7 +35,7 @@ describe ActionController::Base, type: :controller do
|
|||
end
|
||||
|
||||
def ensure_app_given
|
||||
return jsonrpc_invalid_params("I need an app, every time") unless params[:app].present?
|
||||
return jsonrpc_invalid_params("I need an app, every time") unless jsonrpc_params[:app].present?
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче